JqG​​rid 从服务器更新数据

JqGrid update data from server

我们正在使用 jgGrid,它工作得很好。让我解释一下网格是如何设置的,我们正在从服务器获取 json 数据,loadonce: true.

现在我们想每 20 秒更新一次网格,所以

setInterval(function () {
                $("#jqGrid").setGridParam({ datatype: 'json', page: 1 }).trigger('reloadGrid', [{ current: true }]);

            }, 20000);

这工作正常。问题是,它完全刷新了网格,我们想要更新已更改的数据。我的意思是,如果一列中只有一个单元格发生变化,则只应更改该单元格。

刷新整个网格会导致排序和搜索过滤器出现问题。它会在 20 秒后替换所有内容。

提前致谢

解决问题的最简单方法是使用 beforeProcessing 回调,如果数据未更改,则 returns false。如果从服务器返回的数据未更改(与之前的响应相比),它将阻止重新加载网格。我在 the answer. The main problem is: how to determine that the returned data are the same. In the referenced old answer I calculated MD5 cache from the data on the server side and set the value as Etag. Alternatively you can use CryptoJS to make the calculation on the client side. The 3-d parameter of beforeProcessing callback is jqXHR, which is a superset of the XMLHTTPRequest object 中详细描述了场景。它包含例如 responseText 属性,这简化了 MD5 计算。通过使用 jqXHR.getResponseHeader("ETag") 您可以访问 ETag HTTP header.

重新加载整个网格 通常不会造成性能问题。重要的是,网格的 one 单元格的修改遵循 reflow 或可能改变页面上其他元素的位置。整个网格的重新加载实现为 one 分配 <tbody> 网格(如果您使用正确填充网格并且如果您使用 gridview: true 选项)。这似乎有很多额外的工作,但在实践中可能会更快,因为在网格的多行中顺序更改多个单元格。

无论如何,我建议您从实施 beforeProcessing 回调开始,在服务器数据未更改的情况下 returns false。只有当你有一些真正的性能问题时,你才应该详细分析性能问题,并可能对现有代码进行更深入的更改。