General settings object, retrieving the datatable object....

General settings object, retrieving the datatable object....

ellipsisellipsis Posts: 23Questions: 0Answers: 0
edited August 2013 in Bug reports
Hello,

First of all, datatables absolutely rocks!

I'm using several tables and use a general settings object which I merge with table specific settings.

It all works great but I've som annoying redundant code, because I can't seem to retrieve the table object within the general settings object.

I tried :
[code]datatable_default_setting.oColVis = : { 'aiExclude': pfx_determineWhichColumnsToHideFromColVis(this.fnSettings().aoColumns),'buttonText':''}; [/code]

of course the 'this' reference is seen as the datatable_default_setting object.

Best regards,

René

Replies

  • allanallan Posts: 63,522Questions: 1Answers: 10,473 Site admin
    > the table object within the general settings object.

    Where the table object is the `` node and the general settings object is the return from fnSettings ? You can use the `nTable` property of the settings object.

    DataTables 1.10's API will have a much more obvious `table().node()` method...!

    Allan
  • ellipsisellipsis Posts: 23Questions: 0Answers: 0
    Thank you for the quick reply!

    Iĺl give a more exact example:

    [code]

    var ob_settings = {
    'sDom': main_object.datatables_default_sdom,
    'oLanguage': {'sUrl': main_object.datatables_dutch_url},
    'sScrollY': '1',
    'bProcessing': true,
    'bServerSide': false,
    'oColVis' : { 'aiExclude': fnc_determineWhichColumnsToHideFromColVis(this.fnSettings().aoColumns),'buttonText':''}

    };

    function create_table_1(){
    var ob_custom_settings = { 'aoColumns' : ..... };
    jQuery('#table_1').dataTable(jQuery.extend(ob_settings, ob_custom_settings));
    }

    function create_table_2(){
    var ob_custom_settings = { 'aoColumns' : ..... };
    jQuery('#table_2').dataTable(jQuery.extend(ob_settings, ob_custom_settings));
    }

    function create_table_3(){
    var ob_custom_settings = { 'aoColumns' : ..... };
    jQuery('#table_3').dataTable(jQuery.extend(ob_settings, ob_custom_settings));
    }

    function create_table_4(){
    var ob_custom_settings = { 'aoColumns' : ..... };
    jQuery('#table_4').dataTable(jQuery.extend(ob_settings, ob_custom_settings));
    }
    [/code]

    As you can see they all share the same settings object (ob_settings) (what I would like to set as default for the dataTable object but didn't succeed yet)

    The colvis aiExclude is the example I'm using here, needs the datatable object to retrieve its settings (in this case the aocolumns)

    How do I make this work?
  • ellipsisellipsis Posts: 23Questions: 0Answers: 0
    no body ?
  • allanallan Posts: 63,522Questions: 1Answers: 10,473 Site admin
    You can set defaults like this: http://datatables.net/release-datatables/examples/advanced_init/defaults.html . Much easier!

    Allan
  • ellipsisellipsis Posts: 23Questions: 0Answers: 0
    edited September 2013
    Hello Allan,

    I tried, but I still have the same issue then.
    And setting the default the way it is intended is certainly the best way, but the TableTools swf custom location setting does not seem to get across.

    The question still remains:
    How do I get (for example) the aoColumns object inside a function in the default settings....
  • ellipsisellipsis Posts: 23Questions: 0Answers: 0
    The only way I see is when fnInitComple is called, because then I got the oSettings variable (thus nTable)
  • allanallan Posts: 63,522Questions: 1Answers: 10,473 Site admin
    TableTools has its own defaults - use `TableTools.DEFAULTS.sSwfPath` .

    Beyond that, you might want to do a deep copy in jQuery extend.

    > How do I get (for example) the aoColumns object inside a function in the default settings....

    Generally speaking you don't - what are you trying to do? Perhaps I can suggest another way.

    Allan
  • ellipsisellipsis Posts: 23Questions: 0Answers: 0
    Hello Allan,

    Thank you for the reply, sorry for my late response.

    I would like to be able to retrieve (for example) the aocolumns object inside a new defined default settings.
    Because the columns have a sClass value that determines if the columns should be visible in ColVis for example. The functio I use returnes the array of columns to exclude from colvis.

    [code]'oColVis' : { 'aiExclude': fnc_determineWhichColumnsToHideFromColVis(aoColumns) } [/code]

    now I write custom settings for each table with the above repeated code...
  • allanallan Posts: 63,522Questions: 1Answers: 10,473 Site admin
    Are you feeling a bit brave? :-)

    DataTables 1.10 has the ability to give you column index information based on a jQuery selector. For example:

    [code]
    // gives an array of the column indexes which are hidden
    $('#example').DataTable().columns(':not(:visible):jq').index();

    // gives an array of the column indexes which are not hidden
    $('#example').DataTable().columns(':visible:jq').index();

    // gives an array of the column indexes based on a class
    $('#example').DataTable().columns('.myClass:jq').index();
    [/code]

    The `:jq` postfix tells the column selector to treat the first part as a jQuery selector on the header cells.

    DataTables 1.10, which is currently pre-beta, can be obtained here: https://github.com/DataTables/DataTables/tree/1_10_wip/media/js

    Allan
  • ellipsisellipsis Posts: 23Questions: 0Answers: 0
    edited September 2013
    wow...

    This would be the solution!!

    I'm not so brave at the moment, the code will be used a lot and should be reliable.
    Can't wait on the release!!

    Thank you so much!!!!

    Still a question though:

    when setting this as default for each datatable:
    [code]
    'oColVis' : { 'aiExclude': jQuery(this).DataTable().columns('.myClass:jq').index();}
    [/code]

    I must asume it will not work, the THIS is the default settings object....
    Any Suggestions
  • allanallan Posts: 63,522Questions: 1Answers: 10,473 Site admin
    It would work if `this` was the `table` element that you want to work with. If it is a DataTables settings object you could use `this.nTable` .

    Allan
  • ellipsisellipsis Posts: 23Questions: 0Answers: 0
    I'll try some more and let you know. :)
This discussion has been closed.