Only save displaylength state with cookies

Only save displaylength state with cookies

oterradaoterrada Posts: 2Questions: 0Answers: 0
edited December 2009 in General
Hi,
with the option bStateSave:true the Datatables save the state (length, filtering, pagination and sorting) with a cookie (called SpryMedia_DataTables_etc..?).
Is it possible to save/store the actual iDisplayLength only? Only the value of the number of records in each page using pagination. In my project have not sense to store filters or pagination or sorting.

Thanks in advance.

Uri
DataTables is a GREAT plug-in for jQuery!

Replies

  • allanallan Posts: 63,516Questions: 1Answers: 10,472 Site admin
    Hi Uri,

    I'm afraid that the bStateSave is "all or nothing". I hadn't actually considered the option of only making parts of the state saveable - it's an interesting idea.

    It should be fairly easy to hack the core code to make it do what you are looking for if you wish. The functions _fnSaveState and _fnLoadState are the two in question. The easiest way would just be to comment out what you don't want in _fnLoadState. To optimise it a bit, you could modify _fnSaveState to only save the information you want.

    Regards,
    Allan
  • oterradaoterrada Posts: 2Questions: 0Answers: 0
    Thanks Allan.
    Hacking the core...uhmmm... it's too much for me! ;-)
    Don't worry!if I try something, I will tell you.
    Thanks again.

    Uri
  • Vilhelm PerssonVilhelm Persson Posts: 11Questions: 0Answers: 0
    Hi
    I am also interested in only saving the display length between sessions.
    From what I can see in the code there has been no new options since 2009 and therefore I will go for the override _fnLoadState solution.

    If possible future versions can have optional
    * Page display
    * Filter
    * Sort
    * Page (remember current page)
    * Plugin

    Best regards
    Vilhelm Persson
  • Vilhelm PerssonVilhelm Persson Posts: 11Questions: 0Answers: 0
    Hi
    Sorry, when trying to override the load function I noticed that there is now a callback function fnStateLoadCallback that will solve my needs.
    Thanks
    Vilhelm
  • robertbrowerrobertbrower Posts: 158Questions: 1Answers: 0
    edited March 2011
    I use jquery.cookie.js. If you just want to save only the display length in a cookie, then try this:

    var displayLength = $.cookie('the_cookie');

    oDataTable = oTable.dataTable({
    "bJQueryUI": true,
    "iDisplayLength": displayLength == null ? 10 : displayLength,
    "aLengthMenu": [[10, 25, 50, 100, -1], [10, 25, 50, 100, "All"]],
    "sPaginationType": "full_numbers",
    "sScrollY": "239px"
    });

    oContainer.find("select").change(function () {
    $.cookie('the_cookie', oContainer.find("select").val(), { expires: 90 });
    });
  • allanallan Posts: 63,516Questions: 1Answers: 10,472 Site admin
    The information saved in the state saving cookie used by DataTables can be modified using this callback function: http://datatables.net/usage/callbacks#fnStateSaveCallback . An alternative would be to create your own state saving with a listener on the change event of the length menu which will store that in a cookie and then restore it during initialisation.

    Allan
  • robertbrowerrobertbrower Posts: 158Questions: 1Answers: 0
    Did you just beat me to my own edit? Wow. You're quite on top of things. Great work on this plugin. Thank you.
  • Vilhelm PerssonVilhelm Persson Posts: 11Questions: 0Answers: 0
    I use the fnStateLoadCallback. It where a bit tricky to get it as I want since I only want to remember the length but not the rest. It worked in version 1.7.5 and I hope it will work in the future, I use some backward compability code when deleting the abVisCols.

    "bStateSave": true,
    "fnStateLoadCallback": function ( oSettings, oData ) {
    oData.sFilter = "";
    oData.iStart=-1;
    oData.iEnd=-1;
    oData.aaSorting=oSettings.aaSorting;
    delete oData.abVisCols;
    delete oData.sFilterEsc;
    delete oData.aaSearchCols;
    return true;
    }
This discussion has been closed.