Search code examples
javascriptjquerydatatables

Jquery datatable one time callback on initialize


I've been using Jquery's datatable plugin for a while and came across an issue I am not sure how to resolve in a clean manner. I want to have a function callback similar to fnCallback() which only executes when the datatable is first initalized and the first page of data has been loaded in.

I have a situation where I want to wait until my datatable has been constructed to be able to customize some of the elements (add some custom classes to elements dynamically created by the plugin).

I could do something like this:

var isInitialized = false;
$("#completed-action-items-table").dataTable(
     "fnDrawCallback": function () {
         if (!isInitialized) {
            //do logic
            isInitialized = true;
         }
     });

But this seems ugly.

Any suggestions would be great, thanks!


Solution

  • I think yours may be the best technique, short of writing CSS selectors that target the elements you're interested in without adding extra classes.