[英]get same value from multiple select dropdown in react js
I won't do to like this.我不会喜欢这个的。 I have a basket and here I have five apples.
我有一个篮子,这里有五个苹果。 I want to divide it into five-person.
我想把它分成五个人。 If a user takes 2 items from here.
如果用户从这里拿走 2 件物品。 Then the next person will have to be available 3 items.
然后下一个人必须有 3 件物品。 If this person takes 2 items then the basket has only 1 item.
如果这个人拿了 2 件物品,那么篮子里只有 1 件物品。 The five-user will be like
五用户将像
<select>
<label>userOne</label>
<option>0</option>
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
</select>
{/* userOne selected 2 */}
<select>
<label>userTwo</label>
<option>0</option>
<option>1</option>
<option>2</option>
<option>3</option>
{/* removed automaticaly 4 and five option */}
{/* <option>4</option>
<option>5</option> */}
</select>
{/* userTwo select 2 */}
<select>
<label>userThree</label>
<option>0</option>
<option>1</option>
{/* <option>2</option>
<option>3</option> */}
{/* 3-2 = 1 */}
</select>
{/* userFour have only one */}
<select>
<label>userFour</label>
<option>0</option>
<option>1</option>
{/* <option>2</option>
<option>3</option>
<option>4</option>
<option>5</option> */}
</select>
{/* userFour already selected one */}
<select>
<label>userFive</label>
<option>0</option>
{/* userFive have no item available */}
{/* <option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option> */}
</select>
As far as I understand, you want to filter the list of options depending on the selected data据我了解,您希望根据所选数据过滤选项列表
For this you need to display options dynamically.为此,您需要动态显示选项。
Example of such code:此类代码的示例:
import React, { useState } from "react";
const USER_COUNT = 5;
const OPTION_COUNT = 5;
const options = Array(OPTION_COUNT + 1)
.fill(0)
.map((_, i) => i);
function App() {
let [users, setUsers] = useState(Array(USER_COUNT).fill(0));
const handleChange = (idx) => (e) => {
setUsers(
users.map((user, userIdx) => {
if (userIdx < idx) {
return user;
}
if (userIdx === idx) {
return +e.target.value;
}
return 0;
})
);
};
return (
<div className="App">
{users.map((user, userIdx) => {
let prevOptionsSum = 0;
for (let i = 0; i < userIdx; i++) {
prevOptionsSum += users[i];
}
let userOptions = options.slice(0, options.length - prevOptionsSum);
return (
<div key={userIdx}>
<label>user {userIdx + 1}</label>
<select value={user} onChange={handleChange(userIdx)}>
{userOptions.map((_, userOptionIdx) => (
<option key={userOptionIdx}>{userOptionIdx}</option>
))}
</select>
</div>
);
})}
</div>
);
}
export default App;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.