Hi All
I have to create a filtering function for a webshop, and need to do it client side for performance reasons.
I load in x number of product, so I can get to them in the DOM - And after this I have a number of checkbox groups which lets me filter and only display relevant products.
At the moment I have this checkbox group, which works fine:
<input type="checkbox" name="brand[]" value="acer" id="acer" /> <span class="filter_paragraphs">Acer</span><br />
<input type="checkbox" name="brand[]" value="lenovo" id="lenovo" /> <span class="filter_paragraphs">Lenovo</span><br />
<input type="checkbox" name="brand[]" value="hp" id="hp" /> <span class="filter_paragraphs">HP</span><br />
<input type="checkbox" name="brand[]" value="fujitsu" id="fujitsu" /> <span class="filter_paragraphs">Fujitsu</span><br />
<input type="checkbox" name="brand[]" value="samsung" id="samsung" /> <span class="filter_paragraphs">Samsung</span><br />
The products on the page are listed with several data-atr, that I want to use for filtering, like this.
<div class="product_div" data-brand="acer" data-price="xxxx" data-screen="15.6" data-show="TRUE">';
With this bit of Javascript, I am able to filter on the brand name, whenever a checkbos in that group is checked. And if nothing is checked, all products are displayed as default:
$(document).ready(function()
{
// Bind click event til checkbokse
$( 'input[type="checkbox"]' ).click( function()
{
if( $( 'input[type="checkbox"]:checked' ).length > 0 )
{
// Remove all products
$( '.product_div' ).fadeOut(300);
// Loop through all checked check boxes
setTimeout(function(){
$( 'input[type="checkbox"]:checked' ).each( function()
{
// Display ONLY those product_divs that has a match in the data-atr, according to the id of the checkbox.
$( '.product_div[data-brand = ' + this.id + ']' ).fadeIn( 100 );
// IT DOESTN WORK WITH SCREEN SIZES.....
//$( '.product_div[data-screen = ' + this.id + ']' ).fadeIn( 100 );
// Align filtered products, run function
alignFilteredProducts( this.id );
});
}, 300 )
}
else
{
// Default: show all
$('.product_div').show();
}
});
});
For screen sizes, the checkboxes I am using is setup like this:
<input type="checkbox" name="screen_group[]" value="15.6" id="15.6" /> <span class="filter_paragraphs">15,6"</span><br />
<input type="checkbox" name="screen_group[]" value="15.5" id="15.5" /> <span class="filter_paragraphs">15,5"</span><br />
<input type="checkbox" name="screen_group[]" value="15.4" id="15.4" /> <span class="filter_paragraphs">15,4"</span><br />
<input type="checkbox" name="screen_group[]" value="14" id="14" /> <span class="filter_paragraphs">14"</span><br />
But the above function, doesnt react on user input for screen sizes.
Do I need to create a function, for each checkbox group on the page: One for brands, and one for screen sizes and so on?
I need to apply many filters, so I would prefer to use one function that is strong enough to "simply": check all checked check boxes, and scan the page for each choise made, and display the products that has a data-atr="value" alike the check box id's.
Anyone who can see how I can modify the above JS function to work dynamic on all checkboxes i need to apply to the different categories?
Look forward to hear from you,
The best, Jan