Restoring values using localStorage object

Restoring values using localStorage object

dbludblu Posts: 11Questions: 0Answers: 0
edited November 2012 in General
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.

Replies

  • allanallan Posts: 63,523Questions: 1Answers: 10,473 Site admin
    How are you creating / populating the 'select' elements? The fnStateLoad callback fires really early on in the DataTables initialisation - so if you are creating them any time after that, the best way is probably to just read your local storage option again and populate the select elements once they have been populated.

    Allan
  • dbludblu Posts: 11Questions: 0Answers: 0
    Hi 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)
This discussion has been closed.