jQWidgets Forums

jQuery UI Widgets Forums Grid Grid in virtual mode calls server-side script twice

This topic contains 5 replies, has 3 voices, and was last updated by  ngc2 7 years, 2 months ago.

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author

  • Dan Fad
    Participant

    hello,

    i have a grid in virtual mode. i set the grid in a two step process.

    first step is setting the grid columns.
    second step is loading data on a click of a button.

    the problem is that the server-side script is called twice: during setting of the grid source ( source:dataAdapter ) and again when rendergridrows function returns dataAdapter.records.

    here is the sample code:

    <script type="text/javascript">
    $(document).ready(function () {
    var theme=getTheme();
    /*first step is setting the grid coulmns*/
    $("#jqxgrid").jqxGrid({
    altrows: true,
    theme:theme,
    autoheight: true,
    pagesize: 20,
    pageable: true,
    columns: [
    { text: 'Lastname', datafield: 'col1', width: 150},
    { text: 'Firstname', datafield: 'col2', width: 150 },
    { text: 'Birthdate', datafield: 'col3', width: 100 }
    ]
    });
    /*second step is loading data on click of a button*/
    $("#btnLoad").jqxButton({ width: '160', theme: theme});
    $("#btnLoad").on('click', function () {
    var source ={
    datatype: "json",
    datafields: [
    { name: 'col1'},
    { name: 'col2'},
    { name: 'col3'},
    ],
    url:'randomdata.php',
    root: 'Rows',
    cache: false,
    async:true,
    beforeprocessing: function(data){
    if (data) {
    source.totalrecords = data[0].TotalRows;
    }
    }
    };
    var dataAdapter = new $.jqx.dataAdapter(source, {
    loadComplete: function (data) {
    /* here this function is called twice */
    }
    }
    );
    $("#jqxgrid").jqxGrid({
    virtualmode: true,
    source: dataAdapter,
    rendergridrows: function(){
    return dataAdapter.records;
    }
    });
    });
    });
    </script>
    </head>
    <body>
    <input type="button" value="Load Data" id='btnLoad'/>
    <div id="jqxgrid"></div>
    <div id="eventsLog" style="margin-top: 10px;"> </div>
    </body>
    </html>

    here is the server side script – generates random data {randomdata.php}

    <?php
    /*simulated data as if coming from MySql*/
    /*this is just a random text data*/
    $rows=10;
    for($r=1;$r<=$rows;$r++) {
    $ln = substr(str_shuffle(str_repeat("abcdefghijklmnopqrstuvwxyz", 6)), 0, 5); /*Lastname*/
    $fn = substr(str_shuffle(str_repeat("abcdefghijklmnopqrstuvwxyz", 8)), 0, 5); /*Firstname*/
    $int= mt_rand(100000000,1262055681);
    $bdate = date("m-d-Y",$int);
    $rowdata[]=array(
    'col1'=>ucfirst($ln),
    'col2'=>ucfirst($fn),
    'col3'=>$bdate
    );
    }
    $data[] = array(
    'TotalRows' => $rows,
    'Rows' => $rowdata
    );
    writetofile(json_encode($data)); /* writes data to text file -> logs.txt */
    echo json_encode($data);
    /*
    this function will write data to text file
    this will check if this script is called twice
    */
    function writetofile($content) {
    $filename="logs.txt";
    $file = fopen($filename, 'a'); //w-> write; a->apend
    fwrite($file, $content."\n");
    fclose($file);
    }
    ?>

    am i doing it right?

    thanks,
    dan fad


    Peter Stoev
    Keymaster

    Hi,

    The following code should not be there:

    	$("#jqxgrid").jqxGrid({
    virtualmode: true,
    source: dataAdapter,
    rendergridrows: function(){
    return dataAdapter.records;
    }
    });

    If you want to load a Grid with data, then just set its “source” property. The other 2 properties should be set during the initialization.

    Best Regards,
    Peter Stoev

    jQWidgets Team
    http://www.jqwidgets.com


    Dan Fad
    Participant

    Hello Peter,

    Thank you for the reply.

    I transferred these two properties in the initialization stage (virtualmode and rendergridrows) but when I clicked the Load button, the data were not shown in the grid. Only the loader image is shown.

    Am I missing some properties?

    Best Regards,
    Dan Fad


    Peter Stoev
    Keymaster

    Hi,

    Most probably that is due to the fact that you will try to access “return dataAdapter.records;” and dataAdapter will be undefined. I suppose that you will have to either define the dataAdapter before that or use the “rendergridrows” “data” parameter instead.

    Best Regards,
    Peter Stoev

    jQWidgets Team
    http://www.jqwidgets.com


    Dan Fad
    Participant

    Hi Peter

    I transferred the initialization of the source and dataAdapter on top of the grid. This solves the problem.

    Thank you very much,
    Dan Fad


    ngc2
    Participant

    How can you get this to work with server-side pagination?

Viewing 6 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic.