Blazor Hierarchical Grid Conditional Styling
The Ignite UI for Blazor Conditional Styling feature in Blazor Hierarchical Grid allows custom styling on a row or cell level. The IgbHierarchicalGrid 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.
Hierarchical Grid Conditional Row Styling
The IgbHierarchicalGrid component in Ignite UI for Blazor provides two ways to conditional styling of rows based on custom rules.
- By setting
RowClassesinput on theIgbHierarchicalGridcomponent; - By setting
RowStylesinput on theIgbHierarchicalGridcomponent;
Further in this topic we will cover both of them in more details.
Using Row Classes
You can conditionally style the IgbHierarchicalGrid rows by setting the RowClasses input and define custom rules.
<IgbHierarchicalGrid AutoGenerate="true" Id="grid" Data="CustomersData" Name="grid" RowClassesScript="RowClassesHandler" @ref="grid">
</IgbHierarchicalGrid>
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.
igRegisterScript("RowClassesHandler", () => {
return {
activeRow: (row) => row.index === 0
};
}, true);
.activeRow {
border: 2px solid #fc81b8;
border-left: 3px solid #e41c77;
}
Demo
Using Row Styles
The IgbHierarchicalGrid 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
RowStylesandRowClassesis:
(row) => boolean
Let's define our styles:
igRegisterScript("WebGridRowStylesHandler", () => {
return {
background:(row: RowType) => row.data['HasGrammyAward'] ? '#eeddd3' : '#f0efeb',
'border-left': (row: RowType) => row.data['HasGrammyAward'] ? '2px solid #dda15e' : null
};
}, true);
igRegisterScript("WebGridChildRowStylesHandler", () => {
return {
'border-left': (row: RowType) => row.data['BillboardReview'] > 70 ? '3.5px solid #dda15e' : null
};
}, true);
<IgbHierarchicalGrid AutoGenerate="true" RowStylesScript="WebGridRowStylesHandler"
Height="580px" Width="100%">
<IgbRowIsland ChildDataKey="Albums" AutoGenerate="true" RowStylesScript="WebGridChildRowStylesHandler">
</IgbRowIsland>
</IgbHierarchicalGrid>
Demo
Hierarchical Grid Conditional Cell Styling
Overview
The IgbHierarchicalGrid component in Ignite UI for Blazor provides two ways to conditional styling of cells based on custom rules.
- By setting the
IgbColumninputCellClassesto 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 IgbHierarchicalGrid cells by setting the IgbColumn CellClasses input and define custom rules.
<IgbColumn Field="BeatsPerMinute" CellClassesScript="GrammyNominationsCellClassesHandler">
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.
igRegisterScript("GrammyNominationsCellClassesHandler", () => {
return {
downFont: (rowData, columnKey) => rowData[columnKey] < 5,
upFont: (rowData, columnKey) => rowData[columnKey] >= 6
};
}, true);
.upFont {
color: green !important;
}
.downFont {
color: red !important;
}
Demo
- By using the
IgbColumninputCellStyleswhich accepts an object literal where the keys are style properties and the values are expressions for evaluation.
The callback signature for both
cellStylesandcellClassesis now changed to:
(rowData, columnKey, cellValue, rowIndex) => 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:
igRegisterScript("CellStylesHandler", () => {
return {
background: (rowData, columnKey, cellValue, rowIndex) => rowIndex % 2 === 0 ? "#EFF4FD" : null,
color: (rowData, columnKey, cellValue, rowIndex) => {
if (columnKey === "Debut") {
return cellValue > 2000 ? "#28a745" : "#dc3545";
}
return undefined;
}
};
}, true);
<IgbColumn CellStylesScript="CellStylesHandler">
</IgbColumn>
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.
API References
Additional Resources
Our community is active and always welcoming to new ideas.