有没有办法找到页面中的表是dataTable对象?
我正在寻找一种方法来了解页面中的表是否是 dataTable?有没有简单的方法可以找到它?或者如果我可以获得 dataTable 的所有对象。
I am looking for a way to get to know if the tables in a page are dataTable or not? Is there an easy way to find it? Or may be if I can get all the objects of dataTable.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
DataTables 插件中有一个静态方法,因此您可以验证为:
There's a static method in the DataTables plugin, so you may verify as:
可以一次性获取页面上所有属于DataTable的表。 DataTables 1.10 的代码为:
这是 doc 为了它。这将返回一个 DOM 元素数组。如果您想获取与其关联的 DataTable API 实例,您可以执行以下操作:
请注意,尽管像上面那样调用
.DataTable()
可以创建一个新的 DataTable API 实例并因此初始化您的表,如果调用该方法的 DOM 元素已经拥有自己的实例(API 已经创建并初始化),则该调用将不会< /em> 创建新实例。换句话说,上面的代码不会重新初始化您的表。这里的
datatables
对象是一个API实例,它控制整个表集合。在此实例上调用 API 函数将同时控制所有表。改编自文档中的示例,此代码:将使所有表格跳转到下一页(如果存在)。
在 1.10 之前的版本中,等效函数是
$.fn.dataTable.fnTables
。Roy Ling 提到
$.fn.dataTable.fnIsDataTable
可用于单独测试表。当然,在1.10中,仍然可以在需要时单独测试表,函数是$.fn.dataTable.isDataTable
。It is possible to get all the tables on the page that are DataTables in one fell swoop with. The code for DataTables 1.10 would be:
Here's the doc for it. This will return an array of DOM elements. If you want to get the DataTable API instances that are associated with them, you can do:
Note that although calling
.DataTable()
like above can create a new DataTable API instance and consequently initialize your table, if the DOM element(s) for which the method is called already have their own instances (the API has already been created and initialized), the call will not create new instances. In other words, the code above will not initialize your tables anew.The
datatables
object here is an API instance that controls the whole collection of tables. Calling the API functions on this instance will control all the tables at once. Adapting an example from the documentation, this code:would make all the tables jump to their next page (if it exists).
In versions prior to 1.10, the equivalent function was
$.fn.dataTable.fnTables
.Roy Ling mentioned
$.fn.dataTable.fnIsDataTable
that can be used to test tables individually. Of course, in 1.10 it is still possible to test tables individually when needed, the function is$.fn.dataTable.isDataTable
.这有点像黑客,但还没有一种本地方法来检查数据表实例。这是我昨天碰巧学到的东西。
It's a bit of a hack but there isn't yet a native way to check for a datatable instance. This is something I just so happen to have learned yesterday.
我知道这个问题发布可能已经有一段时间了,但是当我自己问同样的问题时,我从 DataTable 参考中找到了这个解决方案 网站。
以下是如何检查 #example 是否是 DataTable 的方法。如果没有,请初始化:
希望这对某人有帮助!
I know it may be a while since this question was posted, but as I was asking the same question myself, I came to this solution from the DataTable reference site.
Here's how to check if #example is a DataTable or not. If not, initialise:
Hope this helps someone!