简体   繁体   English

表 - 如何跳过显示列 1 中的数据,而列 2 有多行要显示

[英]Table - How to skip displaying data in column1, while column2 has more than one line to display

How to skip displaying data in ItemA, while ItemB has more than one line to display:如何在ItemA中跳过显示数据,而ItemB显示多行:

ItemA = [Color Fruit, Nuts] ItemA = [彩色水果,坚果]

ItemB = [[green], [orange, apple, grapes], [cashew, almond]] ItemB = [[绿色], [橙子, 苹果, 葡萄], [腰果, 杏仁]]

             if (this.point.customTT === 'Item') {
                    let tableBody = '';
                    itemA.forEach((name1, i) => {
                    itemB[i].forEach((name2, j) => {

                      tableBody += `
                          <tr>
                            <td >${itemA[i]}</td>
                            <td >${itemB[i][j]}</td>
                          </tr>
                     `

                   })
                   })

                   const html = `
                      <table>
                      <thead>
                      <tr>
                          <th>ItemA</th>
                          <th>ItemB</th>
                      </tr>
                      </thead>
                      <tbody>               
                      ${tableBody}
                      </tbody>
                  </table>
                 `
                 return html;
            }

Right now, above code returns table as below -> you can notice, fruit and nuts are written many a times现在,上面的代码返回如下表 -> 你可以注意到,水果和坚果被写了很多次

ItemA   ItemB

Color   Green
Fruit   Orange
Fruit   Apple
Fruit   Grapes
Nuts    
   

Like to display table as below - not repeat itemA喜欢显示表格如下 - 不重复 itemA

ItemA   ItemB

Color   Green
Fruit   Orange
        Apple
        Grapes
Nuts    

I've added an extra if statement to handle the case where itemA is longer than itemB:我添加了一个额外的 if 语句来处理 itemA 比 itemB 长的情况:

const ItemA = ["Color", "Fruit", "Nuts"];
//const ItemB = [["green"], ["orange", "apple", "grapes"], ["cashew", "almond"]];
const ItemB = [["green"], ["orange", "apple", "grapes"]];

let tableBody = "";
ItemA.forEach((name1, i) => {
  if (ItemB[i]) {
    ItemB[i].forEach((name2, j) => {
      tableBody += `
        <tr>
          <td >${j === 0 ? name1 : ""}</td>
          <td >${name2}</td>
        </tr>
   `;
    });
  } else {
    tableBody += `
  <tr>
    <td >${name1}</td>
    <td ></td>
  </tr>
`;
  }
});

const html = `
    <table>
    <thead>
    <tr>
        <th>ItemA</th>
        <th>ItemB</th>
    </tr>
    </thead>
    <tbody>               
    ${tableBody}
    </tbody>
</table>
`;
console.log(html);

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

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