首页 > 解决方案 > React - Material UI 表在每一行中包含一个下拉列表

问题描述

所以我有这个表我已经从数组映射中渲染了它的行,如下所示

<TableContainer component={Paper}>
            <Table className={classes.table} aria-label="simple table">
              <TableHead>
                <TableRow>
                  <TableCell>Object Name</TableCell>
                  <TableCell align="center">Object Field and Values</TableCell>
                </TableRow>
              </TableHead>
              <TableBody>
                {union &&
                  unionArray.map((row) => (
                    <TableRow key={row.name}>
                      <TableCell component="th" scope="row">
                        {row.name}
                      </TableCell>
                      {Object.keys(row).map((key) =>
                        key === "name" ? (
                          ""
                        ) : (
                          <TableCell align="center">
                            {/*insert dropdown select*/}
                            <FormControl
                              variant="outlined"
                              className={classes.formControl}
                            >
                              <InputLabel htmlFor="outlined-age-native-simple">
                                Values
                              </InputLabel>
                              <Select
                                native
                                label="Value"                                
                              >
                                <option aria-label="None" value="" />
                                <option>{key}:{row[key]}</option>                                
                              </Select>
                            </FormControl>
                          </TableCell>
                        )
                      )}
                    </TableRow>
                  ))}
              </TableBody>
            </Table>
          </TableContainer>

我映射的对象数组如下所示。即UnionArray 联合数组

我的问题是具有第三个键/值对的行呈现为整个表格单元格,我只希望它们成为下拉列表的一部分。现在的输出是这样的 输出

标签: javascriptreactjsmaterial-uidropdown

解决方案


您可以修改从 api 获得的数组并执行以下操作:

let modifiedUnion = unionArray.map(el=>{
  let tempObj={}
  let values=[]
  Object.keys(el).map(key=>
    if(key!=="name"){
      tempObj.values = values.push({[key]:el[key]})
    }else{
      tempObj.name = el.name
    }
  return tempObj
})

然后像这样写这部分:

{modifiedUnion.map((row) => (
  <TableRow key={row.name}>
    <TableCell align="center">
      {row.name}
    </TableCell>
    <TableCell align="center">
      <FormControl variant="outlined" className={classes.formControl}> 
        <InputLabel htmlFor="outlined-age-native-simple">
          Values
        </InputLabel>
        <Select native label="Value">
          <option aria-label="None" value="" />
            {row.values.map((key) =>
              <option>{Object.keys(key)[0]}:{Object.values(key)[0]}</option>    
            )}                             
        </Select>
      </FormControl>
    </TableCell>
  </TableRow>
))}

推荐阅读