【问题标题】:How to handle complex rowspan in angular 6?如何处理角度 6 中的复杂行跨度?
【发布时间】:2021-01-25 13:11:21
【问题描述】:

我已经从下面提到的 JSON 创建了表格,它工作正常。我有某些情况需要处理。这里也提到了我使用的功能。我还附上了相同的输出图像。非常感谢您的帮助...提前致谢 条件:

  • 如果电子邮件行为空,则需要删除该特定行。
  • 假设 value2 在电子邮件中有一个值,在这种情况下应该显示它。

    

        rows = [];
    
    generateTable() {
      if (!this.data) {
        return;
      }
    
      this.rows.push([
        {
          text: this.data.e_o_name,
          rowspan: 0
        }
      ]);
      let maxRowSpan = 0;
    
      this.data.matching_details.forEach((detail, i) => {
        const elemRowSpan = Math.max(detail.matching_attributes.length, 1);
        maxRowSpan += elemRowSpan;
    
        if (i > 0) {
          this.rows.push([])
        }
        this.rows[this.rows.length - 1].push({
          text: detail.me_value,
          rowspan: elemRowSpan
        });
    
        detail.matching_attributes.forEach((attr, j) => {
          if (j > 0) {
            this.rows.push([])
          }
    
          const mail = attr.me_list[0];
          this.rows[this.rows.length - 1].push(
            {
              text: attr.me_name,
              rowspan: 1
            },
            {
              text: mail.me_email_list.map(({ me_value }) => me_value).join(', '),
              rowspan: 1
            },
            {
              text: mail.me_percent,
              rowspan: 1
            }
          );
        })
      });
      this.rows[0][0].rowspan = maxRowSpan;
    }
    
    ```
    #Josn : #
    ```
    
{
   "e_id":"1234",
   "e_o_name":"Contact_info",
   "matching_details":[
      {
         "me_value":"value1",
         "matching_attributes":[
            {
               "me_id":"1234",
               "me_name":"28 sai",
               "me_list":[
                  {
                     "me_type":"Email ID",
                     "me_email_list":[
                        {
                           "me_value":"a@gmail"
                        },
                        {
                           "me_value":"b@gmail"
                        }
                     ],
                     "me_percent":"100"
                  }
               ]
            },
            {
               "me_id":"5678",
               "me_name":"29 meena",
               "me_list":[
                  {
                     "me_type":"Email ID",
                     "me_email_list":[
                        {
                           "me_value":"c@gmail.com"
                        },
                        {
                           "me_value":",d@gmail.com"
                        }
                     ],
                     "me_percent":"100"
                  }
               ]
            }
         ]
      },
      {
         "me_value":"value2",
         "matching_attributes":[
            {
               "me_id":"1234",
               "me_name":"rimzim",
               "me_list":[
                  {
                     "me_type":"Email ID",
                     "me_email_list":[
                        {
                           "me_value":"p@gmail"
                        },
                        {
                           "me_value":"q@gmail"
                        }
                     ],
                     "me_percent":"100"
                  }
               ]
            },
            {
               "me_id":"5678",
               "me_name":"ranu",
               "me_list":[
                  {
                     "me_type":"Email ID",
                     "me_email_list":[
                        {
                           "me_value":"t@gmail.com"
                        },
                        {
                           "me_value":",u@gmail.com"
                        }
                     ],
                     "me_percent":"100"
                  }
               ]
            }
         ]
      }
   ]
}

【问题讨论】:

  • 你能做一个stackbliz吗...它更容易调试。
  • 您是否考虑过使用 *ngIf 来满足您的条件
  • JSmith - 我的 html 是这样的 {{ col.text }}

标签: javascript angular angular6 angular2-template


【解决方案1】:

似乎您想放入列 (attr) 级别的验证,因此在 html 中循环遍历它时,您需要实现检查

https://stackblitz.com/edit/angular-zm1ap1?file=src/app/app.component.html

<table>
    <tbody>
        <tr>
            <th>contact</th>
            <th>ty</th>
            <th>ed</th>
            <th>mail</th>
            <th>percent</th>
        </tr>
        <tr *ngFor="let row of rows">
      <!-- check if row is empty or could add additional check such 
      row[3].text (email) is true 
       -->
      <ng-container *ngIf='row && row.length > 0'> 
      <td [attr.rowspan]='row[0].rowspan'>{{row[0].text}}</td>
      <td *ngIf='row.length > 1' [attr.rowspan]='row[1].rowspan'>{{row[1].text}}</td>
      
      <td *ngIf='row.length > 2' [attr.rowspan]='row[2].rowspan'>{{row[2].text}}</td>
      <td *ngIf='row.length > 3' [attr.rowspan]='row[3].rowspan'>{{row[3].text}}</td>
      <td *ngIf='row.length > 4' [attr.rowspan]='row[4].rowspan'>{{row[4].text}}</td>
      </ng-container>
        </tr>

    </tbody>
</table>

【讨论】:

猜你喜欢
  • 2014-07-27
  • 1970-01-01
  • 2019-03-01
  • 1970-01-01
  • 2020-01-23
  • 2019-01-27
  • 1970-01-01
  • 2019-04-23
  • 2017-07-11
相关资源
最近更新 更多