Multiple custom filters on ext.search.push

Multiple custom filters on ext.search.push

ferran_munozferran_munoz Posts: 28Questions: 7Answers: 0

Hello!.

I have a problem with 3 custom filters that I have in my page and the data of the datatable. There are 3 groups for choose if I want to show the users than speak a determined language (can be more of one), to show the active users (or not) and if the user is locked (or not).

So, when I'm doing the ext.search.push, I don't know how to filter the data because show me more data that I want. This is the code:

$.fn.dataTable.ext.search.push(
    function( settings, data, dataIndex ) {
        var SelectedLanguage = [];
        $.each(arrayLanguage, function(index,value){
            var checked = $('#cbox_'+arrayLanguage[index]).prop("checked") ? 1 : 0;
            if(checked == 1){
                var languageTemp = $('#cbox_'+arrayLanguage[index]).val();
                
                if(!SelectedLanguage.includes(languageTemp)){
                    SelectedLanguage.push(languageTemp);
                }
            };
        });

        var showOnlyActive = $('#chkActive').prop("checked") ? 1 : 0;
        var showOnlyLocked = $('#chkLocked').prop("checked") ? 1 : 0;
        
        if(SelectedLanguage == 0 && showOnlyActive == 0 && showOnlyLocked == 0){
            return true;
        }else{
            var language = data[1];
            var active = data[4];
            var locked = data[5];
            
            if (SelectedLanguage.includes(language) || 
               (showOnlyActive == 1 && showOnlyActive == active) || 
               (showOnlyLocked == 1 && showOnlyLocked == locked)){
                return true;
            }else if((showOnlyActive == 1 && showOnlyActive != active) && (showOnlyLocked == 1 && showOnlyLocked != locked)){
                return true;
            }

            return false;
        }
    }
);

But, for example, when I'm filtering with 1 language selected and active checked, shows me everything (instead non active users) because I think that the condition is wrong (And I don't know how to do better).

So, the question is, it's correct this or maybe can do this more easy?

Thanks!

This question has an accepted answers - jump to answer

Answers

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

    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

  • ferran_munozferran_munoz Posts: 28Questions: 7Answers: 0
    edited June 2020

    Hi Colin!

    Thanks, I don't know that exists something like live.datatables to put examples. Sorry about that.

    I put my test case here: http://live.datatables.net/laboveho/1/edit?html,js,output

    If you try, for example, "Spanish" + "Only active", you will see that appears "English" and "French" too. And I want only the Spanish actives, you know?

    In others words, I want to do a filter of my spanish AND active, not spanish OR active. But if I do this in the query, I have to control all the others filters, and I'm scare if in the future put more filters, because the if sentence will be huge...

  • ferran_munozferran_munoz Posts: 28Questions: 7Answers: 0

    Hi Colin!

    I made a example here: http://live.datatables.net/laboveho/1/edit?html,js,output

    If you select "Spanish" + "Only active", you will see that appears users of other languages.

    I want to filter Language AND State, no language OR state... So, if I filter Spanish + only active, I want to see Spanish + Active, right?

  • kthorngrenkthorngren Posts: 21,717Questions: 26Answers: 5,026
    Answer ✓

    I changed the logic some and think its working the way you want here:
    http://live.datatables.net/kikuxota/1/edit

    I added the name attribute to the language checkboxes to make it easier to get the checked names.

    Basically I changed the code to check each column individually to determine if it should be shown. One return statement is used based on the results of checking the three columns.

    Kevin

  • ferran_munozferran_munoz Posts: 28Questions: 7Answers: 0

    Thank you very much Kevin! It works like a charm, amazing solution :smile:

This discussion has been closed.