Get filter / search value on page load

Get filter / search value on page load

lenamtllenamtl Posts: 265Questions: 65Answers: 1
edited March 2018 in Free community support

Hi,

I'm using this code to display a warning if filter is applied.
The reason is that the filter form may be toggle and I want the user see that filter is applied because stateSave keep the filter value in memory.

It is working fine when applying using the filter or do a search but how can I check on page load....

    table7.on('search.dt', function() {                     
            // check datatables search
            if (table7.search(this.value) != "") {
        $('#users-list_filter input').addClass('dfilter_applied'); 
            } else {  
            $('#users-list_filter input').removeClass('dfilter_applied'); 

            }

           // check yadcf filters 
           if ( yadcf.exGetColumnFilterVal(table7, 0) != ""  )  
            {
              $('#users_7').show(); 
            } 
            else {  
                $('#users_7').hide(); 
            }

    });

Also how can I echo / console.log the filter value (I mean the value used by the filter / search box, not the result)?

Thanks

This question has an accepted answers - jump to answer

Answers

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

    Hi lenamtl,

    This example shows how just search() without parameters returns the current search. Likewise, columns().search() does the same if you allow searching on individual columns.

    I'm not too sure what the intention of your line 3 is - able7.search(this.value) will never return "", it always returns DataTables.Api.

    For page load, if that's the initial load, you can add the event init on line 1.

    Hope that helps,

    Cheers,

    Colin

  • lenamtllenamtl Posts: 265Questions: 65Answers: 1
    edited March 2018

    Hi Colin,

    I have edited , I have remove this.value from the code so now it is
    if (table7.search() != "") and this check if the search field have a value this is still working the same .. I guess this.value has just no effect..

    I already tried init stateLoaded.dt event with no luck?

    I'm out of idea...

    Other solution I'm thinking about is looking directly in localstorage values but this is not easy to parse...

    Could you give me an example with , columns().search() that have multiple columns search box, is there a way to globally find out if there is a search value applied or I have to check every single fields ID?

    Thanks

  • lenamtllenamtl Posts: 265Questions: 65Answers: 1

    I have tried all events and none was working.

    So for page load, I wont use Datatables event, I'm using plain Javascript instead.

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

    Hi lenamtl,

    I'm struggling too! I'm trying to create an example that is doingwhat you want, but I'm also not seeing the stateSave events getting. Once I'm there, I'll post back!

    Cheers,

    Colin

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

    Here we go! This example, if you open the console, will show the current state of the searches for the table and all columns. This also gets displayed correctly on table refresh and on each search. I hope this gets you going again!

    Cheers,

    Colin

  • lenamtllenamtl Posts: 265Questions: 65Answers: 1

    Thanks

This discussion has been closed.