Show the selected option of a select ( select filter ) after refreshing

Show the selected option of a select ( select filter ) after refreshing

elmk93elmk93 Posts: 1Questions: 1Answers: 0

I'm using a select that filters a table.
Let's say I'm filtering cars based on their brands. when I choose for example "Mercedes" it filters the list which is good, and when I refresh it saves the state which is even better ( it shows only Mercedes' cars ).
The only issue is that when I refresh the chosen option displayed is "Select an option" even if the list is filtered for Mercedes.
I would like to know if it's possible update the displayed chosen option as well !

Here is the piece of code I used

"bStateSave": true,
        "fnStateSave": function (oSettings, oData) {
            localStorage.setItem('offersDataTables', JSON.stringify(oData));
        },
        "fnStateLoad": function (oSettings) {
            return JSON.parse(localStorage.getItem('offersDataTables'));
        }

Answers

  • bindridbindrid Posts: 730Questions: 0Answers: 119

    You can use the initComplete callback to get the value from local storage and use it to set the selected value.

  • allanallan Posts: 63,889Questions: 1Answers: 10,530 Site admin

    This is an example I wrote a while back for this sort of thing: http://live.datatables.net/lunopeba/1/edit . It uses the filters in the header and FixedHeader, but the key is that the value is selected when the filtering element is first created - this could be done in initComplete as @bindrid says (in fact it should be done there if you are using Ajax loaded data).

          // Restore state saved values
          var state = this.state.loaded();
          if ( state ) {
            var val = state.columns[ this.index() ];
            select.val( val.search.search );
          }
    

    Allan

This discussion has been closed.