在我的react应用程序中,我有3个下拉菜单,第一个选择一些具有升序和降序选项的属性,第二个按头发颜色过滤,第三个按职业过滤。但是,根据我首先使用哪个下拉列表,之后的其他2个列表将无法工作,除非我刷新浏览器。数据是使用axios从某处获取的,下拉菜单是子组件和作为回调函数传递回的prop。
我认为我需要重设其他搜索条件的状态,已经尝试过了,但是没有用。我也在寻找可以使用多个过滤器的解决方案,但是我似乎无法解决我的问题。
这是我组件的初始状态:
class HomePage extends Component {
constructor(props) {
super(props);
this.state = {
data: [],
imageIsLoaded: false,
orderBy: "",
order: "",
profession: "",
hairColor: "",
};
this.doOrderBy = this.doOrderBy.bind(this);
this.doOrder = this.doOrder.bind(this);
this.handleColor = this.handleColor.bind(this);
this.handlePro = this.handlePro.bind(this);
}
这里是处理程序
doOrderBy(e) {
const newOrderBy = e.target.value;
this.setState({
orderBy: newOrderBy,
});
}
doOrder(e) {
const newOrder = e.target.getAttribute("data-value");
this.setState({ order: newOrder });
}
doOrderBy是一个数组[“ name”,“ age”,weight“ ..] doOrder是[“ asc”,“ desc”]
handleColor(e) {
const uniqueHairColor = [
...new Set(this.state.data.map(item => item.hair_color)),
];
const newColor = [...e.target.selectedOptions].map(opt => opt.value);
const newValue = newColor == " " ? uniqueHairColor : newColor;
this.setState({
hairColor: newValue,
});
}
handlePro(e) {
const newProfession = [...e.target.selectedOptions].map(opt =>
opt.value);
const allProfessions = this.state.data
.map(item => item.professions)
.flat(1);
const uniqueProfession = [...new Set(allProfessions)].sort();
const newValue = newProfession == " " ? uniqueProfession :
newProfession;
this.setState({
profession: newValue,
});
}
以下是排序逻辑:
render() {
const {data, imageIsLoaded, hairColor, orderBy, order, profession}
=this.state;
let sorted = data;
if (order) {
if (orderBy !== ("number of friends" && "number of professions"))
{ sorted = _.orderBy(sorted, item => {
return item[orderBy]}, order);
} else if (orderBy === "number of friends") {
sorted = _.orderBy(sorted, item => {
return item.friends.length},order);
} else {sorted = _.orderBy(sorted, item => {
return item.professions.length}, order);
}
} else if (hairColor) {
sorted = _.filter(sorted, item => _.includes(hairColor,
item.hair_color));
} else if (profession) {
sorted = _.filter(sorted, { professions: profession });
} else {
sorted = data;
}
以下是渲染菜单中作为组件的下拉菜单:
<OrderMenu
doOrder={this.doOrder}
order={order}
orderBy={orderBy}
doOrderBy={this.doOrderBy}
placeholder="Select by..."
/>
<HairMenu
data={data}
handleHairInput={this.handleColor}
placeholder="Choose hair color..."
/>
<ProfessionMenu
data={data}
handleInputPro={this.handlePro}
placeholder="Choose profession..."
/>
,数据呈现为
<AnimalCards sorted={sorted}/>
理想情况下,我希望过滤器可以一起工作,同时进行多个过滤。但是目前,当我使用第一个过滤器时,除非刷新浏览器,否则第二个或第三个过滤器将根本无法工作。指出我所缺少的内容和位置的任何帮助都会很棒。谢谢。
import React, { Component, Fragment } from "react";
class AnimalCards extends Component {
render() {
const { sorted } = this.props;
const Jobless = <div>No profession to show</div>;
const NoFriends = <div>No friends to show</div>;
return (
<Fragment>
<div className="flip-card-container">
{sorted.map(animal => {
return (
<div className="flip-card" key={animal.id}>
<div className="flip-card-inner">
{/* ---------------START front ----------- */}
<div className="flip-card-front">
<div
className="flip-card-front-top"
style={{ backgroundColor: animal.hair_color }}
/>
<div className="img-container">
<img
className="img-circle"
src={animal.thumbnail}
alt={`${animal.name}`}
style={{ border: "5px solid white" }}
/>
</div>
<div className="flip-card-front-bottom">
<div className="name">{animal.name}</div>
<div className="attributes">
<div className="age-group">
<img
className="ageIcon"
src={require("../images/axe.png")}
alt="ageIcon"
/>
<div className="age">{animal.age + " "}
yrs</div>
</div>
<div className="weight-group">
<img
className="weightIcon"
src={require("../images/weight.png")}
alt="weightIcon"
/>
<div className="weight"> {animal.weight} kg
</div>
</div>
<div className="height-group">
<img
className="ageIcon"
src={require("../images/height.png")}
alt="heightIcon"
/>
<div className="height">{animal.height} cm
</div>
</div>
</div>
<div className="professions">
Number of professions:
<div className="professions-num">
{animal.professions.length === 0
? Jobless
: animal.professions.length}
</div>
</div>
<div className="friends">Number of friends:</div>
<div className="friends-num">
{animal.friends.length === 0
? NoFriends
: animal.friends.length}
</div>
</div>
</div>
{/* ------------START front ----------- */}
{/* ------------START back ------------- */}
<div className="flip-card-back">
<div className="back-container">
<div className="back-header">Professions</div>
<div className="back-list">
{animal.professions.length === 0
? Jobless
: animal.professions.join(", ")}
</div>
</div>
<div className="back-container">
<div className="back-header">Friends</div>
<div className="back-list">
{animal.friends.length === 0
? NoFriends
: animal.friends.join(", ")}
</div>
</div>
</div>
{/* -------------END back ------------- */}
</div>
</div>
);
})}
</div>
</Fragment>
);
}
}
export default AnimalCards;
这是我获取的数据(以json格式):
fetchAnimals() {
const apiUrl =
"https://raw.githubusercontent.com/rrafols/
mobile_test/master/data.json";
axios.get(apiUrl).then(({ data }) => {
localStorage.setItem("data", data);
this.setState({
data: data.Brastlewark,
imageIsLoaded: true,
});
});
}
componentDidMount() {
this.fetchAnimals();
}
答案 0 :(得分:1)
问题实际上很简单。您使用了if-else-if块!
if (order) { //If this is true the other else ifs will not be executed
if (orderBy !== ("number of friends" && "number of professions"))
{ sorted = _.orderBy(sorted, item => {
return item[orderBy]}, order);
} else if (orderBy === "number of friends") {
sorted = _.orderBy(sorted, item => {
return item.friends.length},order);
} else {sorted = _.orderBy(sorted, item => {
return item.professions.length}, order);
}
} else if (hairColor) { // If the above condition was met this will not be checked
sorted = _.filter(sorted, item => _.includes(hairColor,
item.hair_color));
} else if (profession) { //If any of the above was true this would not be checked
sorted = _.filter(sorted, { professions: profession });
} else {
sorted = data;
}
所以......将if-else-if块变成单独的/独立的if块! 另外,关于算法的小建议也请务必在排序之前进行过滤!
这是固定的逻辑。
render() {
const {
data,
imageIsLoaded,
hairColor,
orderBy,
order,
profession
} = this.state;
let sorted = data;
if (hairColor) {
sorted = _.filter(sorted, item => _.includes(hairColor,
item.hair_color));
}
if (profession) {
sorted = _.filter(sorted, {
professions: profession
});
}
if (order) {
if (orderBy !== ("number of friends" && "number of professions")) {
sorted = _.orderBy(sorted, item => {
return item[orderBy]
}, order);
} else if (orderBy === "number of friends") {
sorted = _.orderBy(sorted, item => {
return item.friends.length
}, order);
} else {
sorted = _.orderBy(sorted, item => {
return item.professions.length
}, order);
}
}
}