jQuery datatables:测试数据表插件是否已初始化
我想检查 id="datatable" 的表元素是否已数据表初始化。像这样的事情:
if ($('#datatable').dataTable().initialized) {
alert("initialized!");
}
else {
alert("not initialized!");
}
我怎样才能做到这一点? 谢谢!
I want to check if a table element with say, id="datatable" is datatables-initialized. Something like this:
if ($('#datatable').dataTable().initialized) {
alert("initialized!");
}
else {
alert("not initialized!");
}
How can I do that?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
您可以使用 jQuery datatable 中的 fnIsDataTable 函数,
您可以在 api 中找到更多信息
You can the fnIsDataTable function in jQuery datatable
You can find more information in api
首先,在初始化数据表时添加一个特殊的类名:
现在您可以通过类名来区分它们:
First, add a special class name when you're initializing datatables:
And now you can tell them apart by class name:
我觉得以下是对此问题的正确答案。
例如,
通过这种方式,您
if(jQuery().)
应该能够检查是否加载了任何库。I feel following is the right answer to this.
For example
By this way you
if(jQuery().<libname>)
should be able to check any library loaded or not.Datatable 有一个方法来检查元素是否已初始化为数据表 - $.fn.DataTable.fnIsDataTable
Datatable has a method to check if an element has been initialized as a datatable or not - $.fn.DataTable.fnIsDataTable
调用 .dataTable() 后,它会对表执行任何操作以使其可识别吗?即它是否添加了一个新类“初始化”或类似的东西?如果是这样,您可以像这样循环浏览这些项目:
如果这不是您的意思,请道歉。您的问题不清楚“dataTable()”实际上是做什么的。
After you've called .dataTable() does it do anything to the table that makes it identifiable? i.e. does it add a new class "initialised" or something like that? If so, you could just loop through the items like so:
Apologies if this isn't what you mean. It's not clear in your question what "dataTable()" actually does.
我已经使用
callback()
函数在我的场景中执行相同的操作。考虑将其作为另一种方法共享,稍后在代码中..
I have used
callback()
function to do the same in my scenario. Thought of sharing this as an alternate approachLater in code..
这对我有用。
This worked for me.