reactjs 我想在表格列表中多选物料下拉

z5btuh9x  于 2023-01-17  发布在  React
关注(0)|答案(1)|浏览(117)
const [age, setAge] = React.useState("");
const [open, setOpen] = React.useState(false);

const handleChange = (event: SelectChangeEvent<typeof age>) => {
  this.setAge(event.target.value);
};

const handleClose = () => {
  setOpen(false);
};

const handleOpen = () => {
  setOpen(true);
};

<table id="customers">
  <tr>
    <th>Status</th>
    <th>Comments</th>
  </tr>
  {ViewChecklistItem.map((row) => (
    <tr key={row.TaskId}>
      <td>
        <FormControl sx={{ m: 1, minWidth: 120 }}>
          <InputLabel id="demo-controlled-open-select-label">Age</InputLabel>
          <Select
            labelId="demo-controlled-open-select-label"
            id="demo-controlled-open-select"
            open={open}
            onClose={handleClose}
            onOpen={handleOpen}
            value={age}
            label="Age"
            onChange={handleChange}
          >
            <MenuItem value={10}>Ten</MenuItem>
            <MenuItem value={20}>Twenty</MenuItem>
            <MenuItem value={30}>Thirty</MenuItem>
          </Select>
        </FormControl>
      </td>
      <td>View comments</td>
    </tr>
  ))}
</table>

请帮我解决问题是当我选择第一个下拉菜单将打开四个下拉菜单。
这里我附上我的截图

我想选择第一个下拉打开唯一的一个下拉。不为其他下拉。

rsaldnfx

rsaldnfx1#

你正在通过一个数组来创建多个<select>组件,但是所有的<select>都有相同的open属性值,所以它们一起打开和一起关闭,这是有意义的。它们也有相同的value属性值。
因此每个<select>需要具有其自己open值和其自己的value年龄。
一种解决方案是将openage的useState钩子声明为数组。

const [open, setOpen] = useState(() => {
return Array(ViewChecklistItem.length).fill(false); // this will initialize the open useState hook  based on the length of your ViewChecklistItem so open = [false,false,false,false] if you have for example 4 items in the list
});

const [age, setAge] = useState(() => {
return Array(ViewChecklistItem.length).fill(''); // age = ['','','','']
});

现在,让我们更新我们的handleClosehandleClosehandleChange

const handleClose = (index: number) : void => { 
var openArray: boolean[] = [...open];
openArray[index] = false;
setOpen(openArray);
};

const handleOpen = (index: number) : void => { 
var openArray: boolean[] = [...open];
openArray[index] = true;
setOpen(openArray);
};

const handleChange = (event: SelectChangeEvent<typeof age>,index: number) => {
var ageArray: string[] = [...age];
ageArray[index] = (event.target.value);
setAge(ageArray);
};

最后在jsx:

ViewChecklistItem.map((row,index) => ( // not that getting the index from the map function is very important here
<tr key={row.TaskId}>
<td>
        
<FormControl sx={{ m: 1, minWidth: 120 }}>
<InputLabel id="demo-controlled-open-select-label">Age</InputLabel>
<Select
  labelId="demo-controlled-open-select-label"
  id="demo-controlled-open-select"
  open={open[index]}
  onClose={()=>{handleClose(index)}}
  onOpen={()=>{handleOpen(index)}}
  value={age[index]}
  label="Age"
  onChange={(event: SelectChangeEvent<typeof age>)=>{handleChange(event, index)}}
>
  <MenuItem value={10}>Ten</MenuItem>
  <MenuItem value={20}>Twenty</MenuItem>
  <MenuItem value={30}>Thirty</MenuItem>
</Select>

ps:我对 typescript 不太熟悉

相关问题