最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - React - Material UI table to contain a dropdown in each row - Stack Overflow

programmeradmin0浏览0评论

so I have this Table I've rendered its rows from an array map as shown below

<TableContainer ponent={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 ponent="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>

the array of objects where I mapped from is shown below. i.e UnionArray

my problem is the rows that have a third key/value pair are rendering as entire table cell, I just want them to be part of the dropdown. the output now is something like this

so I have this Table I've rendered its rows from an array map as shown below

<TableContainer ponent={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 ponent="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>

the array of objects where I mapped from is shown below. i.e UnionArray

my problem is the rows that have a third key/value pair are rendering as entire table cell, I just want them to be part of the dropdown. the output now is something like this

Share Improve this question asked Feb 3, 2021 at 17:38 Jude BobinihiJude Bobinihi 1962 silver badges14 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 2

you could modify your array you get from the api and do sth like this:

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
})

and after that write this part like this:

{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>
))}

I don't have enough reputations to ment but the answer lies in @lakshya's response and @ahmed's ment. Object.keys will return null for when key === 'name' but it will return valid JSX for when the keys aren't name, hence, your table cell having 2 dropdowns.

As for how to go about formatting the response, you can format your loop like in the image attached.

Convert the object to a format like

[{
    name: 'obj1',
    values: [
      {
        a: 1
      }
    ]
  },
  {
    name: 'obj3',
    values: [
      {
        c: 2,
        d: 5
      }
    ]
  }
]
发布评论

评论列表(0)

  1. 暂无评论