【发布时间】:2018-10-21 09:27:02
【问题描述】:
我有两个正在过滤的下拉菜单,但它们会在您将它们下拉并进行选择时进行过滤。我有一个搜索按钮,我想将它们都连接到。因此,在您按下按钮后,您只看到了一次结果的变化。我想我有我需要的所有逻辑但我不确定如何连接按钮 注意:我知道我在渲染中有很多逻辑,但我只是想让它先工作
到目前为止,这是我所拥有的:
constructor(props) {
super(props);
this.state = {
developers: [],
filterCountry: "All locations",
filterSkills: "All skills"
};
}
componentDidMount() {
fetch('API')
.then(features => features.json())
.then(developers => {
this.setState({ developers })
})
}
filterCountry(e){
this.setState({filterCountry: e })
}
filterSkills(e){
this.setState({filterSkills: e })
}
render() {
let developers = this.state.developers.features
if (!developers ){
return null
}
if (this.state.filterCountry && this.state.filterSkills) {
developers = developers.filter( developer => {
return this.state.filterCountry === 'All locations' ||
developer.properties.continent.includes(this.state.filterCountry)
});
developers = developers.filter( developer => {
return this.state.filterSkills === 'All skills' ||
developer.properties.skills.includes(this.state.filterSkills)
});
}
return (
<div>
<div>
<ControlSelect
onChange={this.filterCountry.bind(this)}
value={this.state.filterCountry}
options={options_dd1}
/>
</div>
<div className="inline-block mr24">
<ControlSelect
onChange={this.filterSkills.bind(this)}
value={this.state.filterSkills}
options={options_dd2}
/>
</div>
<button>Search</button>
</div>
<div>
<div>
{developers.map(developer => {
return (
<div key={developer.id}">
{developer.properties.name}
{developer.properties.description}
{developer.properties.skills}
</div>
</div>
</div>
)}
)}
)
任何帮助将不胜感激
【问题讨论】:
-
你为什么不能把这个逻辑添加到
onClick的button中? -
这部分我有点困惑.. 我不知道如何构建 onclick
-
更多关于你的代码发生了什么的细节会有所帮助。这些
ControlSelect组件是什么?它们的代码在哪里?.includes()是什么? -
控件选择只是生成下拉菜单(这些组件的代码并不是真正需要解决按钮问题)..,并且includes是一个javascript方法,用于确定数组是否包含某个元素,
标签: javascript reactjs filter dropdown