通过 Ajax 刷新表列
我有一个网页,其中包含我的系统上运行的作业表。 当这些作业完成或改变状态时,表需要反映这些变化。
目前我每 8 秒通过 AJAX 刷新一次整个表。 这对于一定数量的作业来说效果很好,但会导致非常高(~100)的作业负载的性能问题,最终导致浏览器锁定。
我正在使用 struts2 的 sx:div 来执行这些更新,我相信它每次都会解析返回的表以搜索小部件,从而关闭此功能。
我认为仅刷新状态列将对此有很大帮助,因为返回的数据会小得多,但我不确定这将如何工作。
我愿意使用 struts 标签以外的其他东西来执行此操作。 所以本质上我问是否有一种简单的方法来刷新表的列。
提前致谢。
I have a web page containing a table of jobs running on my system. As these jobs finish or change state the table needs to reflect the changes.
Currently I refresh the whole table via AJAX every 8 seconds. This works fine for a certain number of jobs, but causes performance issues with very high (~100) job loads, eventually causing browsers to lock up.
I am using struts2's sx:div to perform theses updates and I believe it is parsing the returned table each time to search for widgets, turning this off breaks functionality.
I think refreshing just the status column would help greatly with this as the data returned will be much smaller, but I am unsure how this will work.
I'm open to using something other than the struts tag to perform this. So in essence I am asking is there a simple way to just refresh a column of a table.
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
仅更新状态列中的文本肯定比重新呈现整个表表现得更好。 节省带宽可能并不是那么大的胜利。 但这只是我的直觉——你当然应该检查并衡量什么最适合你。
当每次更新中仅某些行的状态发生变化时,仅更新已更改的行也应该会提高性能。
此外,正如 m_oLogin 建议的那样,您可以考虑仅发送更改的增量。 尽管根据我的经验,这并不是最容易做对的事情,所以我不会从那里开始。
Updating only the texts in status column should definetely perform better than re-rendering the whole table. The saving from bandwidth probably isn't that big win. But that's just my gut feeling - you should of course check and measure what works best for you.
When the status of only some rows changes in each update, then updating only the changed rows should also improve performance.
Also, as m_oLogin suggested, you can consider sending only the deltas of the changes. Although in my experience it's not the easiest thing to get right, so I wouldn't start from there.
如果每列都有一个类(
col-1
、col-2
等)。 您的 Ajax 调用只能查询该行并将新文本输出为分隔字符串。如果您使用 jQuery.ajax,您可以提供
success
回调函数,其中代码首先分割字符串。 然后使用$('.col-X').each()
循环遍历列的每个元素,以更新其innerhtml
。If every column has a class (
col-1
,col-2
, etc.). Your Ajax call can query for the row only and output the new text as a delimited string.If you are using jQuery.ajax you can provide the
success
callback function with code first splitting your string. Then use$('.col-X').each()
to loop through each element of the column in order updating theirinnerhtml
.当你说“我每8秒刷新一次整个表”时,你的意思是你每8秒检索一次整个json数据吗? 因为如果是这种情况,那么速度变慢是正常的...
我会跟踪服务器端的更改,并在没有数据更改的情况下出现查询时发送一个小的“同表”消息。 仅当服务器端发生更改时才发回表数据。
When you say "I refresh the whole table every 8 seconds", do you mean that you retrieve the entire json data every 8 seconds? Because if that's the case, it's normal to have slow-downs...
I would keep track of changes on the server side, and send a small "same table" msg if the query comes in whereas no data has changed. Only send back the table data if something has changed on the server side.