I have an application which polls a database and updates the jqGrid at runtime. I am using datatype: "local" in order to have the ability to manipulate the data on the client- side without having to reload everything.
My first attempt updated that data itself, updated the grid's data and reloaded it. This worked, but in IE8 (our main target unfortunately) there is a flicker when the scrollbar is reset to its original position. There is also the issue that the selections are reset, but that one would be resolvable.
grid.setGridParam({ data: localData });
var scrollPosition = grid.closest(".ui-jqgrid-bdiv").scrollTop();
grid.trigger('reloadGrid');
grid.closest(".ui-jqgrid-bdiv").scrollTop(scrollPosition);
My second attempt updates the individual rows themselves depending on the operation.
if (toUpdate) { /* Not auto sorted */
grid.jqGrid('setRowData', entityUpdate.EntityId, entityUpdate);
}
else if(toAdd) { /* Not auto sorted */
grid.jqGrid('addRowData', entityUpdate.EntityId, entityUpdate);
}
else if(toDelete) {
grid.jqGrid('delRowData', entityUpdate.EntityId);
}
This works great. The selections are not reset, there is no flicker, however there is one last issue: the rows are not resorted.
Any row that is updated stays where it is, and any row that is added does not go in the right place. I can use the "sortGrid" method, but then we're back to refreshing the whole grid. I can use a combination of the "position" and "srcrowid" parameters of the addRowData method, in order to place it in the right place, but I would have to know exactly where to put it. Is there a way to use the built in sorting algorithms to find where to put it? The code would become:
if (toUpdate) {
grid.jqGrid('delRowData', entityUpdate.EntityId);
grid.jqGrid('addRowData', entityUpdate.EntityId, entityUpdate, ?, ?);
}
else if(toAdd) {
grid.jqGrid('addRowData', entityUpdate.EntityId, entityUpdate, ?, ?);
}
else if(toDelete) {
grid.jqGrid('delRowData', entityUpdate.EntityId);
}