Javascript 如果以前在reactjs中选择了,则从一个下拉列表中隐藏值
我在左侧有项目列表,在右侧有下拉列表。我需要设置列表项的一对一值和下拉值。所以当我为一个列表项选择一个下拉值时,它应该从下拉列表中删除Javascript 如果以前在reactjs中选择了,则从一个下拉列表中隐藏值,javascript,reactjs,select,drop-down-menu,Javascript,Reactjs,Select,Drop Down Menu,我在左侧有项目列表,在右侧有下拉列表。我需要设置列表项的一对一值和下拉值。所以当我为一个列表项选择一个下拉值时,它应该从下拉列表中删除 export default class MyComponent extends PureComponent { setFields = (e, i) => { const customers = [...this.state.customers]; const otherCustomers = this.state.otherCust
export default class MyComponent extends PureComponent {
setFields = (e, i) => {
const customers = [...this.state.customers];
const otherCustomers = this.state.otherCustomers.filter(
(qb) => qb._id === e.target.value
);
customers[i].customerId = e.target.value;
const otherCustomers = this.state.otherCustomers.filter(
(qb) => qb._id !== e.target.value
);
this.setState({ customers, otherCustomers });
};
render() {
return (
<div>
{this.state.customers.map((d, i) => {
return (
<div key={i}>
<ul>
<li>
<p>{d.cName}</p>
</li>
<li>
<div>
<select
value={d._id}
onChange={(e) => this.setFields(e, i)}
>
<option value="">Select</option>
{otherCustomers.map((d1) => {
return (
<option value={d1._id} key={d1._id}>
{d1.name}
</option>
);
})}
</select>
<label>Customers Field</label>
</div>
</li>
</ul>
</div>
);
})}
;
</div>
);
}
}
现在的问题是,当我从列表中删除所选客户时,它也会将其从所选客户中删除
那么如何在渲染本身中删除它呢?创建该列表的两个单独副本,并将该列表绑定到下拉列表中
从左侧下拉列表中选择一个值后,过滤掉另一个右侧下拉列表,并将此更新副本绑定到右侧。您需要过滤掉选项,显示相关选项code@Vaibhav更新了我的代码