Out-of-box State Saving not working
Out-of-box State Saving not working
Justindfunk
Posts: 5Questions: 0Answers: 0
I'm having issues with basic state saving in 1.9. I have two branches of my code, the only difference between them being the upgrade from 1.8 to 1.9. I have drop down list filters for each of my columns. In the 1.8 version their selections are preserved during page refresh. In the 1.9 version they are not. I'm using server side processing with ajax, which as far as I'm aware shouldn't affect the state save at all, but other than that I'm not doing anything fancy.
This discussion has been closed.
Replies
The drop down filters are outside of DataTables core (they must be, since the core doesn't provide that functionality) thus if you want to use the state saved parameters, that must be explicitly coded for (there much have been something doing that before with 1.8 as well so it might be that you just need a parameter renamed or something).
Allan
You're right, forgot that the drop down stuff came from here http://datatables.net/release-datatables/examples/api/multi_filter_select.html. Here's the code that I had to add to reselect the filter on refresh:
[code]
"fnDrawCallback": function() {
var oSettings = oTable.fnSettings();
var columns = $('#lines tfoot th');
columns.each(function(index){
search_string = oSettings.aoPreSearchCols[index].sSearch;
if (search_string !== ""){
$(this).find('select').val(search_string);
}
}
[/code]
When I debug the javascript it works when the datatable filter fires after selecting the drop down value; sSearch has the expected value. However, if I refresh the page, it is empty.
Allan
[code]
var oData = oSettings.fnStateLoad.call( oSettings.oInstance, oSettings );
[/code]
However I just tried to add that line under my var oSettings line shown in my original code, and then change the search_string to populate from oData.aoSearchCols, but oData winds up null.
Looking at your code again I actually don't fully understand why it doesn't work! If you console.log( oSettings.aoPreSearchCols[index] ) inside the loop - what does that give?
Allan
Page first loaded:
[code]
Object
bCaseInsensitive: true
bRegex: false
bSmart: true
sSearch: ""
__proto__: Object
[/code]
Drop down choice "03/23/2012" selected, data table processes and returns:
[code]
Object
bCaseInsensitive: true
bRegex: false
bSmart: true
sSearch: "03/23/2012"
__proto__: Object
[/code]
Refresh the page:
[code]
bCaseInsensitive: true
bRegex: false
bSmart: true
sSearch: ""
__proto__: Object
[/code]
So, then I moved the code up into fnStateLoaded, and changed it to pull from oData.aoSearchCols instead, and it's not even firing that chunk of code. Here's my entire table declaration:
[code]
var oTable = $('#lines').dataTable({
"aoColumnDefs": [ { "bSortable": false, "aTargets": [0] }
,{ "bVisible": false, "aTargets": [ 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33] }
,{ "fnRender": function (oObj) { if (oObj.aData[3][0] == 'H') { return '' + oObj.aData[3] + '' } else { return oObj.aData[3]; } }, "aTargets": [3] }
],
"aaSorting":[],
"bDeferRender": true,
"sDom": '<"H"lr>t<"F"ip>',
"aLengthMenu": [1, 10, 25, 50, 100],
"iDisplayLength": 10,
/* "bScrollInfinite": true,
"bScrollCollapse": true,
"sScrollY": "400px",*/
"bProcessing": true,
"bServerSide": true,
"sAjaxSource": "/ajax/so_based_schedule",
"bJQueryUI": true,
"bAutoWidth": false,
"bStateSave": true,
"fnStateLoaded": function(oSettings, oData) {
var columns = $('#lines tfoot th');
columns.each(function(index){
search_string = oData.aoSearchCols[index].sSearch;
console.log(oData.aoSearchCols[index])
if (search_string !== ""){
$(this).find('select').val(search_string);
}
});
}
});
[/code]
I get no log to console, and I put a breakpoint on the var columns = line and it never hits it
and just for sanity check, the top of the jquery.dataTables.js resource
[code]
* @summary DataTables
* @description Paginate, search and sort HTML tables
* @version 1.9.0
* @file jquery.dataTables.js
* @author Allan Jardine (www.sprymedia.co.uk)
* @contact www.sprymedia.co.uk/contact
[/code]
I've been trying to think if it could be something else, but like I said in my original post, two branches of the exact same code with the only difference being datatables 1.8 in one, 1.9 in the other, and the values persist through page refresh in 1.8 but not in 1.9.
Allan