typescript Angular Ng Xorro表列背景更改

chhqkbe1  于 2023-01-10  发布在  TypeScript
关注(0)|答案(1)|浏览(134)

我正在使用我的Angular 项目的antd**NG-ZORRO Table**,所以任何人知道知道如何添加表特殊颜色的背景色?
此处解释图像
这是我的代码

超文本标记语言

<nz-table #basicTable [nzData]="dataSet">
  <thead>
    <tr>
      <th>Name</th>
      <th>Age</th>
      <th>Address</th>
      <th>Action</th>
    </tr>
  </thead>
  <tbody>
    <tr *ngFor="let data of basicTable.data">
      <td>{{data.name}}</td>
      <td>{{data.age}}</td>
      <td>{{data.address}}</td>
      <td>
        <a>Action 一 {{data.name}}</a>
        <nz-divider nzType="vertical"></nz-divider>
        <a>Delete</a>
      </td>
    </tr>
  </tbody>
</nz-table>

.ts

export class NzDemoTableBasicComponent {
  listOfData: Person[] = [
    {
      key: '1',
      name: 'John Brown',
      age: 32,
      address: 'New York No. 1 Lake Park'
    },
    {
      key: '2',
      name: 'Jim Green',
      age: 42,
      address: 'London No. 1 Lake Park'
    },
    {
      key: '3',
      name: 'Joe Black',
      age: 32,
      address: 'Sidney No. 1 Lake Park'
    }
  ];
}
nbysray5

nbysray51#

如果你想动态地做它对象必须改变到这个

interface Person {
  key: string;
  name: { value: string; colored: boolean };
  age: { value: number; colored: boolean };
  address: { value: string; colored: boolean };
}

我为属性的值加上value,如果我们想要colored col,那么数据数组将如下所示

listOfData: Person[] = [
    {
      key: '1',
      name: { value: 'John Brown1', colored: true },
      age: { value: 32, colored: false },
      address: { value: 'New York No. 1 ', colored: false },
    },
    {
      key: '2',
      name: { value: 'John Brown2', colored: false },
      age: { value: 55, colored: false },
      address: { value: 'New York No. 2', colored: true },
    },
    {
      key: '3',
      name: { value: 'John Brown3', colored: false },
      age: { value: 18, colored: false },
      address: { value: 'New York No. 3', colored: false },
    },
  ];

和HTML代码就像这样

<nz-table #basicTable [nzData]="listOfData">
  <thead>
    <tr>
      <th>Name</th>
      <th>Age</th>
      <th>Address</th>
      <th>Action</th>
    </tr>
  </thead>
  <tbody>
    <tr *ngFor="let data of basicTable.data">
      <td  [style.background]="data.name.colored ? 'yellow' : ''">{{ data.name.value 
      }}</td>
      <td [style.background]="data.age.colored ? 'yellow' : ''">{{ data.age.value }} 
      </td>
      <td [style.background]="data.address.colored ? 'yellow' : ''">{{ 
       data.address.value }}</td>
      <td>
        <a>Action 一 {{ data.name.value }}</a>
        <nz-divider nzType="vertical"></nz-divider>
        <a>Delete</a>
      </td>
    </tr>
  </tbody>
</nz-table>

最后的结果会是这样的

您可以在stackblitz中查看here的代码

相关问题