to$() not returning correct JQuery object

to$() not returning correct JQuery object

EricBuistEricBuist Posts: 5Questions: 1Answers: 0

Hi,

I need to store some table-wide information in some of my DataTable instances. For this, I tried to use JQuery data object. However, if I have the DataTable API instance, I cannot access the data; I need to keep track of the JQuery object or table ID.

var table_id = 'my_table_id'
var tableNode = $('#' + table_id)
var tableApi = tableNode.DataTable()
var tableNode2 = tableApi.to$()
var id2 = tableNode2.attr('id')

I would expect id2 == table_id, but instead, id2 is undefined.

That means from within a render function, if some table global information is needed (e.g., URL patterns to generate hyperlinks), it would not be sufficient to retrieve the table api from "new $.fn.dataTable(meta.settings)"; something else would be needed to access the JQuery data attributes. Unfortunately, the $.fn.dataTable construct fails for me and the only workaround is to use private settings.nTable field.

I could work around by putting the table id in one of the rows, but it would be better that tableApi.to$() == tableNode.
I know my life would be simpler if I used global variables but I have some pages with multiple tables so I cannot just use a single global state.

I am using DataTables 1.10.7 with JQuery 1.11.2. Thanks for any help.

Replies

  • allanallan Posts: 63,686Questions: 1Answers: 10,500 Site admin

    tableApi.to$()

    This won't work. As the to$() documentation notes:

    This method will create a jQuery object from the contents of the API instance's result set. This of primary use when working with the nodes() functions of the API

    To get a jQuery object for the table, just use $('#' + table_id) - or if you need use table().node() and make it into a jQuery object ($( table.table().node() )).

    Allan

This discussion has been closed.