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 :
- Passing through the array which contains the filter (
filtre_transports) - Passing through the array which contains the value(s) (
ligne_transports) - 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
Fixed: http://jsfiddle.net/r4mfv/2/
You had a couple of issues:
$(filtre_transports).eachis not the way to iterate over an array, you should use$.each(filtre_transports, function() {...}).You should cast
filtreandthistoStringbefore comparing them.