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.
3
Answers
You can simply do something like –
Or perhaps a better way would be to turn it into a component
Then –
Try this:
Example:
All you need to do is create a square using css and apply your
color
dynamically to the element as thebackground-color
In place of where you were displaying the color in the table, add a div which we will style as a square using css. Then set the background color to it like below.
In your css