Answer the question
In order to leave comments, you need to log in
Why does the filter work without a bunch of other filters?
I have a React component with multiple content filters.
--- First - text search:
<div className="search-bar filter">
<input type="text"
name="search"
id='search'
onChange={handleChange}
value={search}
placeholder = "Enter a title"
className="filter-item"/>
</div>
<select name="color"
id="color"
onChange={handleChange}
value={color}
className="filter-item select">
{
colors.map((color, index) => {
return (
<option key={index} value={color}> {color} </option>
)
})
}
</select>
<input
type="range"
name="price"
id="price"
min={min}
max={max}
value={price}
className="range-price"
onChange = {handleChange}
/>
state = {
search: '',
price: 0,
min: 0,
max: 0,
color: 'all',
cup: 'all',
}
handleChange = (event) => {
const name = event.target.name;
const value = event.target.type === "checkbox"
? event.target.checked
: event.target.value;
this.setState({
[name]: value
},
this.sortData
);
};
sortData = () => {
const {storeProducts, price, color, cup, shipping, search} = this.state;
let tempProducts = [...storeProducts];
let tempPrice = parseInt(price);
// ---------- Filter by price
tempProducts = tempProducts.filter(item => item.price <= tempPrice);
// ---------- Filter by colors
if (color !== "all") {
tempProducts = tempProducts.filter(item => item.color === color)
}
// ---------- Filter by checkbox
if(shipping) {
tempProducts = tempProducts.filter(item => item.freeShipping === true)
}
if(search.length > 0) {
tempProducts = tempProducts.filter(item => {
let tempSearch = search.toLowerCase();
let tempTitle = item.title.toLowerCase().slice(0, search.length);
if (tempSearch === tempTitle) {
return item;
}
});
}
this.setState ({
filteredProducts: tempProducts
})
}
<select name="cup"
id="cup"
onChange={handleChange}
className="filter-item select">
<option value="all" >all</option>
<option value="A" >A</option>
<option value="B" >B</option>
<option value="C" >C</option>
<option value="D" >D</option>
</select>
if (cup === "A") {
tempProducts = brasAll.filter(item => item.cup.includes("A"))
} else if (cup === "B") {
tempProducts = brasAll.filter(item => item.cup.includes("B"))
} else if (cup === "C") {
tempProducts = brasAll.filter(item => item.cup.includes("C"))
} else if (cup === "D") {
tempProducts = brasAll.filter(item => item.cup.includes("D"))
}
sortData = () => {
const {storeProducts, price, color, cup, shipping, search} = this.state;
let tempProducts = [...storeProducts];
let tempPrice = parseInt(price);
let brasAll = storeProducts.filter(item => item.type === "Bras");
// ---------- Filter by price
tempProducts = tempProducts.filter(item => item.price <= tempPrice);
// ---------- Filter by colors
if (color !== "all") {
tempProducts = tempProducts.filter(item => item.color === color)
}
// ---------- Filter by Cup
if (cup === "A") {
tempProducts = brasAll.filter(item => item.cup.includes("A"))
} else if (cup === "B") {
tempProducts = brasAll.filter(item => item.cup.includes("B"))
} else if (cup === "C") {
tempProducts = brasAll.filter(item => item.cup.includes("C"))
} else if (cup === "D") {
tempProducts = brasAll.filter(item => item.cup.includes("D"))
}
// ---------- Filter by checkbox
if(shipping) {
tempProducts = tempProducts.filter(item => item.freeShipping === true)
}
if(search.length > 0) {
tempProducts = tempProducts.filter(item => {
let tempSearch = search.toLowerCase();
let tempTitle = item.title.toLowerCase().slice(0, search.length);
if (tempSearch === tempTitle) {
return item;
}
});
}
this.setState ({
filteredProducts: tempProducts
})
}
Answer the question
In order to leave comments, you need to log in
Because you are filtering the original brasAll array, not the results. Basically, this code:
if (cup === "A") {
tempProducts = brasAll.filter(item => item.cup.includes("A"))
} else if (cup === "B") {
tempProducts = brasAll.filter(item => item.cup.includes("B"))
} else if (cup === "C") {
tempProducts = brasAll.filter(item => item.cup.includes("C"))
} else if (cup === "D") {
tempProducts = brasAll.filter(item => item.cup.includes("D"))
}
if (cup) {
tempProducts = tempProducts.filter(item => item.cup.includes(cup));
}
handleSearch = () => {
const { price, color, cup, shipping, search } = this.state;
this.props.dispatch(fetchProducts({ price, color, cup, shipping, search });
};
GET 'https://api.mysite.com/products?search=soes&price=120&color=red'
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question