1

I want to refresh my data, obtained from an external PHP page, every second. The PHP sends JSON data, retrieved with an AJAX call. With this code, I see the table correctly, but I need to refresh my browser to see the new data.

$(document).ready(function() {
    $("#list").jqGrid({
        url: 'get_data.php',
        datatype: 'json',
        mtype: 'GET',
        jsonReader: {
            repeatitems : false,
        },
        colNames: [.............],
        colModel: [.............],
        autowidth: true,
        height: 'auto',
        loadonce: true,
        key: true,
        altRows: true,
        altclass: 'odd',
        rowNum: 100,
        viewrecords: true,
        gridview: true,
        gridComplete: function(){
            if(this.x == undefined){
                var j = 0;
                this.x = 1;
                while(j < mydata2.length){
                    jQuery("#list").addRowData(mydata2[j].id, mydata2[j]);
                    j++;
                }
            }
            return true;
        }
    })
});

To update the data I've already tried this:

var $grid = $("#list"), timer;
timer = setInterval(function () {
    $grid.trigger('reloadGrid', [{current: true, datatype: 'json', url: 'get_data.php'}]);
}, 1000);

And this:

var refreshId = setInterval(function() {
    // ... jqGrid function ...
}, 1000);

but neither worked.

1 Answers1

0

The main error is the usage datatype: 'json', url: 'get_data.php' as an option of reloadGrid. The method reloadGrid understand only two options: current and page (see the answer). What you need is to reset datatype option of jqGrid because of usage loadonce: true. So you need replace the line

$grid.trigger('reloadGrid',
    [{current: true, datatype: 'json', url: 'get_data.php'}]);

called inside of setInterval to the following

$grid.jqGrid("setGridParam", {datatype: "json"})
   .trigger("reloadGrid", [{current: true}]);

It should solve you main problem with refreshing of data. Additionally I would recommend you to examine the code of gridComplete which seems me suspected. I don't full understand it. You should take in consideration that addRowData calls internal updatepager method which calls gridComplete and can follows to recursion (see the line of code and the answer).

Community
  • 1
  • 1
Oleg
  • 220,925
  • 34
  • 403
  • 798
  • Hi Oleg, thanks for reply. i have try your code `var refreshId = setInterval(function() { $('#list').jqGrid("setGridParam", {datatype: "json"}).trigger("reloadGrid", [{current: true}]); }, 1000);` but in Inspect Element of Chrome I don't see ajax call every second on get_data.php If I try to write a javascript alert into setInterval it's successful runned every second – Francesco Langiulli Jul 17 '13 at 12:06
  • @FrancescoLangiulli: It sounds strange. You can include `beforeRequest` and `loadBeforeSend` callbacks with `alert` and `loadError`. See [the answer](http://stackoverflow.com/a/6969114/315935) for some variations of the code of `loadError`. You can additionally to use `jquery.jqGrid.src.js` instead of `jquery.jqGrid.min.js` and debug the code of `jquery.jqGrid.src.js` near the first call of `$.ajax`. – Oleg Jul 17 '13 at 12:17