How to add dynamically rows using ajax return type is json with first row position? In JQgrid

巧了我就是萌 提交于 2019-11-29 18:06:57

First of all you need define the rowid of input data. The grid have the option jsonReader: {repeatitems: false, id: "ID"} and the column having name: 'id'. It means that every item of input data have to have unique "ID" property and should have additional "id" property. It seems strange.

Seconds, if you add new row using addRowData method you should use rowid as the first parameter. In your case the rowid will be specified by "ID" property of input data. So you should use something like data.ID as the first parameter of addRowData method. The code which you posted don't contains any clear description of the response from URL "server.php?q=5". If the input data is one item of data then the code should be samGrid.jqGrid("addRowData", data.ID, data). On the other side if data is array of items having "ID" as the property which specify the rowid then you should use samGrid.jqGrid("addRowData", "ID", data) to add the multiple rows.

The next problem which you have to solve is validation of rowid duplicates. Let us "server.php?q=5" returns one item to simplify the situation at the beginning. Is it possible that the item with the same ID property already exist in the grid? In the case you have to use setRowData instead of addRowData to modify/replace existing row instead of adding row and getting id duplicates which is not allowed in HTML.

One more problem in your question. You wrote that you don't want to append the data as the last row. On the other side you wrote that you want to have sorted data. So the position of the added data should depend on contain of name column (you use sortname: 'name' option).

Probably the most simple way to implement autorefresh of data by replacing all data in the grid. In the way the possible merge (modification of existing data) will be done on the server and the server could just return the full set of current data. Because you use loadonce: true then datatype of grid will be change to "local" after the first loading from the server and so you can work with the grid like with the grid having local data. It means the data parameter with array of items exist in the grid. by usage of

var intData = $("#list5").jqGrid("getGridParam", "data");

you get the reference to internal array with the data. Using

success: function (data) {
    var $mygrid = $("#list5"),
        allGridParams = $mygrid.jqGrid("getGridParam");
    allGridParams.data = data;
    $mygrid.trigger("reloadGrid", [{current: true}]);
}

Because $("#list5").jqGrid("getGridParam") represent the reference to all internal parameters of jqGrid the line allGridParams.data = data; replace old internal data to another array of items. The next line reload local grid. It means that the data will be sorted by sortname and the first page of the size specified by rowNum will be displayed. The option {current: true} of reloadGrid will try to select the same row after reloading which was selected.

Of cause you have to fix syntax error in your code (the line url: server.php ? q = 5, should be replacted to url: "server.php?q=5",) and you have to move the lines

setTimeout((function (param) {
    return function () {
        poll();
    };
})(), 180000);

out of body of the poll function.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!