persistent variables
persistent variables
Greetings,
I'm using DataTables in a fairly complex project, and was wondering if anyone has come across a need to maintain stateful data associated with a datatable instance. The site itself never leaves the first page loaded, instead other page elements are dynamically loaded in using jQuery's load(...) method. The specific problem I am trying to tackle right now is how to maintain a "selection model" associated with a table to keep track of selected items. The only solution I have found so far is to create a global variable (which I gather is the same as assigning the variable to the window object) and then accessing that later. Is there a way for me to assign a custom property to a datatable object? Ideally, I would be able to do something like this:
[code]
$(document).ready(function() {
var table = $('#my_table').dataTable( options );
table.selection_model = new Array();
});
[/code]
and from some other page:
[code]
var table = $('#my_table').dataTable();
console.log(table.selection_model);
[/code]
this doesn't seem to work currently, I imagine because of some scope confusion.
I'm using DataTables in a fairly complex project, and was wondering if anyone has come across a need to maintain stateful data associated with a datatable instance. The site itself never leaves the first page loaded, instead other page elements are dynamically loaded in using jQuery's load(...) method. The specific problem I am trying to tackle right now is how to maintain a "selection model" associated with a table to keep track of selected items. The only solution I have found so far is to create a global variable (which I gather is the same as assigning the variable to the window object) and then accessing that later. Is there a way for me to assign a custom property to a datatable object? Ideally, I would be able to do something like this:
[code]
$(document).ready(function() {
var table = $('#my_table').dataTable( options );
table.selection_model = new Array();
});
[/code]
and from some other page:
[code]
var table = $('#my_table').dataTable();
console.log(table.selection_model);
[/code]
this doesn't seem to work currently, I imagine because of some scope confusion.
This discussion has been closed.
Replies
i would imagine you could extend DT the same way you would with plug-ins and add your storage variable.
http://www.datatables.net/blog/Creating_feature_plug-ins
What you can do is attach to the DataTables settings object, which is persistent. For example, modifying your example above:
[code]
$(document).ready(function() {
var table = $('#my_table').dataTable( options );
table.fnSettings().selection_model = new Array();
});
[/code]
[code]
var table = $('#my_table').dataTable();
console.log(table.fnSettings().selection_model);
[/code]
Like fbas suggests, if you are going to be using this a lot, I'd suggest creating a plug-in API method which will wrap this up into a getter / setter function to make things a little cleaner.
Allan