Datatables custom properties - How do.

Datatables custom properties - How do.

Tester2017Tester2017 Posts: 145Questions: 23Answers: 17

I would like to extend DataTable with some custom properties.

I can do this after initializing a table and simple add my properties like this:

table.customProperties = [];
table.customProperties.propertyA = null;
table.customProperties.propertyB = false;

etc...

But I would like to have this custom properties already available at the moment of defining defaults for tables with

$.extend(true, $.fn.dataTable.defaults

As far as I can see and understand I can only realize this if I make my own extension "CustomProperties".

Is this true, or do I have other options.

This question has accepted answers - jump to:

Answers

  • allanallan Posts: 61,690Questions: 1Answers: 10,101 Site admin
    Answer ✓

    Hi,

    Its important to be aware of the separation of internal properties and the external API in DataTables:

    1. The settings object (settings()) is unique per table and it contains the configuration parameters and data for each table.
    2. The API instance (probably what your table is in your above code) does not need to be unique per table - multiple instances can point at the same table. Indeed, a single instance can also point at multiple tables.

    The public API provides access to manipulate the table, including modifying the settings object as needed. The settings object is considered to be private and its properties can, will and do change between versions, so if you do use it, you need to be really careful with it.

    That said, writing values to the settings object in a unique name is useful for per table information, and this is the approach the extensions take.

    Allan

  • Tester2017Tester2017 Posts: 145Questions: 23Answers: 17

    Hi @allan,

    Thanks for your answer, advice and warnings. Yes I will take care.

    The only objective I have is to reduce the number of global vars I have. A lot of them are very coupled to my tables. And I thought if I transform them in custom properties of my tables I can access them in events and do not need them anymore as global vars.

  • allanallan Posts: 61,690Questions: 1Answers: 10,101 Site admin
    Answer ✓

    Perhaps $().data() on the table might be appropriate in that case?

    Allan

  • Tester2017Tester2017 Posts: 145Questions: 23Answers: 17

    @allan Thanks for your suggestion.

    It is also after the initializing of a table as in my first approach, but your solution is more elegant. So I adapted your suggestion.

This discussion has been closed.