[Solved] Override bStateSave with oSearch and aoSearchCols

[Solved] Override bStateSave with oSearch and aoSearchCols

ajosephdesignajosephdesign Posts: 5Questions: 0Answers: 0
edited December 2009 in General
When using bStateSave and filters are used in the general search input and/or column filter inputs - upon page refresh the filters are applied but the text is no longer in the inputs. This is confusing for the user. To clear the filters the user must put he cursor in the input and then press return - as if searching for nothing.

I'd like to save everything but filters in the bStateSave cookie. I've tried to use oSearch and aoSearchCols but that hasn't worked.

If it is not possible to override just the filters from the bStateSave cookie, is there a simple way to have the search phrase show up as the input's value upon page refresh.

I can imagine it would be fairly simple for the general search - just grab oPreviousSearch » sSearch param from fnSettings(). But not sure how to drill down into fnSettings() to get each column's input value.

Btw - Thank you for a really fantastic plug-in. This has saved me so much time and is working perfectly for my needs.

Replies

  • ajosephdesignajosephdesign Posts: 5Questions: 0Answers: 0
    edited December 2009
    I found this in another discussion from Allan:

    [QUOTE]

    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.

    [/QUOTE]

    I've commented out the code and its worked perfectly. I would still like to know how to get the search phrase into the inputs upon page refresh/reload as well as I'm now thinking it would be a nicer alternative.

    In Version 1.5.4 I've commented out the following"

    On lines 460:

    /*oSettings.oPreviousSearch.sSearch=oData.sFilter;*/

    On lines 461 - 464:

    /*if(typeof oData.sFilterEsc!="undefined"){oSettings.oPreviousSearch.bEscapeRegex=oData.sFilterEsc
    }if(typeof oData.aaSearchCols!="undefined"){for(var i=0;i
  • ajosephdesignajosephdesign Posts: 5Questions: 0Answers: 0
    edited December 2009
    I wrote a little function that does the trick:

    [code]
    function fill_search_fields(){
    var oSettings = oTable.fnSettings();
    $("tfoot input").each(function(i){
    if(oSettings.aoPreSearchCols[i]['sSearch']!=''){
    $(this).val(oSettings.aoPreSearchCols[i]['sSearch']);
    }
    });
    if(oSettings.oPreviousSearch['sSearch']!=''){
    $('.search_field').val(oSettings.oPreviousSearch['sSearch']);
    }
    }
    [/code]

    This is to be used as an alternative to the above technique of commenting out the code in the core (which is always a bad idea IMHO).
  • allanallan Posts: 63,523Questions: 1Answers: 10,473 Site admin
    Hi ajosephdesign,

    Nice function! This is exactly the approach that I would suggest taking with this. The reason the fields aren't automatically filled in is that DataTables doesn't know anything about them - they are entirely under the control of you, the developer, so it's necessary to have a function such as this one.

    Thanks for posting your solution - I'm sure I'll be linking to this in future!

    Regards,
    Allan
  • wcmaneswcmanes Posts: 19Questions: 1Answers: 0
    I added one update to this, after setting the footer search field value, you should remove the class that makes the field "dimmed":

    [code]
    if(oSettings.aoPreSearchCols[i]['sSearch']!=''){
    $(this).val(oSettings.aoPreSearchCols[i]['sSearch']);
    $(this).removeClass("search_init");
    }
    [/code]

    Bill
  • wtjoneswtjones Posts: 2Questions: 0Answers: 0
    What is the proper way to implement this function? I added it to my script tag and called it right after I init dataTable(); Doesn't seem to have an effect.

    My issue is that I allow the user to send a search term via GET but bSaveState clobbers it. My workaround right now to set bSaveState to false if a query string is detected.
This discussion has been closed.