Restoring values using localStorage object
Restoring values using localStorage object
I am using an external multi-select filter on some grids and for my purposes after leaving the page and coming back, I know that the filter values I used still exist but how to get them to populate the select again. So this is what I did and it could be used for anything.
The localStorage addition really helped a lot, thanks!
Below you will see the fnStateLoad and I added in the dataFN and other variable below it. if you reload and it is null it does nothing. I used it to find the column, this being 2, and grab the data to set the select box in a later function in the fnDrawCallback. I can elaborate more but basically if you are using localStorage, this allows you to parse the object and put back items that are not a part of the datatables object.
[code]
"bStateSave": true,
"fnStateSave": function (oSettings, oData) {
localStorage.setItem( 'DataTables_'+window.location.pathname, JSON.stringify(oData) );
},
"fnStateLoad": function (oSettings) {
//look for the saved info for the Filter boxes.
//if find match, then save it.
dataFN = JSON.parse( localStorage.getItem('DataTables_'+window.location.pathname));
if (dataFN != null) {
yourvariablename_sSearch = dataFN.aoSearchCols[2].sSearch;
}
return JSON.parse( localStorage.getItem('DataTables_'+window.location.pathname) );
},
[/code]
I can show all the code I did but this is the core of it. Hope it helps.
Enjoy.
The localStorage addition really helped a lot, thanks!
Below you will see the fnStateLoad and I added in the dataFN and other variable below it. if you reload and it is null it does nothing. I used it to find the column, this being 2, and grab the data to set the select box in a later function in the fnDrawCallback. I can elaborate more but basically if you are using localStorage, this allows you to parse the object and put back items that are not a part of the datatables object.
[code]
"bStateSave": true,
"fnStateSave": function (oSettings, oData) {
localStorage.setItem( 'DataTables_'+window.location.pathname, JSON.stringify(oData) );
},
"fnStateLoad": function (oSettings) {
//look for the saved info for the Filter boxes.
//if find match, then save it.
dataFN = JSON.parse( localStorage.getItem('DataTables_'+window.location.pathname));
if (dataFN != null) {
yourvariablename_sSearch = dataFN.aoSearchCols[2].sSearch;
}
return JSON.parse( localStorage.getItem('DataTables_'+window.location.pathname) );
},
[/code]
I can show all the code I did but this is the core of it. Hope it helps.
Enjoy.
This discussion has been closed.
Replies
Allan
The elements are all created on the page separate from the table. As I noted on there, I save the data, then after that, I seem to have forgotten to mention that I use it in the fndrawcallback to use that data in a function that simply sets the items in the multi-select to "selected". If I selected more than one item, it splits it by the "|" and such. There are some other things I did also. it was my way of making it work. :o)