Close
Angular React Web Components Blazor Blazor
Premium

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.

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 IgbHierarchicalGrid.RowClasses input and define custom rules.

<IgbHierarchicalGrid AutoGenerate="true" Id="grid" Data="CustomersData" Name="grid" RowClassesScript="RowClassesHandler" @ref="grid">
</IgbHierarchicalGrid>

The IgbHierarchicalGrid.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 % 2 === 0
    };
}, true);
.activeRow {
    border-top: 2px solid #fc81b8;
    border-left: 3px solid #e41c77;
}

Demo

Using Row Styles

The IgbHierarchicalGrid control exposes the IgbHierarchicalGrid.RowStyles property which allows conditional styling of the data rows. Similar to IgbHierarchicalGrid.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 IgbHierarchicalGrid.RowStyles and IgbHierarchicalGrid.RowClasses is:

(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 IgbColumn input IgbColumn.CellClasses 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 IgbHierarchicalGrid cells by setting the IgbColumn IgbColumn.CellClasses input and define custom rules.

<IgbColumn Field="BeatsPerMinute" CellClassesScript="GrammyNominationsCellClassesHandler">

The IgbColumn.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 IgbColumn input IgbColumn.CellStyles` which accepts an object literal where the keys are style properties and the values are expressions for evaluation.

The callback signature for both cellStyles and cellClasses is 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 IgbColumn.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.