css - How to convert color code into actual color
问题描述
I'm using Angular material with Angular6 in my project. What I want to do is convert color codes which stored in the database into actual colors in my mat-table
.
Currently, my table is as follows,
This is how I'm getting visible columns data from my component.ts
file,
getIssueCategory() {
this.IssueService.getAllIssueCategories().subscribe(result => {
this.issueCategoryDTOList = result.reverse();
//updating data source
this.updatingDataSource(result);
this.IssueService.issueCategoryDTOList = result;
}
);
}
get visibleColumns() {
return this.displayedColumns.filter(column => column.visible).map(column => column.property);
}
applyFilter(filterValue: string) {
this.dataSource.filter = filterValue.trim().toLowerCase();
}
The color column in my HTML file is as follows,
<!-- color Column -->
<ng-container matColumnDef="color">
<mat-header-cell *matHeaderCellDef mat-sort-header="color" style="max-width: 25%; min-width: 25%;">COLOR
</mat-header-cell>
<mat-cell *matCellDef="let element" style="text-transform: uppercase; max-width: 25%; min-width: 25%;">
{{element.color}}
</mat-cell>
</ng-container>
Finally to getting understand easier I have designed my expected output using photoshop,
PS: In my database, the colors are stored as color codes.
解决方案
You can simply do something like -
<!-- color Column -->
<ng-container matColumnDef="color">
<mat-header-cell *matHeaderCellDef mat-sort-header="color" style="max-width: 25%; min-width: 25%;">COLOR
</mat-header-cell>
<mat-cell *matCellDef="let element" style="text-transform: uppercase; max-width: 25%; min-width: 25%;">
<div style="width: 15px; height: 15px" [style.background-color]="element.color">/div>
</mat-cell>
</ng-container>
Or perhaps a better way would be to turn it into a component
@Component({
selector: 'app-color-swatch',
template: `<div [style.background-color]="colorCode"></div>`,
styles: ['div { height: 15px; width: 15px }']
})
export class ColorSwatchComponent {
@Input colorCode: string;
}
Then -
<app-color-swatch [color]="element.color"></app-color-swatch>
推荐阅读
- php - Node js无法通过php从curl请求中读取数据
- mysql - 为什么在使用 go lang 执行 mysql DB 操作时,内部将 sql 转换为准备好的语句样式执行
- vb.net - 如何过滤包含列数据库中指定的所有单词的记录
- python - 如何利用字典计算数据框中的多个平均值
- javascript - 使用 switch case 和过滤/查找/循环对象数组来过滤并找到正确的对象以更新 JavaScript
- python - Python3如何从列表中检索列表
- shell - 在多个目录中执行`make`
- linux - 清漆 ESI 缓存 - 远程 url 返回 404
- reactjs - 在反应中将一些值从一页传递到另一页
- postgresql - Postgres lo_import 从其他表文本数据导入大对象