简体   繁体   English

使用Angular 4为html表中的每个行索引设置不同的颜色

[英]Set different colors each row index in html table using Angular 4

I would like to set different colors for selected rows programatically each time when a button outside the table is clicked. 每次单击表格外部的按钮时,我想以编程方式为选定的行设置不同的颜色。 I am using the latest version of angular. 我正在使用最新版本的angular。

I know how to set row color with css but do not know how to influence the css class programatically in typescript. 我知道如何使用CSS设置行颜色,但不知道如何在打字稿中以编程方式影响CSS类。

.ui-table .ui-table-tbody > tr:nth-child(1) {
    background-color: #2DA449; 
}

Is there some way to edit the css class to set the child index and b-color in typescript? 有什么方法可以编辑CSS类以在打字稿中设置子索引和b-color?

Typescript 打字稿

export class MyComponent{
    items:any[];
    changeColors(){
    // I'm just selecting the first row for this example but you have to define your own logic 
    this.items[0].color ="#ddd";
    }
}

HTML 的HTML

<button (click)="changeColors()">Change colors<button/>
<table>
  <tr *ngFor="let item in items;let i=index" [style.color]="item.color">
    ...
  </tr>
</table>

More documention can be found https://angular.io/guide/template-syntax#binding-targets 可以找到更多文档https://angular.io/guide/template-syntax#binding-targets

All you need is ngClass : 您需要的只是ngClass

<td [ngClass]="{'selected': checkSelected(i)}"> // checkSelected should return boolean

WORKING DEMO 工作演示

暂无
暂无

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

相关问题 在表单元格div上为角材料中的每一行设置z索引 - Set z index on table cell div for each row in angular material 如何为每个选定的行使用不同的颜色 int Primeng 表 - How to use different colors for each selected row int Primeng table 如何以角度的方式组成html表的每一行 - how to componentize each row of an html table in angular 如何在 agular 中使用 ngClass 在行的每一列上显示不同的颜色? - how to display different colors on each column of a row using ngClass in agular? 如何使用 Angular 中的 ngModel 在表格的每一行中绑定不同的输入文本值 - How to bind different input text value in each row in a Table using ngModel in Angular 在一个 HTML 表中使用 Angular,每行都有一个打开模态显示的按钮,它总是显示元素列表的第一个 - In a HTML table using Angular, and a button to open a modal displays on each row, it always displays the first of the element list 将不同的边框颜色应用于颜色集中的每个垫子数据表行 - Applying different border color to each mat datatable row from the set of colors 在 angular 材料表中的每一行动态添加一个 html 按钮 - Add a html button to each row dynamically in angular material table 使用 angular 中的索引保存表的特定行的数据 - save data of a particular row of a table using index in angular 通过使用索引值动态更改Angular 6材料特定表行 - Change Angular 6 material specific table row dynamically by using index value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM