Add Custom Behavior to All Datatables
Add Custom Behavior to All Datatables
We'd like to extend Datatables to include our own custom functionality without worrying about initializing it on every DataTable declaration. A couple minor examples include storing the sorting information (when it occurs) in our own object, performing custom behavior when the datatable is waiting on a request from the server, and even implementing the fnSetFilteringDelay globally without needing to call it for each datatable declaration.
Is there a general way of accomplishing this or do these functions need to generally call and append their native functions? Thanks again!
Is there a general way of accomplishing this or do these functions need to generally call and append their native functions? Thanks again!
This discussion has been closed.
Replies
The development area outlines ways to extend datatables in a "safe" manner, allowing myself to define new methods within the $.fn.dataTableExt object (and usually the $.fn.dataTableExt.oApi object) which I can later call when I define a datatable. However, I'd like to hook this functionality into each datatable without needing to explicitly call the method as we have dozens of datatables, and I'd hate to need to do something akin to this on every declaration:
$().dataTable()
.something()
.anotherMethod()
.moreBehaviors()
.evenMoreBehaviors()
Overall I'd like to know if there's a nice, general way of extending functionality without needing to do this. Thanks!
[code]
oTable = $('#example').dataTable().fnSettings(); // <-- calling an API function
oTable = $('#example').dataTable().find('tr'); // <-- calling a jquery function, chained onto datatables
[/code]
anything you code as a jquery plugin will be chainable to it. anything you code into the api can be tacked onto the end. if you want it to be a link in the chain for other functions to be tacked onto it, you just need to return jquery objects.
http://docs.jquery.com/Plugins/Authoring
all API functions get oSettings passed to it as the first argument.
if you're not using a formal extension API function, easily get the settings object and the oInstance variable:
[code]
$('#example').fnSettings().oInstance;
[/code]
or maybe you already kept a variable reference to oTable.
$().dataTable()
.something()
.anotherMethod()
would work.
Having said that you would need to use sDom to activate these features - but you could possibly share that between all tables. There isn't a way to have a plug-in feature invoked automatically (sorting can be through the type detection, but features can't). For that a little bit of custom code would be required in DataTables.
Allan
.something()
.anotherMethod()
...is exactly what I do *not* want, as it would mean maintaining these definitions over dozens of pages. It seems like jQuery Datatables cannot be extended like some of the popular jQuery widgets. Shame - thanks anyhow!
Allan
Is there a good, safe way of doing this (obviously outside of editing jQuery.dataTables.js itself, which breaks our ability to easily update)? Thanks again!