1

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?

1 Answer 1

1

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/

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.