Why is the API context element an array? When would there ever be more than 1 element?

Why is the API context element an array? When would there ever be more than 1 element?

jLinuxjLinux Posts: 981Questions: 73Answers: 75

This is really just out of curiosity, but when I initiate a table and poke around the DataTables object, I find the context element:

var dt = new $.fn.dataTable.Api( '#example' )
dt.context // Array 
dt.context[0] // Object
dt.context[0].sTableId // "example"

My question is: is there a reason that dt.context doesn't go right to the object currently stored at dt.context[0]? I'm assuming theres some logical reason that context is an array, I just don't see at the moment and thought id ask.

Thanks!
-J

P.S. HI ALLAN! Long time no talk. Hope all is well.

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,455Questions: 1Answers: 10,465 Site admin

    Hi - good to hear from you again. Yup, all going well here thanks - hope likewise with yourself!

    The DataTables API is capaible of operating on multiple tables at a time, which is why the context (i.e. basically the settings object for the tables) can be an array.

    e.g. $('.dataTable').DataTable().data() will get the data from all DataTables on the page, not just a single one.

    Sometimes its useful, sometimes not! It felt right to generalised it to allow for multiple tables though.

    Allan

  • jLinuxjLinux Posts: 981Questions: 73Answers: 75
    edited July 2018

    All is well. Im noticing that I have a ton of alerts on here and in my DataTables related plugins at Github, so spending some time trying to take care of those.... And I do miss working with DataTables :-D lol

    How does that work if the two contexts are nested under the var dt = new $.fn.dataTable.Api( '#example' ) part? One would think that the variable dt now only points to the #example table.

    If you have time, do you think you can provide an example where it could be leveraged appropriately? (Not if itll take too much of your time. I know you're a busy guy)

  • jLinuxjLinux Posts: 981Questions: 73Answers: 75
    edited July 2018

    P.S. I thought of another plugin, similar to my DT Keep Conditions plugin - where the developer can basically code table conditions that will be indexed by strings for shortcuts. So you could link someone to #Table1=goto:TopUsers for example, and it would automatically implement the table conditions (search, sorting, etc) that would show the top users. Whatcha think?

  • allanallan Posts: 63,455Questions: 1Answers: 10,465 Site admin
    Answer ✓

    One would think that the variable dt now only points to the #example table.

    Correct. With an ID selector it will result ins a single context only. If you use some other selector such as class selector, it can pick up multiple tables: var dts = new $.fn.dataTable.Api( '.dataTable' ); for example.

    You could use that to filter all tables on the page: dts.search('Hello').draw(). Example: http://live.datatables.net/kurohaqe/1/edit .

    Your plug-in idea sounds like an interesting one. Deep linking can often be useful.

    Allan

  • jLinuxjLinux Posts: 981Questions: 73Answers: 75

    Got it. Thanks for clarifying!

This discussion has been closed.