stateLoadParams event issues
stateLoadParams event issues
Hi,
I am having issues with my search fields and the savestate option.
The following is not producing the intended results:
stateLoadParams: function (settings, data) {
data.search.search = '';
}
Is not clearing my search results, so it loads with select options loaded (unselected).
stateLoadParams: function(settings, data) {
for (i = 0; i < data.columns["length"]; i++) {
var col_search_val = data.columns[i].search.search;
if (col_search_val != "") {
$("select", $("#example foot th")[i]).val(col_search_val);
}
}
}
Is not setting the selecting the select options.
Test case: https://live.datatables.net/fekehoxi/1/edit
Any help would be appreciated.
Thanks and regards, Stuart
This question has an accepted answers - jump to answer
Answers
Try https://live.datatables.net/fekehoxi/3/edit?html,js,output, added missing semicolon
The
data.search.search = '';
applies only to the default global search which your test case isn't displaying. So it wouldn't affect the column searches if that is what you are expecting.Take a look at the search values. If one option is selected the value looks like this:
If multiple are selected the value looks like this:
Updated the test case to show the values:
https://live.datatables.net/potujuda/1/edit
You will need to parse the search value to reverse what is used in the
column().search()
call, for example:^(
and trailing)$
.$.fn.dataTable.util.escapeRegex()
.|
.Then use something like the suggestions in this SO thread to set the multiple selection values.
Kevin
Hi, thank you for your reply. However, I am still not able to get the selection working.
I tried hard coding the values to eliminate the parsing. For example, creating the select controls as:
<select id="x1" multiple="multiple"><option value=""></option></select>
When selecting:
$("#x1 option[value='" + "Airi Satou" + "']").prop("selected", true);
This working in the $(document).ready() function, but not in stateLoadParams event.
Please see: https://live.datatables.net/potujuda/2/
Thanks and regards, Stuart
Right. Its due to
initComplete
executing afterstateLoadParams
and building the select inputs afterstateLoadParams
. Instead of usingstateLoadParams
usestate.loaded()
ininitComplete
to set the selected options after the inputs are created.Also note that the same
id
is assigned to each of the inputs. Eachid
must be unique on the page. Maybe incorporate the use of the column index in theid
.Kevin
Thank you!!!
Added to the end of init complete:
var state = this.state.loaded();
var cols = state.columns;
for (i = 0; i < cols.length; i++) {
var col_search_val = state.columns[i].search.search;
if (col_search_val != "") {
//set controls
}
}