Web Components Tree Grid Conditional Styling
The Ignite UI for Web Components Conditional Styling feature in Web Components Tree Grid allows custom styling on a row or cell level. The IgcTreeGridComponent
Conditional Styling functionality is used to visually emphasize or highlight data that meets certain criteria, making it easier for users to identify important information or trends within the grid.
Tree Grid Conditional Row Styling
The IgcTreeGridComponent
component in Ignite UI for Web Components provides two ways to conditional styling of rows based on custom rules.
- By setting
rowClasses
input on theIgcTreeGridComponent
component; - By setting
rowStyles
input on theIgcTreeGridComponent
component;
Further in this topic we will cover both of them in more details.
Using Row Classes
You can conditionally style the IgcTreeGridComponent
rows by setting the rowClasses
input and define custom rules.
<igc-tree-grid id="grid" height="600px" width="100%">
</igc-tree-grid>
constructor() {
var grid = this.grid = document.getElementById('grid') as IgcTreeGrid;
grid.rowClasses = this.rowClasses;
}
The rowClasses
input accepts an object literal, containing key-value pairs, where the key is the name of the CSS class, while the value is either a callback function that returns a boolean, or boolean value.
public rowClasses = {
activeRow: (row: IgcRowType) => row.index === 0
}
.activeRow {
border: 2px solid #fc81b8;
border-left: 3px solid #e41c77;
}
Demo
Using Row Styles
The IgcTreeGridComponent
control exposes the rowStyles
property which allows conditional styling of the data rows. Similar to rowClasses
it accepts an object literal where the keys are style properties and the values are expressions for evaluation. Also, you can apply regular styling (without any conditions).
The callback signature for both
rowStyles
androwClasses
is:
(row: IgcRowType) => boolean
Let's define our styles:
public rowStyles = {
'background': (row: IgcRowType) => row.data['Title'] === 'CEO' ? '#6c757d' :
row.data['Title'].includes('President') ? '#adb5bd' :
row.data['Title'].includes('Director') ? '#ced4da' :
row.data['Title'].includes('Manager') ? '#dee2e6' :
row.data['Title'].includes('Lead') ? '#e9ecef' :
row.data['Title'].includes('Senior') ? '#f8f9fa' : null,
'border-left': (row: IgcRowType) => row.data.data['Title'] === 'CEO' || row.data.data['Title'].includes('President') ?
'2px solid' : null,
'border-color': (row: IgcRowType) => row.data.data['Title'] === 'CEO' ? '#495057' : null,
color: (row: IgcRowType) => row.data.data['Title'] === 'CEO' ? '#fff' : null
};
<igc-tree-grid id="treeGrid" moving="true" primary-key="ID" foreign-key="ParentID"
width="100%" height="550px">
</igc-tree-grid>
constructor() {
var treeGrid = this.treeGrid = document.getElementById('treeGrid') as IgcTreeGridComponent;
treeGrid.rowStyles = this.rowStyles;
}
Demo
Tree Grid Conditional Cell Styling
Overview
The IgcTreeGridComponent
component in Ignite UI for Web Components provides two ways to conditional styling of cells based on custom rules.
- By setting the
IgcColumnComponent
inputcellClasses
to an object literal containing key-value pairs. The key is the name of the CSS class, while the value is either a callback function that returns a boolean, or boolean value. The result is a convenient material styling of the cell.
Using Cell Classes
You can conditionally style the IgcTreeGridComponent
cells by setting the IgcColumnComponent
cellClasses
input and define custom rules.
<igc-column id="unitPrice" field="UnitPrice" header="Unit Price" data-type="currency"></igc-column>
constructor() {
var unitPrice = this.UnitPrice = document.getElementById('unitPrice') as IgcColumnComponent;
unitPrice.cellClasses = this.unitPriceCellClasses;
}
The cellClasses
input accepts an object literal, containing key-value pairs, where the key is the name of the CSS class, while the value is either a callback function that returns a boolean, or boolean value.
private downPriceCondition = (rowData: any, columnKey: any): boolean => {
return rowData[columnKey] <= 5;
}
private upPriceCondition = (rowData: any, columnKey: any): boolean => {
return rowData[columnKey] > 5;
}
public unitPriceCellClasses = {
downPrice: this.downPriceCondition,
upPrice: this.upPriceCondition
};
.upPrice {
color: red !important;
}
.downPrice {
color: green !important;
}
Demo
- By using the
IgcColumnComponent
inputcellStyles
which accepts an object literal where the keys are style properties and the values are expressions for evaluation.
The callback signature for both
cellStyles
andcellClasses
is now changed to:
(rowData: any, columnKey: string, cellValue: any, rowIndex: number) => boolean
Using Cell Styles
Columns expose the cellStyles
property which allows conditional styling of the column cells. Similar to cellClasses
it accepts an object literal where the keys are style properties and the values are expressions for evaluation. Also, you can apply regular styling with ease (without any conditions).
Let's define our styles:
public webTreeGridCellStylesHandler = {
background: (rowData, columnKey, cellValue, rowIndex) => rowIndex % 2 === 0 ? "#EFF4FD" : null,
color: (rowData, columnKey, cellValue, rowIndex) => {
if (columnKey === "UnitPrice") {
if (cellValue > 10) return "#dc3545";
if (cellValue < 5) return "#28a745";
if (cellValue >= 5 && cellValue <= 10) return "#17a2b8";
}
}
}
<igc-column id="col1">
</igc-column>
constructor() {
var col1 = document.getElementById('col1') as IgcColumnComponent;
col1.cellStyles = this.webTreeGridCellStylesHandler;
}
Demo
Known issues and limitations
- If there are cells bind to the same condition (from different columns) and one cell is updated, the other cells won't be updated based on the new value, if the condition is met.
A check should be performed in order to apply the changes to the rest of the cells. The example below shows how to do that.
public backgroundClasses = {
myBackground: (rowData: any, columnKey: string) => {
return rowData.Col2 < 10;
}
};
public editDone(evt) {
this.Col1.cellClasses = {...this.backgroundClasses};
}
<igc-tree-grid id="grid1" height="500px" width="100%" >
<igc-column id="Col1" field="Col1" data-type="number"></igx-column>
<igc-column id="Col2" field="Col2" data-type="number" editable="true"></igx-column>
<igc-column id="Col3" field="Col3" header="Col3" data-type="string"></igx-column>
<igc-tree-grid>
constructor() {
var grid = this.grid = document.getElementById('grid1') as IgcTreeGrid;
var Col1 = this.Col1 = document.getElementById('Col1') as IgcColumnComponent;
var Col2 = this.Col2 = document.getElementById('Col2') as IgcColumnComponent;
var Col3 = this.Col3 = document.getElementById('Col3') as IgcColumnComponent;
grid.data = this.data;
grid.onCellEdit = this.editDone;
Col1.cellClasses = this.backgroundClasses;
Col2.cellClasses = this.backgroundClasses;
Col3.cellClasses = this.backgroundClasses;
}
API References
Additional Resources
Our community is active and always welcoming to new ideas.