简体   繁体   English

使用reactjs创建表时如何删除行中重复的数据条目

[英]how to remove duplicating data entry in a row when creating a table with reactjs

Sorry for the bad title, but I'm not sure how to google this issue. 对不起,标题不好,但是我不确定如何用Google搜索这个问题。 For what ever reason, I'm having trouble wrapping my head around about how to create this table in reactjs. 无论出于什么原因,我都难以理解如何在reactjs中创建该表。 Where XX under column1 is empty while the data in column two will continue to populate. 其中column1下的XX为空,而第二列中的数据将继续填充。

Pretty much this is what I want: where xx is blank 这就是我想要的:xx为空白

   column 1    column 2

1. David       Male
   xx          30
   xx          BasketBall
2. Sam         BaseBall
3. John        Male
   xx          Football

I'm trying to do this with reactjs and material-ui. 我正在尝试使用reactjs和material-ui做到这一点。

my data format : 我的数据格式:

[
 ['David', ['Male', '30', 'BasketBall']],
 ['Sam', ['BaseBall']],
 ['John', ['Male', 'FootBall']]
]

This is a snippet of my code: 这是我的代码的片段:

    <TableBody>
//this will display 1 row for 1 item as in number 2 mentioned above.
              {data.map((prop, key) => {
                if (prop[1].length <= 1) {
                  return (
                    <TableRow key={key}>
                      {prop.map((prop, key) => {
                        return (
                          <TableCell className={classes.tableCell} key={key}>
                            {prop}
                          </TableCell>
                        );
                      })}
                    </TableRow>
                  );
                } else {

//this is where I'm stuck at
                  return (
                    <TableRow key={key}>
                      {prop.map((prop, key) => {
                        return (
                          <TableCell className={classes.tableCell} key={key}>
                            {prop}
                          </TableCell>
                        );
                      })}
                    </TableRow>
                  );
                }
              })}
            </TableBody>

One solution is to check if the value is an array. 一种解决方案是检查该值是否为数组。 If so map each array value into a div (to display them line-by-line). 如果是这样,则将每个数组值映射到一个div (逐行显示它们)。 Else, just output prop . 否则,只输出prop

I've added an inline style for vertical-align: top however you may want to move that to your CSS file. 我为vertical-align: top添加了一个内联样式vertical-align: top但是,您可能希望将其移动到CSS文件中。

//this is where I'm stuck at
return (
    <tr key={key}>
        {prop.map((prop, key) => (
            <td
                key={key}
                style={{verticalAlign: 'top'}}
            >
                {Array.isArray(prop) && prop.map((prop, key) => (
                    <div key={key}>{prop}</div>
                ))
                || prop}
            </td>
        ))}
    </tr>
);

I would transform my data so it's ready to be rendered without much trouble. 我将转换数据,以便可以轻松呈现它。

Here's how I would do it: 这是我的处理方式:

const tabularizeData = data =>
  data.reduce((acc, item, index) => {
      let curName;
      const name = item[0]
      const stuff = item[1]

      for (const i in stuff) {
        const thing = stuff[i]
          if (i > 0) {
            acc.push(['', '', thing])
          } else {
            acc.push([index, name, thing])
          } 
      }
      return acc
}, [])

Which would transform your data like this: 它将像这样转换您的数据:

const data = [
  ['David', ['Male', '30', 'BasketBall']],
  ['Sam', ['BaseBall']],
  ['John', ['Male', 'FootBall']]
]

tabularizeData(data)

[
  [0, "David", "Male"]
  ["", "", "30"]
  ["", "", "BasketBall"]
  [1, "Sam", "BaseBall"]
  [2, "John", "Male"]
  ["", "", "FootBall"]
]

And now creating your table should be easy. 现在创建表应该很容易。 Hope this helps! 希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM