检测 DataTable 上的页面更改
使用 DataTable 我可以排序、列出、分页,但我想检测分页何时更改,我已经看到了 < a href="http://datatables.net/api" rel="noreferrer">API 但我唯一能做的就是更改页面,但没有检测到此更改。
With DataTable I can order, list, do pagination but I want to detect when the pagination changes, I've seen the API but the only one I can do is change the page but no detect this change.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
您可以使用 fnDrawCallback 或 fnInfoCallback 来检测更改,当单击 next 时,它们都会发生被解雇了。
但请注意,页面更改并不是触发这些回调的唯一来源。
对于 DataTables 1.10.0+
该选项现在为
drawCallback
You may use fnDrawCallback or fnInfoCallback to detect changes, when next is clicked both of them are fired.
But beware, page changes are not the only source that can fire those callbacks.
For DataTables 1.10.0+
The option is now
drawCallback
分页事件就是这样处理的,
官方网站上有记录,这里 http://www. datatables.net/examples/advanced_init/dt_events.html
只要表的页面长度发生更改,就会触发 length.dt 事件
http://datatables.net/reference/event/length
更多活动请点击这里
http://datatables.net/reference/event/
Paging events are handled in this way,
documented in the official website, here http://www.datatables.net/examples/advanced_init/dt_events.html
The length.dt event is fired whenever the table's page length is changed
http://datatables.net/reference/event/length
More events here
http://datatables.net/reference/event/
我使用以下方法让它工作:
I got it working using:
如果您的版本大于 1.8,您可以使用它来触发页面更改事件:
希望这有帮助!
更新:
一些评论指出使用 .live() 而不是 .on() 对他们有用。请注意,您应该同时尝试两种方法,看看哪一种最适合您的特定情况! (我相信这可能与您的 jQuery 版本有关,但如果您发现其他原因,请发表评论!)
IF you have a version greater than 1.8, you can use this to hit the page change events:
Hope this helps!
UPDATE:
Some comments have pointed out that using .live() instead of .on() worked for them. Be aware of that you should try both and see which one works best in your particular circumstance! (I believe this may have to do with your version on jQuery, but please comment if you find another reason!)
就我而言,“page.dt”事件并没有起到作用。
我使用“draw.dt”事件代替,它有效!,一些代码:
每当数据表页面通过搜索、排序或页面更改而更改时,都会触发“Draw.dt”事件。
/***** 附加信息 *****/
我们声明事件监听器的方式有一些不同。您可以将其分配给“文档”或“html 对象”。 “文档”侦听器将始终存在于页面中,而“html 对象”侦听器仅当对象在声明时存在于 DOM 中时才会存在。一些代码:
//文档事件监听器
//HTML对象事件监听器
In my case, the 'page.dt' event did not do the trick.
I used 'draw.dt' event instead, and it works!, some code:
'Draw.dt' event is fired everytime the datatable page change by searching, ordering or page changing.
/***** Aditional Info *****/
There are some diferences in the way we can declare the event listener. You can asign it to the 'document' or to a 'html object'. The 'document' listeners will always exist in the page and the 'html object' listener will exist only if the object exist in the DOM in the moment of the declaration. Some code:
//Document event listener
//HTML object event listener
这个运作良好
This working good
另一种方法是在分页链接上注册一个事件处理程序,如下所示:
当然,将“#dataTableID_”替换为表的 ID。我使用 JQuery 的 ON 方法,因为这是当前的最佳实践。
An alternative approach would be to register an event handler on the pagination link like so:
Replace "#dataTableID_" with the ID of your table, of course. And I'm using JQuery's ON method as that is the current best practice.
尝试使用 delegate 而不是 live,如下所示:
Try using delegate instead of live as here:
当我点击下一步时,这可以让我滚动到
This works form me to scroll to when I click next
如果在page.dt之后处理draw.dt事件,则可以在移动页面后准确检测。下班后,draw.dt必须解绑
If you handle the draw.dt event after page.dt, you can detect exactly after moving the page. After work, draw.dt must be unbind
我在 API 中没有找到任何内容,但您可以做的一件事是将事件处理程序附加到标准分页器并检测它是否已更改:
I've not found anything in the API, but one thing you could do is attach an event handler to the standard paginator and detect if it has changed: