Filtering Products by Price Range
In your existing application, you have successfully implemented filtering for products based on brand and store names using JavaScript. However, you’re currently facing a challenge in adding functionality to filter the product list by price range.
Current Implementation Overview
Your current HTML structure defines each product as a <div> element with data-brand, data-price, and data-store attributes that hold relevant information about the product. The filtering logic is handled via JavaScript, where you maintain arrays for selected brands and stores to display only the matching products.
Price Range Filtering Approach
To implement price range filtering, we need to extend the existing logic to consider the data-price attribute alongside brand and store filters. Here’s a step-by-step approach:
-
Expand the
displaydivs()function: Modify thedisplaydivs()function to consider the selected price range when displaying matching products. -
Add a new array for selected price ranges: Create an empty array, say
priceRange, to store the selected price ranges from checkboxes with class.price. -
Update the filtering logic: When a price range is selected or deselected, update the
priceRangearray and iterate through thebrandandstorearrays alongside this new array in thedisplaydivs()function.
Implementation Details
Let’s modify your existing JavaScript code to incorporate price range filtering:
// Step 1: Add a new array for selected price ranges
let priceRange = [];
$('input.price').change(function () {
if ($(this).is(":checked")) {
priceRange = $(this).val().split('-').map(Number);
displaydivs(brand, store);
} else {
// Reset the price range array when deselected
priceRange = [];
displaydivs(brand, store);
}
});
// Step 2: Modify the displaydivs() function to consider price ranges
function displaydivs(brand, store) {
$("#prod > div").hide();
if (brand.length > 0 && store.length > 0) {
// ... existing code ...
} else if (brand.length > 0 && !(store.length > 0)) {
$.each(brand, function (index, value) {
$("#prod > div[data-brand=" + value + "]").each(function () {
let price = parseInt($(this).data('price'));
// Display only products within the selected price range
if (price >= priceRange[0] && price <= priceRange[1]) {
$(this).show();
}
});
});
} else if (!(brand.length > 0) && store.length > 0) {
$.each(store, function (index, value) {
$("#prod > div[data-store=" + value + "]").each(function () {
let price = parseInt($(this).data('price'));
// Display only products within the selected price range
if (price >= priceRange[0] && price <= priceRange[1]) {
$(this).show();
}
});
});
} else if (priceRange.length > 0) { // Step 3: Single price filter case
$("#prod > div").each(function () {
let price = parseInt($(this).data('price'));
if (price >= priceRange[0] && price <= priceRange[1]) {
$(this).show();
}
});
} else {
$("#prod > div").show();
}
}
Conclusion
In this updated implementation, the displaydivs() function now considers the selected price range when displaying matching products. When a brand, store, or price range is selected or deselected, the relevant arrays are updated, and matching products are displayed accordingly.
This approach ensures that your application maintains the flexibility to display products based on brand, store, and price range combinations, providing users with a comprehensive filtering experience.
Remember to test this implementation thoroughly to ensure it handles edge cases correctly. For further best practices in JavaScript development, consider exploring resources like Laravel Company for insights into modern web technologies.
Happy coding!