【问题标题】:HTML table rowspan in reactjsreactjs中的HTML表格行跨度
【发布时间】:2022-01-28 10:18:12
【问题描述】:

当我使用地图并想制作这样的表格时我很困惑

有数据

const arr =[{
no: 1,
name:'david',
fruit: 'apple',
type:[{ typeName:'red apple'},{typeName:'green apple'}]
},
{
no: 2,
name: 'david',
fruit: 'orange',
type:[{ typeName:'mandarin orange'},{typeName:'bergamot orange'}]
}
]

我已经尝试过了,但我卡住了,我只能这样做

我很困惑如何在此表中合并“大卫”

【问题讨论】:

    标签: javascript html css reactjs html-table


    【解决方案1】:

    我解决了这个问题。请查看this code

    // App.js
    import "./styles.css";
    import React, { Fragment } from "react";
    
    export default function App() {
      const arr = [
        {
          no: 1,
          name: "david",
          fruit: "apple",
          type: [{ typeName: "red apple" }, { typeName: "green apple" }]
        },
        {
          no: 2,
          name: "david",
          fruit: "orange",
          type: [{ typeName: "mandarin orange" }, { typeName: "bergamot orange" }]
        },
        {
          no: 3,
          name: "jason",
          fruit: "orange",
          type: [{ typeName: "mandarin orange" }, { typeName: "bergamot orange" }]
        }
      ];
    
      let namesArr = {};
      const rowSpan = arr.reduce((result, item, key) => {
        if (namesArr[item.name] === undefined) {
          namesArr[item.name] = key;
          result[key] = 1;
        } else {
          const firstIndex = namesArr[item.name];
          if (
            firstIndex === key - 1 ||
            (item.name === arr[key - 1].name && result[key - 1] === 0)
          ) {
            result[firstIndex]++;
            result[key] = 0;
          } else {
            result[key] = 1;
            namesArr[item.name] = key;
          }
        }
        return result;
      }, []);
    
      return (
        <div>
          <table>
            <tr>
              <th>no</th>
              <th>name</th>
              <th>fruit</th>
              <th>type</th>
            </tr>
            {arr.map((el, index) => (
              <tr>
                <td>{el.no}</td>
                {rowSpan[index] > 0 && <td rowSpan={rowSpan[index]}>{el.name}</td>}
                <td>{el.fruit}</td>
                <td style={{}}>
                  {el.type.length &&
                    el.type.map((ele, i) => (
                      <Fragment>
                        <tr
                          style={{
                            border: "none",
                            display: "flex",
                            flexWrap: "wrap"
                          }}
                        >
                          <td
                            style={{
                              border: "none",
                              width: "100%",
                              borderTop: i > 0 ? "1px solid black" : "none"
                            }}
                          >
                            {ele.typeName}
                          </td>
                        </tr>
                      </Fragment>
                    ))}
                </td>
              </tr>
            ))}
          </table>
        </div>
      );
    }
    
    // styles.css
    table,
    th,
    td {
      border: 1px solid black;
      border-collapse: collapse;
    }
    

    【讨论】:

    • 谢谢兄弟!!!!你非常非常帮助我:')。我已经头晕了 3 天了
    • 我很高兴,我想我们可以成为好朋友。我可以和你聊天吗?
    • 是的,我想是的。我可以添加哪些社交媒体?
    • 当然,我更喜欢使用Skype。请告诉我你的 Skype id。
    • 这是我的 Skype id 直播:dickareynaldisiahaan,你可以添加它
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-10-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-14
    • 2021-02-05
    • 1970-01-01
    相关资源
    最近更新 更多