如何基于字符串值在Angular中创建表?

DG4

我正在使用Angular 4/5,我必须基于字符串值在Angular中创建多个表。这是我创建表的模型。

class NameValuePair {
    Name: string;
    Value: string;
}

export class Family {
    Properties: Array<NameValuePair>;
    PropertyType: string;
}

下面给出的是表将包含的硬编码值。

export const list1: Family[] =
[
    {
      Properties: [
        {
          Name: "A",
          Value: "1"
        },

        {
          Name: "B",
          Value: "2"
        },
        {
          Name: "C",
          Value: "3"
        }
      ],
      PropertyType: "Type 1"
    },
    {
      Properties: [
        {
          Name: "A",
          Value: "10"
        },

        {
          Name: "B",
          Value: "20"
        },
        {
          Name: "C",
          Value: "30"
        }
      ],
      PropertyType: "Type 1"
    },
    {
      Properties: [
        {
          Name: "A",
          Value: "100"
        },

        {
          Name: "B",
          Value: "200"
        },
        {
          Name: "C",
          Value: "300"
        }
      ],
      PropertyType: "Type 2"
    }
  ]

现在,这里要注意的主要事情是将根据PropertyType创建表。与上面的结构一样,数组的前两个元素的PropertyType相同,即Type 1,因此将创建2个表。一个带有标题/标题:类型1,另一个带有标题/类型2。第二个数组元素的properties []数组将成为第一个表的第二行。我找不到有关如何基于此PropertyType字符串值创建表的逻辑。但是,这就是我在component.html文件中编写的内容,但是这种逻辑是不正确的。

<div class="container pt-4" *ngFor="let element of list;let i = index">
  <ng-container *ngIf="list[i].PropertyType == list[i+1].PropertyType">
    <div style="padding-left:250px;font-size: 20px" class="pb-2">{{element.PropertyType}}</div>

    <table id="{{element.PropertyType}}" class="table table-striped table-bordered table-responsive pb-3 mx-auto">
      <thead style="height:40px">
        <tr align="center">
          <th *ngFor="let property of element.Properties" style="font-size: 20px">{{property.Name}}</th>
        </tr>
      </thead>

      <ng-container *ngFor="let element1 of list">
          <tr align="center" *ngIf="element.PropertyType == element1.PropertyType">
            <td *ngFor="let property of element1.Properties; let propertyIndex = index" style="width: 200px">
              <ng-container [ngSwitch]="propertyIndex">         
                <div *ngSwitchDefault style="font-size: 20px">{{property.Value}}</div>
              </ng-container>
            </td>
          </tr>
      </ng-container>
    </table>
  </ng-container>
</div>

这里的list指的是如上所述的list1 const数组。请帮忙。

阿克沙伊(Akshay Vijay Jain)

这是完整的逻辑,我没有添加CSS,因为没有要求。使用它可以正常工作https://stackblitz.com/edit/angular-hd9jey

import {
  Component
} from '@angular/core';
import {
  list1
} from './list1';
@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',

})
export class AppComponent {
  tableArray = [];
  constructor() {
    let tableObject = {};
    list1.forEach(i => {
      if (tableObject[i.PropertyType]) {
        tableObject[i.PropertyType].push(i);
      } else {
        tableObject[i.PropertyType] = [i];
      }
      this.tableArray = Object.entries(tableObject);
    });
  }

}
<div class="container pt-4" *ngFor="let table of tableArray;index as i">

  <div style="padding-left:250px;font-size: 20px" class="pb-2">{{table[0]}}</div>
  <table id="{{table[0]}}" class="table table-striped table-bordered table-responsive pb-3 mx-auto">
    <thead style="height:40px">
      <tr align="center">
        <th *ngFor="let property of table[1][0].Properties" style="font-size: 20px">
          {{property.Name}}</th>
      </tr>
    </thead>


    <tr *ngFor="let property of table[1];" align="center">
      <td *ngFor="let va of property.Properties">
        {{va.Value}}
      </td>
    </tr>

  </table>
</div>

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

基于字符串值创建实例

如何基于C中的变量创建字符串数组

如何基于javascript中的值拆分字符串

如何基于字符串创建列

如何基于密钥中的字符串获取密钥和json字符串的值

如何在Lua中创建字符串,表名

如何在sqlcommand字符串中创建表?

如何在熊猫中基于搜索子字符串创建规则集以将值分配给特定列?

在SQL存储过程中基于逗号分隔的字符串动态创建表

如何从字符串创建lua表?

如何在 Angular 中创建 TypeScript 文字类型(从字符串到特定字符串)?

如何比较 Angular 中 tagName 和字符串的值?

如何基于字段的子字符串值联接两个表?

尝试创建一个基于字典中“整数”值返回“字符串”键的函数

如何比较数据框列中的字符串值和单元格中的值以创建基于多值字典的新数据框?

比较表中的字符串值

给定一个字符串值和一个时间戳,如何在BigQuery中创建一个基于年份的自定义分区表?

如何基于python中的字符串从列表中创建子列表?

基于字符串数组以字符串格式“<table> ...”创建html表

在 BigQuery 中基于字符串中的键值对创建列

如何在基于Java中字符串值的方法中返回不同类型?

如何基于python中的float值对列表中的元组字符串部分进行排序?

如何基于python pandas中不同行的字符串形式创建新列

如何在MATLAB中基于字符串创建具有动态名称的变量?

如何基于javascript中的字符串数组创建嵌套数组?

如何基于R中的多个条件为字符串创建if语句?

R:如何基于行字符串创建集群

创建基于逗号分隔的字符串在java中的json?

如何匹配字符串中的字符并从excel中的表中查找值