YADCF - how to get status from LocalStorage if data is filtered?

YADCF - how to get status from LocalStorage if data is filtered?

matissgmatissg Posts: 63Questions: 15Answers: 0

I'm using YADCF externally trggered and would like to notify users if data is filtered. In addition I'm using stateSave: true for my DataTable. At the moment in my Local Storage I see DataTables has saved yadcfState. When I do

"stateLoaded": function (settings, data) {
  console.log( 'Saved filter was: '+ data.yadcfState );
}

I get back [object, Object].

Is there a way I can get back some value wich tells me if there is any filtering ON for my table? Then I could do checking and notify a user that "Filter is ON!".
Thank you for any hint.

Answers

  • colincolin Posts: 15,240Questions: 1Answers: 2,599

    Hi @matissg ,

    YADCF is a third-party extension, so would be worth asking on their site or SO. If it's storing state in the stateSave object, then you could try seeing what's in that data.yadcfState, as there might be some clues in there. Try console.log(JSON.stringify(data.yadcfState)) , that'll display the object.

    If no joy, we're happy to take a look, but as per the forum rules, please link to a test case - a test case that replicates the issue will ensure you'll get a quick and accurate response. Information on how to create a test case (if you aren't able to link to the page you are working on) is available here.

    Cheers,

    Colin

  • matissgmatissg Posts: 63Questions: 15Answers: 0

    @colin Thank you, it looks like there is no values saved into yadcfState, however my filter values are saved in DataTable columns. So I besically have to do something like in this answer, I believe.

  • matissgmatissg Posts: 63Questions: 15Answers: 0

    @colin Looks like final solution could be something like this:

    stateSave: true,
    "stateLoaded": function (settings, data) {
      var i = 0;
      $.each(data.columns, function(idx, obj) {
           if ((obj.search.search !== "") && (obj.search.search !== "-yadcf_delim-")) {
             ++i;
           }
       });
       if (i > 0) {
           // do something, e.g., append your notice that Filter is ON
       } else if (i == 0) {
            // do something, e.g., append your notice that Filter is OFF
        }
      },
    

    For externally triggered YADCF filtering add this as well:

      $( "#filter-button" ).click(function() {
        yadcf.exFilterExternallyTriggered(publishedOffers);
         // do something, e.g., append your notice that Filter is ON
      });
      $( "#reset-button" ).click(function() {
        yadcf.exResetAllFilters(publishedOffers);
        // do something, e.g., append your notice that Filter is OFF
      });
    

    For anyone who is interested, what happens here is this:
    1. when DataTable is opened, we can use stateLoaded to retrieve info about saved state of the table, including any filtering
    2. we iterate over saved state of Columns from LocalStorage and count if there is any where filter is on
    3. if there is at least one filter on, do something (e.g., show notice), else do something else, e.g., remove notice
    4. in case there is externally triggered filtering panel with "Filter"/"Clean" buttons, add lines to add/remove notice as well

    I hope this helps someone.

  • colincolin Posts: 15,240Questions: 1Answers: 2,599

    Hi @matissg ,

    Yep, I'd agree with that :)

    Cheers,

    Colin

  • matissgmatissg Posts: 63Questions: 15Answers: 0

    @colin Minor tweak to have externally triggered filtering fields appear when there is no LocalStorage for DataTable:

        var tableKey = ("my_table_localStorage_key");
        if (localStorage.getItem(tableKey) === null) {
           // do something, e.g., append your notice that Filter is OFF
        }
    

    We need this for situations when there is nothing in LocalStorage for a table.

This discussion has been closed.