0
votes

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?

1
A jsfiddle or jsbin would be helpful - sjm
I have created the Fiddle. - Anna Gabrielyan

1 Answers

2
votes

In your html you have:

<div class="new-pr" data-price="5000" data-id="8" data-brand="3" data-new="1" data-act="1" data-inverter="1" data-sale="1" style="display: block;">

<!-- your items -->

</div>

Note that data-brand="3" data-inverter = "1". Therefore in your javascript code this line would be executed:

return ( ( jQuery.inArray( brand, brands ) > -1 ) && ( jQuery(this).data('inverter') == '0' || jQuery(this).data('inverter') == '' ) && ( $(this).data('price') >= price_from && $(this).data('price') <= price_to ) );

( jQuery(this).data('inverter') == '0' || jQuery(this).data('inverter') == '' ) is false, because jQuery(this).data('inverter') = 1