I have a products list. Each product is belong to one brand,also products can be "invertor" ( a sample attribute ) and has a price.
I need to Filter the products by brand, price range and attribute.
I don't want to send a ajax request to server to filter.
I am using jQuery filter() function to do that.
Everything is working fine for me on price and "invertor" filtering, but it is working wrong when I am trying to combine it with brands with array. Fiddle
var price_from = jQuery('#min_price').val();
var price_to = jQuery('#max_price').val();
var inverter = jQuery('#inverter').val();
var brands = [];
var brands = jQuery("input[name='brand']:checked").map(function() {
return parseInt( $(this).val() );
}).get();
if ( jQuery.isNumeric( price_from ) ) {
price_from = parseInt(price_from);
}else{
price_from = 0;
}
if ( jQuery.isNumeric( price_to ) ) {
price_to = parseInt(price_to);
}else{
price_to = 30000000;
}
jQuery('#kod-pr-catalog .new-pr').hide().filter(function() {
var brand = parseInt($(this).data('brand'));
if ( jQuery('#inverter').is(":checked") && ( brands.length <= 0 ) && ( jQuery(this).data('price') >= price_from && jQuery(this).data('price') <= price_to ) ) {
console.log( "invertor ");
return ( ( jQuery(this).data('inverter') == '1' ) && ( jQuery(this).data('price') >= price_from && jQuery(this).data('price') <= price_to ) );
} else if ( jQuery('#inverter').is(":checked") && ( brands.length > 0 ) && ( jQuery(this).data('price') >= price_from && jQuery(this).data('price') <= price_to ) ) {
console.log( "invertor dd");
return ( ( jQuery.inArray( brand, brands ) > -1 ) && ( jQuery(this).data('inverter') == '1' ) && ( $(this).data('price') >= price_from && jQuery(this).data('price') <= price_to ) );
} else if ( !jQuery('#inverter').is(":checked") && ( brands.length > 0 ) && ( jQuery(this).data('price') >= price_from && jQuery(this).data('price') <= price_to ) ) {
console.log( brands.indexOf( brand ) );
return ( ( brands.indexOf( brand ) >= 0 ) && ( jQuery(this).data('inverter') == '0' || jQuery(this).data('inverter') == '' ) && ( $(this).data('price') >= price_from && $(this).data('price') <= price_to ) );
} else if ( !jQuery('#inverter').is(":checked") && ( brands.length <= 0 ) && ( jQuery(this).data('price') >= price_from && jQuery(this).data('price') <= price_to ) ) {
console.log( "invertorssss ");
return ( jQuery(this).data('price') >= price_from && jQuery(this).data('price') <= price_to ) ;
} else if ( !jQuery('#inverter').is(":checked") && ( brands.length <= 0 ) && ( jQuery(this).data('price') == 0 && jQuery(this).data('price') == 30000000 ) ) {
return false;
}
}).show();
I have 5 products, 2 of them are invertor, price range is 0-20000, and each product brand is different. When I am checking all brands and diselect invertor, only one product is appears , instead of all 5.
Where is my bug?