Umut Akın
Umut Akın

Reputation: 45

Jquery Datatables extend settings and functionality

I'm not an expert in JQuery so this question may be applied to plugins other than DataTables. Suppose I want to declare an additional(custom) setting in the initialisation:

var dTable = $('#example').DataTable(
    ...,
    'myOption' : [{ 'text' : 'Foo' } , { 'text' : 'Bar' }, ...],
)

and if myOption is declared, i want to automatically call a function like this one(like a callback function):

function() {
    console.log(dTable.settings().myOption)
}

so basically, i want to extend a plugin but i do not understand how it can be done from the manuals or other examples. How can I achieve that?

Upvotes: 1

Views: 1644

Answers (1)

davidkonrad
davidkonrad

Reputation: 85528

Actually it is quite easy to achieve what you want. Hook into the init.dt event and with some closure magic you have a "plugin" :

//myOption "plugin"
(function() {
  var run = function(myOptionSettings) {
    console.log(myOptionSettings)  
  };
  $(document).on('init.dt', function (e, settings, json) {
    var myOption = settings.oInit.myOption || false;
    if (myOption) {
      run(myOption)
    }
  })
})(document);

In use :

var table = $('#example').DataTable({
  myOption : [{ 'text' : 'Foo' }, { 'text' : 'Bar' }]
}) 

demo -> http://jsfiddle.net/e3vyjta5/

Upvotes: 1

Related Questions