StateSave being fired immediately after page load

StateSave being fired immediately after page load

WeaponX86WeaponX86 Posts: 40Questions: 0Answers: 0
edited March 2014 in DataTables 1.9
When using server side processing, a call to State Save fires twice during DT loading. Here is a screenshot.
http://i.imgur.com/lSOk28S.jpg

NOTE: I was unable to use the debug bookmark due to a mixed content warning in Firefox or IE 11.

Here is my DT definition:
[code]
var oDTSettings = {
//"aoColumns": aoColumns,
"aoColumnDefs": aoColumnDefs,
//"aaSorting": <?php echo $aaSorting; ?>,
"sScrollX": "100%", // Required
"sScrollY": "400px", // Setting to auto or 100% introduces column glitches
"sPaginationType": "full_numbers",
"sAjaxSource": '<?php echo MOD_URL.CLASS_NAME."/?event=filterAjax";?>',
"sDom": '<"dt-header"l<"dt-breadcrumb">f>t<"dt-footer"i<"dt-selected">rp<"dt-textsize">>',

"iDisplayLength": 100,
"aLengthMenu": [50,100,250,500],

"bAutoWidth": false,
"bStateSave": true,
"bDeferRender": true,
"bProcessing": true,
"bServerSide": true,
"oLanguage": {
"sSearch": "Search Within:",
"sEmptyTable": "No matching Work Orders found",
"sProcessing": "Loading..."
},

"fnCreatedRow": fnCreatedRow,
//"fnRowCallback": fnRowCallback,
"fnDrawCallback": fnDrawCallback,
"fnPreDrawCallback": fnPreDrawCallback,
"fnInitComplete": fnInitComplete,
"fnCreatedCell": fnCreatedCell,
"fnStateSave": fnStateSave,
"fnStateLoad": fnStateLoad
};

<?php if (!$grouped){ ?>
var oTable = jq('#work_orders').dataTable(oDTSettings);
//console.log(oTable.fnSettings());

<?php if ($freeze_icons_column){ ?>
var oFixedOptions = {
"fnRowCallback": '',
"fnDrawCallback": fnPluginDrawCallback,
"iLeftColumns": 0,
"sHeightMatch": "none"
}

// If the last column is icons, apply the fixed column plugin
if(window.aoColumnDefs[window.aoColumnDefs.length-1].mData == "icons"){
oFixedOptions.iRightColumns = 1;
}

// Don't render on IE8 or below
if (!(jq.browser.msie && jq.browser.version <= '8')){
//new FixedColumns( oTable, oFixedOptions);
new jq.fn.dataTable.FixedColumns(oTable, oFixedOptions);
}
<?php } ?>

<?php } ?>
[/code]


And here is the state load/save callbacks:
[code]
function fnStateSave(oSettings, oData) {
// Send an Ajax request to the server with the state object
jq.ajax({
"url": '<?php echo MOD_URL.CLASS_NAME."/?event=stateSave";?>',
"data": oData,
"dataType": "json",
"method": "POST",
"success": function () {}
});
}

function fnStateLoad(oSettings) {
var o;

// Send an Ajax request to the server to get the data. Note that
// this is a synchronous request.
jq.ajax({
"url": '<?php echo MOD_URL.CLASS_NAME."/?event=stateLoad";?>',
"async": false,
"dataType": "json",
"success": function (json) {
o = json;
}
});

return o;
}

[/code]

Replies

  • allanallan Posts: 63,489Questions: 1Answers: 10,470 Site admin
    DataTables will do a state save on each draw, which is what is happening here. It is a little cheeky, and generally I strongly recommend against using anything in the settings object, but what you could do is:

    [code]
    if ( oSettings.iDraw <= 1 ) {
    return;
    }
    [/code]

    in your state save function.

    Allan
  • WeaponX86WeaponX86 Posts: 40Questions: 0Answers: 0
    Why would it do this? It also fires the second time due to FixedColumns which is causing unnecessary overhead.
  • allanallan Posts: 63,489Questions: 1Answers: 10,470 Site admin
    Simply because its programmatically the easiest thing to do - do a save on every draw requires a lot less code than any other option.

    Allan
This discussion has been closed.