Phil Phil - 1 year ago 100
jQuery Question

DataTables 1.10.10 - Add function to call everytime

I am using DataTables version 1.10.10. I know I can add a function into the fnDrawCallback when I init datatables on a specific table, however I have a LOT of pages with table calls and would need to update each and every one of them.

I am wondering where in the DataTables code I can place a new function that will run automatically on the DrawCallback function before any additional functions that are added during the initialization stage.

Can anyone point me in the right spot?

Answer Source

I see two options here. Option A is much easier to implement, but will require you to call your function in every DataTables initialization. Option B requires much less code (and is probably closer to what you really want to do), but is going to be potentially very difficult and could have unintended consequences.

Option A

You could create a .js script file that you include in all your pages (like how you presumably already include the DataTables .js files) that defines your custom function. Then all you'd have to do is put that function call at the top of each fnDrawCallback option. As you mentioned:

I have a LOT of pages with table calls and would need to update each and every one of them.

This doesn't really alleviate that problem (and it's probable you already considered this, so sorry if it's not helpful), but while it may involve a little more grunt work up front, you avoid some of the challenges and risks of option B. It does mean you won't have to re-define your custom function in each page, but you will still have to call it. If you really don't want to do that look at Option B.

Option B

You could go into the actual jquery.dataTables.js file and try to override the fnDrawCallback function's default behavior to call your function. The advantage of this option is that you won't have to mess with your custom function more than once. The disadvantage is that if you ever don't want to use your custom version, you'll have to manually override it in the initialization.

Fair warning, I've never actually tried this, so beyond pointing you in the right direction I can't provide much help.

I have version 1.10.12 of DT, but not much has changed, so hopefully this information is relevant. In my version of jquery.DataTables.js the fnDrawCallback function is on line 10614 (you can, of course, just do a find on the file to get the exact line for your version). There you can redefine the default behavior of fnDrawCallback. (You can get some examples of how to do this from the other functions defined in the same section, or the comments on each function).


You can, of course, override pretty much everything in DataTables from this script file, but this is not going to be well-supported or well-documented, so if you choose option B (which seems more aligned with what you want to do) tread lightly.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download