Val
Val

Reputation: 752

jQuery filter table with multi select

I'm trying to filter a table with some filters. Some are simple selects, and others are multiples. For the simple ones, that's ok, but not the multiple.

I want to follow this logic :

  1. Passing through the array which contains the filter (filtre_transports)
  2. Passing through the array which contains the value(s) (ligne_transports)
  3. If an element of the 1. isn't in the 2. so not display the line (transports_matches = false)

I made this code :

// Pass through each line of the table
jQuery('#agents_liste tbody tr').not('.vide').each(function() {
    var transports_matches = true;

    // ligne_transports is an array contains values to compare with the filter
    var ligne_transports = jQuery(this).children('td').eq(2).text().split('###');

    // filtre_transports is an array contains the selected val of a multi select
    jQuery(filtre_transports).each(function() {
        var filtre = jQuery(this);
        var filtreOk = false;

        jQuery(ligne_transports).each(function() {
            if (filtre == jQuery(this)) {
                filtreOk = true;
                return false;
            }
        });

        if (!filtreOk) {
            transports_matches = false;
            return false;
        }
    });
});

The problem : When we have filters selected, the result transports_matches is always false.

Btw, I saw this post where the answer is to use classes, but is there a way without them ?

EDIT : You can see the JSFiddle here.

Thanks

Upvotes: 0

Views: 2327

Answers (1)

wroniasty
wroniasty

Reputation: 8052

Fixed: http://jsfiddle.net/r4mfv/2/

You had a couple of issues:

  1. $(filtre_transports).each is not the way to iterate over an array, you should use $.each(filtre_transports, function() {...}).

  2. You should cast filtre and this to String before comparing them.

Upvotes: 1

Related Questions