i have disabled several columns in my table and want to display them in different colors. Therefore I wrote a function in Typescript where CSS is changed via the class .disabledRange. I have read that you can solve it with [ngClass]... I have an array with three columns: first, second, third. How can I leave the columns disabled and still style them in different colors? In my case I don't know how to use it in the most useful way. I would be grateful for help :)
My code:
// Template
<td mat-cell *matCellDef [class.disabledRange]="column.disabledRange">
...
</td>
// Array
private displayedColumns: EditColumns[] = [
{ attribute: 'first', name: 'MyFirstCol', object: null, disabledRange: false },
{ attribute: 'second', name: 'MySecondCol', object: null, disabledRange: false },
{ attribute: 'third', name: 'MyThirdCol', object: null, disabledRange: false }
];
// Set columns disabled
private disabledColumns(attributeName: string) {
if (attributeName) {
const displayedColumn = this.displayedColumns.find((c) => c.attribute === first);
if (displayedColumn) {
displayedColumn.disabledRange = !displayedColumn.disabledRange;
const columnIndex = this.columns.findIndex((c) => c === attributeName);
}
}
ngAfterViewInit() {
// To disabled columns
this.disabledColumns('first');
this.disabledColumns('second');
this.disabledColumns('third');
}
// Style
// When ranges are disabled in the data table
.disabledRange {
background-color: #eae9e9;
color: #000000;
}
you can use together [class.disabledRange]="condition" and [ngClass], e.g. if you has some like
You can use
See, stackblitz, for more about ngClass see the docs
Updated if we can change the "class" of the rows we can use the tag "tr mat-row.
Imagine some data like:
Of course we can also changing the td., we need use "some variable of the row". We can has a property of our "data",
You can write
We can also use the "index of the row", e.g.
NOTE: If only want to change the background, we can use