Is there a way to "remember" the search values of column range filters? (similar to aoPreSearchCols)

Is there a way to "remember" the search values of column range filters? (similar to aoPreSearchCols)

daniel_rdaniel_r Posts: 460Questions: 4Answers: 67
edited January 2014 in DataTables 1.9
Hi

Im my plugin YADCF I want to be able to support the bStateSave for my column filters, I know I can use the aoPreSearchCols to load the filters values for my simple filters select/autocomplete, but the more advanced filters range_number/range_date/etc... works a bit differently from the simple ones and because of that their values are not being stored in the aoPreSearchCols (for a good reason) but still before I'll start writing my own cookies I wanted be sure that there is no other way to do it with the help of datatables.

So is there a way to store the range filters values (from - to) with datatables, or I must add my own cookies?

Daniel.

Replies

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    You'd need to hook into the stateSaveParams and stateLoaded events to save and load the extra parameters:

    https://github.com/DataTables/DataTablesSrc/blob/master/js/core/core.state.js#L32
    https://github.com/DataTables/DataTablesSrc/blob/master/js/core/core.state.js#L105

    This allows modification of the parameters being loaded (you might actually need to use `oSettings.oLoadedState` depending upon when your plug-in is initialised - since stateLoaded might have already fired!).

    Regards,
    Allan
  • daniel_rdaniel_r Posts: 460Questions: 4Answers: 67
    Thanks for the references, I tried to figure out how to use those two methods, stateSaveParams and stateLoaded, but without luck :/ , I looked at other plugins and how they make use of the stateSaveParams and stateLoaded, but I still can't figure it out, any chance for a really basic example (some hello world) on how to save some key:value parameter and how to read it out later on?


    That could be really helpful. and speed up the development of the yadcf plugin.

    Thanks ahead,

    Daniel.
  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    edited January 2014
    This is possibly the dumbest plug-in ever, however... What it does is count the number of clicks on the table, displaying that number. It also state saves that number and shows the number of clicks on the table the last time the table was loaded:

    Live: http://live.datatables.net/irawin/3/edit

    [code]
    $.fn.dataTableExt.aoFeatures.push( {
    "fnInit": function( settings ) {
    var clickCount = 0;
    var loadClickCount = 0;
    var el = $(''+
    'Live click count:
    '+
    'Load click count: '+
    ''
    );
    var redraw = function () {
    el.find('span').eq(0).html( clickCount );
    el.find('span').eq(1).html( loadClickCount );
    };

    $(settings.nTable).on( 'click', function () {
    clickCount++;
    redraw();
    settings.oApi._fnSaveState( settings ); // internal function since there is no 'public' api to save state
    } );

    $(settings.nTable).on( 'stateSaveParams.dt', function ( e, settings, data ) {
    data.clickCount = clickCount;
    } );

    // Load - feature plug-ins init occurs after stateLoaded event,
    // so read from the staved state
    loadClickCount = settings.oLoadedState && settings.oLoadedState.clickCount ?
    settings.oLoadedState.clickCount : 0

    redraw();

    return el[0];
    },
    "cFeature": "C",
    "sFeature": "TableClicks"
    } );


    $(document).ready(function() {
    $('#example').dataTable( {
    dom: "Cfrtip",
    stateSave: true
    } );
    } );
    [/code]

    I think I should perhaps add `state.save()` , `state.data()` and `state.loaded()` methods to the API to make this cleaner... And I'd do that immediately if I weren't trying to keep the core code size down - its already 2K larger than I wanted for 1.10 and I'm running gout of ideas for how to make it smaller!

    Allan
  • daniel_rdaniel_r Posts: 460Questions: 4Answers: 67
    Thanks for your example, I really appreciate your help. But the yadcf plugin being called on the datatables constructor, like this: $('#example').dataTable().yadcf(....);
    I'm not using the sDom, so I'm not sure that I can use your example :/

    I'm looking for a way to save some info to the "state" just after user perform an action (use one of my range filters) and to load it just before the table is being drawn so I will be able to read from state and the table will be filtered according to my $.fn.dataTableExt.afnFiltering pushed logic...

    As I said I tried to to find some relevant information but without luck, Also yadcf support atm datatables 1.9 only.

    Thanks ahead.

    Daniel
  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    Here is a small modification to use a jQuery plug-in: http://live.datatables.net/irawin/4/edit . Its totally dodgy obviously, with its insertion of the extra elements in a "random" place and the assumptions the plug-in makes about the fact that the DataTable has already been initialised, only one table etc, but hopefully it will show how it could work...

    Allan
  • daniel_rdaniel_r Posts: 460Questions: 4Answers: 67
    Thanks Allan!

    Looks good, and it looks like what I was looking for.
This discussion has been closed.