bSaveState & saving the state in a database...

bSaveState & saving the state in a database...

robertbrowerrobertbrower Posts: 158Questions: 1Answers: 0
edited October 2013 in General
I'd prefer to save the state of the datatable in what I call a shortcut in the underlying database of my application.

I am using server side processing.

I can get the display length, display start, and sorting info and save it in the database. Now I want to restore the state from what is saved in the database.

Is there a way to at least set the display length, display start, and sorting info without reinitializing the datatable?

Thanks,

Robert

Replies

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    You can do it upfront at initialisation time using the options such as iDisplayStart etc, or you need API methods to do it. 1.9- doesn't have all the methods you'd want, but 1.10 will. Having said that, there are a few plug-ins which will let those options be set in 1.9: http://datatables.net/plug-ins/api

    Allan
  • robertbrowerrobertbrower Posts: 158Questions: 1Answers: 0
    Thanks Allan. The fnDisplayStart and fnLengthChange plugins are exactly what I needed. What is missing is a plugin for setting _aaSorting.

    I'm going to try to write fnSorting. And since I only want to redraw one time after setting length, display start, and sorting, then I will have to combine all three plugins into one.

    Does this sound correct? Thanks.

    Robert
  • robertbrowerrobertbrower Posts: 158Questions: 1Answers: 0
    $.fn.dataTableExt.oApi.fnRestoreState = function (oSettings, iDisplayLength, iDisplayStart, aaSorting, bRedraw) {

    if (typeof bRedraw == 'undefined') {
    bRedraw = true;
    }

    oSettings._iDisplayLength = iDisplayLength;
    oSettings._iDisplayStart = iDisplayStart;
    oSettings.aaSorting = aaSorting;
    oSettings.oApi._fnCalculateEnd(oSettings);

    /* If we have space to show extra rows (backing up from the end point - then do so */
    if (oSettings._iDisplayEnd == oSettings.aiDisplay.length) {
    oSettings._iDisplayStart = oSettings._iDisplayEnd - oSettings._iDisplayLength;
    if (oSettings._iDisplayStart < 0) {
    oSettings._iDisplayStart = 0;
    }
    }

    if (oSettings._iDisplayLength == -1) {
    oSettings._iDisplayStart = 0;
    }

    if (bRedraw) {
    oSettings.oApi._fnDraw(oSettings);
    }

    if (oSettings.aanFeatures.l) {
    $('select', oSettings.aanFeatures.l).val(iDisplayLength);
    }

    };
  • robertbrowerrobertbrower Posts: 158Questions: 1Answers: 0
    It's not perfect. I guess it is because I am using server side processing. The correct page is not selected after retrieving the data.
  • robertbrowerrobertbrower Posts: 158Questions: 1Answers: 0
    It works the first time it is called. It triggers the ajax call (server side processing) and displays the correct page. Then if I load my shortcut call the function again, it does not display the correct page number. Looking for a workaround...
  • robertbrowerrobertbrower Posts: 158Questions: 1Answers: 0
    Ok I got it. I removed the part about:

    /* If we have space to show extra rows (backing up from the end point - then do so */

    And it gives me what I need.

    $.fn.dataTableExt.oApi.fnRestoreState = function (oSettings, iDisplayLength, iDisplayStart, aaSorting, bRedraw) {

    if (typeof bRedraw == 'undefined') {
    bRedraw = true;
    }

    //oSettings.oApi._fnCalculateEnd(oSettings);
    oSettings._iDisplayLength = iDisplayLength;
    oSettings._iDisplayStart = iDisplayStart;
    oSettings._DisplayEnd = 0;
    oSettings.aaSorting = aaSorting;
    oSettings.oApi._fnCalculateEnd(oSettings);

    if (oSettings._iDisplayLength == -1) {
    oSettings._iDisplayStart = 0;
    }

    if (bRedraw) {
    oSettings.oApi._fnDraw(oSettings);
    }

    if (oSettings.aanFeatures.l) {
    $('select', oSettings.aanFeatures.l).val(iDisplayLength);
    }

    };
  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    Very nice! Looks good to me (and I'm blooming glad about the new API in 1.10 - its going to be a lot nicer for us all ;-) ).

    Allan
  • robertbrowerrobertbrower Posts: 158Questions: 1Answers: 0
    I hate to reopen old issues. I have another page that requires saving state to the database. But restoring the oSettings.oPreviousSearch.sSearch text value and redrawing does not update the search box. Should I just $.find() it and set the value manually or is there a better way?
  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    That's odd - it should update! yes you could just use $().find().val() to set the value, but it should be working automatically there, populating the value into the input box.

    Allan
  • robertbrowerrobertbrower Posts: 158Questions: 1Answers: 0
    It is odd indeed. But this works:

    $.fn.dataTableExt.oApi.fnRestoreState = function (oSettings, iDisplayLength, iDisplayStart, aaSorting, sSearch, bRedraw) {

    if (typeof bRedraw == 'undefined') {
    bRedraw = true;
    }

    //oSettings.oApi._fnCalculateEnd(oSettings);
    oSettings._iDisplayLength = iDisplayLength;
    oSettings._iDisplayStart = iDisplayStart;
    oSettings._DisplayEnd = 0;
    oSettings.aaSorting = aaSorting;
    oSettings.oPreviousSearch.sSearch = sSearch;

    $(oSettings.nTable).closest(".dataTables_wrapper").find(".dataTables_filter").find("input").val(sSearch);

    oSettings.oApi._fnCalculateEnd(oSettings);

    if (oSettings._iDisplayLength == -1) {
    oSettings._iDisplayStart = 0;
    }

    if (bRedraw) {
    oSettings.oApi._fnDraw(oSettings);
    }

    if (oSettings.aanFeatures.l) {
    $('select', oSettings.aanFeatures.l).val(iDisplayLength);
    }

    };
This discussion has been closed.