Correctly re-creating the table with all its settings

Correctly re-creating the table with all its settings

vlad_stryapkovlad_stryapko Posts: 6Questions: 1Answers: 0

Hi,

I need to modify an existing option and, as far as I found out, the best way would be to re-initialise the table with the modified version of its previous settings. However, the question is, how can I reliably get the settings object belonging to my grid?

According to this, I can get it via settings, but it might return more than one object. How can I identify which one belongs to the grid?

In case of suggestions to initialise grid with the right setting in the first place - it's hardly possible since the initialisation code is used in various places and only one place requires this setting to differ. Making this generic initialisation code configurable is also pretty hard and time-consuming, so I'd like to leverage a quick-and-dirty workaround if possible.

Thanks

Answers

  • kthorngrenkthorngren Posts: 21,303Questions: 26Answers: 4,947

    but it might return more than one object

    Depends on how you are selecting the tables. If you are doing something like this example:
    https://datatables.net/examples/basic_init/multiple_tables.html

    Then settings for multiple tables will be returned. You could use a for loop like this to get the table names and to determine which array element contains the table of interest:

      var table = $('table.display').DataTable();
      var settings = table.settings();
      for (i = 0; i < settings.length; i++) {
        console.log(settings[i].sTableId);
      }
    

    If your selector is for only one table, $('#example').DataTable() for example, then the array will contain just one element.

    You can see the above example and experiment here:
    http://live.datatables.net/daqigico/1/edit

    Kevin

  • vlad_stryapkovlad_stryapko Posts: 6Questions: 1Answers: 0
    edited December 2017

    I've tried the options you provided in our code and it seems to create a table instead of retrieving an existing instance. Also, it doesn't seem to find any table with the class 'display'. Is this something that's automatically added by DTs when the table is initialised or should we add this class manually? If it's the former, then I'm afraid the way we work with DTs is a bit weird so the usual approach might not work.

  • bindridbindrid Posts: 730Questions: 0Answers: 119

    I have several tables that use the same settings so I simple create the settings as an object to be passed as needed. You can do the same, and modify the setting in question.

    here is an example http://jsbin.com/socitiy/edit?js,output

  • vlad_stryapkovlad_stryapko Posts: 6Questions: 1Answers: 0

    I've considered this option but, unfortunately, that's not easy to do in my case. I'm working with the legacy project and all the initialisation and table creation is wrapped by another language code so I can't simply create a settings object and use it later on. I will have to extend this wrapper to support it and also to deal with multiple table settings, since not all tables are supposed to reuse them.

  • kthorngrenkthorngren Posts: 21,303Questions: 26Answers: 4,947
    edited December 2017

    it seems to create a table instead of retrieving an existing instance.

    You need to choose the appropriate jQuery table selector based on your configuration. ``$('#example').DataTable()is a standard one used for the examples on this site. Thedisplay` is a Datatable class. They are described here:
    https://datatables.net/manual/styling/classes

    I guess you would use the same as you use to initialize your tables in Javascript.

    all the initialisation and table creation is wrapped by another language code

    Not sure how this code is structured but is it possible to have it return the config object in JSON? then you could do something like @bindrid option.

    Kevin

  • vlad_stryapkovlad_stryapko Posts: 6Questions: 1Answers: 0

    You need to choose the appropriate jQuery table selector based on your configuration. ``$('#example').DataTable()is a standard one used for the examples on this site.

    Do I understand it correctly that the id of the table element should be used? That's what I've tried and it didn't work. However, when I use the same selector in Chrome console, I can clearly see that the selected object contains a dataTable property, which contains a 'settings' array with two elements, and one of them resembles the configuration of the grid. So it looks like the selection is correct, but it doesn't work when I do it from the JS side.

    but is it possible to have it return the config object in JSON?

    Well, technically yes, but in practice I don't think so. It's server-side C# code which builds the HTML markup (including the table id I use for selection) and renders it. I'd really like to avoid altering it since it was written ages ago.

  • kthorngrenkthorngren Posts: 21,303Questions: 26Answers: 4,947

    I believe Datatables adds the class dataTable when it initializes the table. You can try that. I updated the example to use var DT = $('.dataTable').DataTable();:
    http://live.datatables.net/daqigico/2/edit

    DT will contain API's for all the tables on the page.

    Kevin

  • vlad_stryapkovlad_stryapko Posts: 6Questions: 1Answers: 0

    I believe Datatables adds the class dataTable when it initializes the table.

    In my case it doesn't seem to do so. Also, we're using 1.9 version, which doesn't support this API, as far as I know.

  • kthorngrenkthorngren Posts: 21,303Questions: 26Answers: 4,947

    You can try fnSettings. This conversion table may be of interest.
    https://datatables.net/upgrade/1.10-convert

    Kevin

  • vlad_stryapkovlad_stryapko Posts: 6Questions: 1Answers: 0

    Thanks, fnSettings worked pretty nice.

    It seems the issues with table initialisation were caused by the fact that I called dataTable() on the table that wasn't completely initialised yet. Moving the same code to fnInitComplete does the trick.

This discussion has been closed.