Dynamic option name upon table initialization

Dynamic option name upon table initialization

ludalexludalex Posts: 4Questions: 0Answers: 0
edited February 2014 in DataTables 1.9
Hello everyone,

I'm trying to write a function that loads the table with different sources (ajax/javascript array) dynamically.
Instead of writing the code to initialize the table twice in a conditional statment, I'd like to have the "source" option of the DataTable to come from a variable, as shown in this example:

[code]
if (navigator.onLine)
source_ = 'sAjaxSource'
else
source_ = 'aaData'

oTable = $('#table').dataTable({

"sDom": 'Tfrt',
source_: data

})
[/code]

It doesn't work as expected tough, any suggestions?

I know 'data' variable is not defined and the actual data source is not my problem as if I put a string like "aaData" instead of 'source_' (the option name) it works.

Thanks in advance

Replies

  • ludalexludalex Posts: 4Questions: 0Answers: 0
    Any ideas?
  • allanallan Posts: 63,498Questions: 1Answers: 10,470 Site admin
    Sounds like you want to execute a function that will make the decision, return an object and you can extend that:

    [code]
    function src () {
    var o;

    if ( ... ) {
    o.aaData = [];
    }
    else {
    o.sAjaxSource = ...;
    }
    }

    oTable = $('#table').dataTable( $.extend( true, src(), {
    sDom: ...
    } );
    [/code]

    Allan
  • ludalexludalex Posts: 4Questions: 0Answers: 0
    @allan Brilliant, it works. Just need to declare "o" as object literal (o = {}) and return it at the end of src() ;)
This discussion has been closed.