如何维护 Ajax 刷新页面上的复选框状态?
我的应用程序中有一个 html 表,显示系统中运行的各种作业的状态。 每个作业都有一个与之关联的状态,例如用于运行作业的漩涡 gif。 新作业旁边有一个复选框,允许用户选择并启动相关作业。
该表是一个 struts2 自动刷新 div (sx:div),它每隔几秒刷新一次以反映作业当前发生的情况。
问题是,当 div 刷新时,我会丢失复选框的状态。
有没有一种优雅的方式来维持它们的状态? 我可以选择使用标签中内置的 dojo 主题系统完成 ajax 刷新后调用一些 javascript,但我不确定实现它的最佳方法是什么。
I have a html table in my application that shows the state of various jobs running in the system. Each job has a state associated with it e.g a swirly gif for running jobs. New jobs have a checkbox next to them that allows the user to select and kick off the associated job.
The table is a struts2 auto refreshing div (sx:div), it refreshes every few seconds to reflect what is currently happening with the jobs.
The problem is that when the div refreshes I lose that state of the checkboxes.
Is there an elegant way of maintaining their state? I have the option of calling some javascript upon completion of the ajax refresh using the dojo topic system built into the tag but i'm not sure what is the best way to approach it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我对 struts 不太熟悉,所以请听听我的建议,了解它的价值。
我认为有两种方法可以解决这个问题。
第一个(可能也是最简单的)是将一个事件添加到复选框,该事件将选中状态存储在数组或对象 onchange 中。 然后,在 ajax 刷新的回调中,恢复这些状态。
第二种方法要求 ajax 刷新要么作为 post 执行,以便将复选框提交到服务器,要么有一个单独的 ajax 操作,在选中复选框时触发。 使用这些选项中的任何一个,ajax 刷新都可以在渲染时“知道”要渲染哪些复选框为选中状态。
如果您决定选择第一,那么 javascript 并不是很困难,特别是如果您碰巧使用了一个好的库(jquery 等)。
I'm not very familiar with struts so take me advice for what it's worth.
There are two ways to approach this that I see.
The first (and probably simplest) is to add an event to the checkboxes which stores the checked state in an array or object onchange. Then, on callback from the ajax refresh, restore those states.
The second approach would require that the ajax refresh either be executed as a post so that the checkboxes are submitted to the server, or having a separate ajax action which fires off when a checkbox is checked. With either of these options, the ajax refresh could "know" at render which checkboxes to render as checked.
If you decide to go with number one, the javascript is not very difficult, especially if you happen to be using a good library (jquery, etc.).