Getting data object from arbitrary table
Getting data object from arbitrary table
Alrighty... so, I have a page with multiple tables on it. I initialize them like this:
[code]
fcmc.oTableAlarm = $('#alarm_overview').dataTable(fcmc.initializer("alarms"));
fcmc.oTableNodes = $('#nodes_overview').dataTable(fcmc.initializer("nodes"));
fcmc.oTableStats = $('#stats_overview').dataTable(fcmc.initializer("stats"));
[/code]
(where the fcmc.initializer function returns an arguments object).
I have clickable elements in tables, which have custom classes ('.clickable') allowing me to bind an event to like this:
[code]
$('.container').on('click', '.clickable', function(e) {
// do stuff
});
[/code]
Some of the "do stuff" portion would be much easier if I could access the original data. However, I don't know how to ascertain that "the cached DT object for this particular table is (for example) fcmc.oTableNodes" in order to call fnGetData() on it.
Consequently, I do this, and it does work:
[code]
$('.container').on('click', '.clickable', function(e) {
var dataObj = $(this).closest('table').dataTable().fnGetData(this.parentNode);
// do something with dataObj.someColumn
});
[/code]
So... after that extremely long and example-heavy explanation, the simple question is this:
Am I unintentionally creating a new DT object with that last snippet? Ideally I would love to just be able to say:
[code]
var dataObj = fcmc.oTableNodes.fnGetData(this.parentNode);
[/code]
and use the cached object; however I can think of no way of being aware within the click event that I need fcmc.oTableNodes. This is because the event should be able to fire on any table with '.clickable' elements. I could click the element in another table and actually need fcmc.oTableAlarms for example.
Any advice? Or even just a sanity check that the way I'm currently getting the data object is considered a perfectly acceptable practice?
[code]
fcmc.oTableAlarm = $('#alarm_overview').dataTable(fcmc.initializer("alarms"));
fcmc.oTableNodes = $('#nodes_overview').dataTable(fcmc.initializer("nodes"));
fcmc.oTableStats = $('#stats_overview').dataTable(fcmc.initializer("stats"));
[/code]
(where the fcmc.initializer function returns an arguments object).
I have clickable elements in tables, which have custom classes ('.clickable') allowing me to bind an event to like this:
[code]
$('.container').on('click', '.clickable', function(e) {
// do stuff
});
[/code]
Some of the "do stuff" portion would be much easier if I could access the original data. However, I don't know how to ascertain that "the cached DT object for this particular table is (for example) fcmc.oTableNodes" in order to call fnGetData() on it.
Consequently, I do this, and it does work:
[code]
$('.container').on('click', '.clickable', function(e) {
var dataObj = $(this).closest('table').dataTable().fnGetData(this.parentNode);
// do something with dataObj.someColumn
});
[/code]
So... after that extremely long and example-heavy explanation, the simple question is this:
Am I unintentionally creating a new DT object with that last snippet? Ideally I would love to just be able to say:
[code]
var dataObj = fcmc.oTableNodes.fnGetData(this.parentNode);
[/code]
and use the cached object; however I can think of no way of being aware within the click event that I need fcmc.oTableNodes. This is because the event should be able to fire on any table with '.clickable' elements. I could click the element in another table and actually need fcmc.oTableAlarms for example.
Any advice? Or even just a sanity check that the way I'm currently getting the data object is considered a perfectly acceptable practice?
This discussion has been closed.
Replies
Good to 'see' you again.
> Am I unintentionally creating a new DT object with that last snippet?
Yes and no! :-) You are creating a new instance of the DataTables object, yes. But that's not a bad thing - you are not creating a new DataTable! DataTables holds information for each table in a single settings object (which it holds in its own internal cache, so you don't need to worry about that), and the instance references that settings object. So you could have multiple instances referencing the same table.
This could be likened to multiple jQuery objects referencing the same nodes:
[code]
var p = $('p');
var all_p = $('p');
[/code]
There `p !== all_p` - but there are different instances of similar (almost identical) 'pointers'.
Using a new instance as you are is good practice - at least there is nothing wrong with it (like with jQuery, if it can be cached into a variable, it should be, for speed and memory).
Allan