使用 jQuery DataTables 插件,fnAddData() 是否将行添加到 html 表格的顶部或底部?
我已经对此进行了测试,我问这个问题的原因是它似乎为不同的表做了不同的事情。
我找不到任何关于这是否可配置的文档。我本以为它会将行添加到底部,但我有几个例子,我看到它有时在顶部甚至中间添加行。看起来很随机。
有什么想法吗?
I have tested this and the reason I asked the question is that it seems to be doing different things for different tables.
I couldn't find any documentation on if this is configurable or not. I would have thought it would add the rows to the bottom, but I have a few examples where I see it adding rows at the top or even in the middle sometimes. It seems quite random.
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我查看了源代码,fnAddData调用内部方法_fnAddData,该方法添加一行,如果成功,则返回其在表中的索引。 _fnAddData 在底部添加新行(新行的索引等于表中的行数)。
然而,fnAddData 有一个可选参数来重绘表格,同时考虑到启用的各种功能。该参数默认为 true,这可能是导致“随机”行为的原因。
I have looked at the source code and fnAddData calls the internal method _fnAddData, which adds one row and if successful, returns its index in the table. _fnAddData adds the new row at the bottom (the index of the new row equals the number of the rows in the table).
However, fnAddData has an optional parameter to redraw the table taking into account the various features which are enabled. This parameter defaults to true and this is what probably causes the 'random' behavior.
浏览 Datatable 的来源,如 kgiannakaki 所解释的。该方法将按照应有的方式将所有新数据行追加到表的末尾。
“随机”行为的原因是,每次添加新行时,除非另有说明,该方法都会对整个表执行“重绘”。当这样做时,表将通过多个功能运行,例如过滤/排序等……并完全重写到页面。本质上,我猜测您将随机的数据位添加到新行中,每当添加新行时,这可能会产生“随机”效果。
您可以通过在 fnAddData 调用中添加第二个参数(需要设置为 false)来轻松禁用表重绘。
如果这个问题仍然存在,我会为此问题提交一个错误,因为他们不是造成这种行为的其他原因。
Browsing through the source of Datatable, as explained by kgiannakaki. The method will append all new data rows to the end of the table as it should.
The reason for the "random" behavior, is as each time a new row is added the method will perform a "redraw" of the entire table unless otherwise specified. When this does so the table is run through multiple features such as filtering/sorting etc etc ... and completely rewritten to the page. Essentially I am going to guess you are adding random bits of data into new rows which may be creating the "random" effect, whenever a new row is added.
You can easily disable table redrawing by adding a second parameter in the fnAddData call which needs to be set to false.
If this issue still persists I would file a bug for this issue, as their is no other reason for this behavior.
要禁用排序,以便将新行附加到表的底部,请添加
到 DataTable 初始化(顶部)。
To disable sorting, so that a new row will be appended to the bottom of a table, add
to (the top of) your DataTable initialization.