[英]Retrieve datatable object prototype
I have many datatables throughout my application (version 1.10.10). 我在整个应用程序(版本1.10.10)中都有许多数据表。 I invoke them with
我用
var myDt = $("#myId").DataTable({/*properties object*/});
I need access to the prototype of the object myDt
to add once, and for all in my app, all the custom functions I need for datatables. 我需要访问对象
myDt
的原型以添加一次,并且对于我的应用程序中的所有内容,我都需要数据表的所有自定义函数。 Using $().DataTable.prototype
didn't work, neither did I find any "fn" properties inside the datatables data structure. 使用
$().DataTable.prototype
不起作用,我也没有在datatables数据结构内找到任何“ fn”属性。 I didn't find anything on the net about this. 我没有在网上找到任何有关此的信息。
Assuming that the datatables plugin you're using uses a prototype, you should be able to find it in the source looking for .prototype
or Object.create
, the former ( prototype
) being the property on functions that refers to the object that will be assigned as the prototype when that function is called via new
, and the latter ( Object.create
) being a mechanism for creating an object with a specific prototype directly, without a constructor function and new
. 假设您使用的datatables插件使用原型,则应该可以在源代码中找到它来寻找
.prototype
或Object.create
,前者( prototype
)是函数的属性,该函数引用将要使用的对象。通过new
调用该函数时将其分配为原型,而后者( Object.create
)是一种直接使用特定原型创建对象的机制,而无需构造函数和new
。
In the worst case, on ES5+ browsers, you can get the prototype of an object using Object.getPrototypeOf
: 在最坏的情况下,在ES5 +浏览器上,您可以使用
Object.getPrototypeOf
获得对象的原型:
var proto = Object.getPrototypeOf(myDt);
...but that would require you to create a datatable before you could add the features, which seems clunky. ...但是这要求您先创建一个数据表,然后才能添加功能,这似乎很麻烦。
See API plug-in development for more information. 有关更多信息,请参见API插件开发 。
A new API method can be registered using the
$.fn.dataTable.Api.register()
static method.可以使用
$.fn.dataTable.Api.register()
静态方法注册新的API方法。
Example: 例:
$.fn.dataTable.Api.register( 'sum()', function () {
var sum = 0;
for ( var i=0, ien=this.length ; i<ien ; i++ ) {
sum += this[i];
}
return sum;
} );
Example calls: 示例调用:
table.column(2).data().sum();
This should work: 这应该工作:
$("#myId").dataTable()
If doesn't work you can use "data" to store the dataTable object on your own 如果不起作用,您可以使用“数据”自行存储dataTable对象
$("#myId").data("myDt", myDt)
and get it later with: 并在稍后获得它:
$("#myId").data("myDt")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.