【问题标题】:How to set button or icon color in mat-cell based on the color Hexcode value of element如何根据元素的颜色Hexcode值在mat-cell中设置按钮或图标颜色
【发布时间】:2021-11-04 09:02:18
【问题描述】:

我在mat-cell中放了一个按钮,其实一个图标也可以,因为我的目的是指示表格中每条记录的颜色属性。每条记录都有自己的颜色值:名称、十六进制代码和前景,例如,'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 的红色按钮。任何人都可以帮忙吗?任何更正或解决方案将不胜感激。

【问题讨论】:

    标签: html css angular colors angular-material


    【解决方案1】:

    当我尝试使用 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'},
        ];

    【讨论】:

    • 谢谢高拉夫!是您的确认让我有信心重新运行我的代码。我的也可以。早些时候,我的数据有问题。而我所拥有的前景确实毫无用处。
    【解决方案2】:

    当您使用 [disabled]="true" 时,我认为这会与颜色混淆。

    改用[disasbled]="false" 试试

    【讨论】:

    • 无论将其更改为“false”还是完全删除 [disabled]="true" 仍然无法正常工作。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-03-20
    • 2019-03-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-09
    相关资源
    最近更新 更多