如何根据元素的颜色十六进制值在mat-cell中设置按钮或图标颜色
How to set button or icon color in mat-cell based on the color Hexcode value of element
我在mat-cell里放了一个按钮,其实一个图标也可以,因为我的目的是在一个table中表示每条记录的颜色属性。每条记录都有自己的颜色值:名称、十六进制代码和前景,例如 'Red',值:'#ff1744',前景:'white'.
<ng-container matColumnDef="sku">
<mat-header-cell *matHeaderCellDef mat-sort-header>SKU</mat-header-cell>
<mat-cell *matCellDef="let element">{{element.sku}}</mat-cell>
</ng-container>
<ng-container matColumnDef="color">
<mat-header-cell *matHeaderCellDef mat-sort-header> Color </mat-header-cell>
<mat-cell *matCellDef="let element">
<button [disabled]="true"
[style.background-color]="element.value"
[style.color]="element.foreground"
class="color-preview">
</button>
</mat-cell>
</ng-container>
<ng-container matColumnDef="pictureUrl">......
要将按钮设置为圆形,请在 css 中进行如下设置。这确实符合预期。
.color-preview {
width: 20px;
height: 20px;
border-radius: 50%;
}
在上面,我似乎无法获得所需颜色的按钮,例如十六进制值为 #ff1744 的红色按钮。谁能帮忙?任何更正或解决方案将不胜感激。
当你使用 [disabled]="true"
时,我认为这会弄乱颜色。
改用 [disasbled]="false"
试试
当我尝试使用 Angular Material Mat Table https://material.angular.io/components/table/overview
示例和编辑 stalkblitz
以下代码有效。 (见屏幕截图)
https://xnnrglgqygj.angular.stackblitz.io/
在您的 HTML & Angular 组件中
<ng-container matColumnDef="position">
<th mat-header-cell *matHeaderCellDef> No. </th>
<td mat-cell *matCellDef="let element"> {{element.position}}
<button [disabled]="true"
[style.background-color]="element.color"
class="color-preview">
</button>
</td>
</ng-container>
const ELEMENT_DATA: PeriodicElement[] = [
{position: 1, name: 'Hydrogen', weight: 1.0079, symbol: 'H' , color : '#ff1744'},
{position: 2, name: 'Helium', weight: 4.0026, symbol: 'He' , color : '#fff000'},
{position: 3, name: 'Lithium', weight: 6.941, symbol: 'Li' , color : '#ff1744'},
{position: 4, name: 'Beryllium', weight: 9.0122, symbol: 'Be', color : '#fff000'},
{position: 5, name: 'Boron', weight: 10.811, symbol: 'B', color : '#ff1744'},
{position: 6, name: 'Carbon', weight: 12.0107, symbol: 'C', color : '#fff000'},
{position: 7, name: 'Nitrogen', weight: 14.0067, symbol: 'N', color : '#ff1744'},
{position: 8, name: 'Oxygen', weight: 15.9994, symbol: 'O', color : '#fff000'},
{position: 9, name: 'Fluorine', weight: 18.9984, symbol: 'F', color : '#ff1744'},
{position: 10, name: 'Neon', weight: 20.1797, symbol: 'Ne', color : '#fff000'},
];
我在mat-cell里放了一个按钮,其实一个图标也可以,因为我的目的是在一个table中表示每条记录的颜色属性。每条记录都有自己的颜色值:名称、十六进制代码和前景,例如 'Red',值:'#ff1744',前景:'white'.
<ng-container matColumnDef="sku">
<mat-header-cell *matHeaderCellDef mat-sort-header>SKU</mat-header-cell>
<mat-cell *matCellDef="let element">{{element.sku}}</mat-cell>
</ng-container>
<ng-container matColumnDef="color">
<mat-header-cell *matHeaderCellDef mat-sort-header> Color </mat-header-cell>
<mat-cell *matCellDef="let element">
<button [disabled]="true"
[style.background-color]="element.value"
[style.color]="element.foreground"
class="color-preview">
</button>
</mat-cell>
</ng-container>
<ng-container matColumnDef="pictureUrl">......
要将按钮设置为圆形,请在 css 中进行如下设置。这确实符合预期。
.color-preview {
width: 20px;
height: 20px;
border-radius: 50%;
}
在上面,我似乎无法获得所需颜色的按钮,例如十六进制值为 #ff1744 的红色按钮。谁能帮忙?任何更正或解决方案将不胜感激。
当你使用 [disabled]="true"
时,我认为这会弄乱颜色。
改用 [disasbled]="false"
试试
当我尝试使用 Angular Material Mat Table https://material.angular.io/components/table/overview 示例和编辑 stalkblitz
以下代码有效。 (见屏幕截图) https://xnnrglgqygj.angular.stackblitz.io/
在您的 HTML & Angular 组件中
<ng-container matColumnDef="position">
<th mat-header-cell *matHeaderCellDef> No. </th>
<td mat-cell *matCellDef="let element"> {{element.position}}
<button [disabled]="true"
[style.background-color]="element.color"
class="color-preview">
</button>
</td>
</ng-container>
const ELEMENT_DATA: PeriodicElement[] = [
{position: 1, name: 'Hydrogen', weight: 1.0079, symbol: 'H' , color : '#ff1744'},
{position: 2, name: 'Helium', weight: 4.0026, symbol: 'He' , color : '#fff000'},
{position: 3, name: 'Lithium', weight: 6.941, symbol: 'Li' , color : '#ff1744'},
{position: 4, name: 'Beryllium', weight: 9.0122, symbol: 'Be', color : '#fff000'},
{position: 5, name: 'Boron', weight: 10.811, symbol: 'B', color : '#ff1744'},
{position: 6, name: 'Carbon', weight: 12.0107, symbol: 'C', color : '#fff000'},
{position: 7, name: 'Nitrogen', weight: 14.0067, symbol: 'N', color : '#ff1744'},
{position: 8, name: 'Oxygen', weight: 15.9994, symbol: 'O', color : '#fff000'},
{position: 9, name: 'Fluorine', weight: 18.9984, symbol: 'F', color : '#ff1744'},
{position: 10, name: 'Neon', weight: 20.1797, symbol: 'Ne', color : '#fff000'},
];