Close
Angular React Web Components Blazor React
Premium

React Hierarchical Grid Conditional Styling

The Ignite UI for React Conditional Styling feature in React Hierarchical Grid allows custom styling on a row or cell level. The IgrHierarchicalGrid 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 IgrHierarchicalGrid component in Ignite UI for React 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 IgrHierarchicalGrid rows by setting the IgrHierarchicalGrid.rowClasses input and define custom rules.

<IgrHierarchicalGrid id="grid" height="600px" width="100%" rowClasses={rowClasses}>
</IgrHierarchicalGrid>

The IgrHierarchicalGrid.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.

const rowClasses = {
    activeRow: (row: IgrRowType) => row.index % 2 === 0
}
.activeRow {
    border-top: 2px solid #fc81b8;
    border-left: 3px solid #e41c77;
}

Demo

Using Row Styles

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

(row: IgrRowType) => boolean

Let’s define our styles:

const rowStyles = {
    background:(row: RowType) => row.data['HasGrammyAward'] ? '#eeddd3' : '#f0efeb',
    'border-left': (row: RowType) => row.data['HasGrammyAward'] ? '2px solid #dda15e' : null
};

const childRowStyles = {
    'border-left': (row: RowType) => row.data['BillboardReview'] > 70 ? '3.5px solid #dda15e' : null
};
<IgrHierarchicalGrid autoGenerate={true} rowStyles={rowStyles}
        height="580px" width="100%">
        <IgrRowIsland childDataKey="Albums" autoGenerate={true} rowStyles={childRowStyles}>
        </IgrRowIsland>
</IgrHierarchicalGrid>

Demo

Hierarchical Grid Conditional Cell Styling

Overview

The IgrHierarchicalGrid component in Ignite UI for React provides two ways to conditional styling of cells based on custom rules.

  • By setting the IgrColumn input IgrColumn.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 IgrHierarchicalGrid cells by setting the IgrColumn IgrColumn.cellClasses input and define custom rules.

<IgrColumn field="BeatsPerMinute" dataType="number" cellClasses={grammyNominationsCellClassesHandler}></IgrColumn>

The IgrColumn.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.

const grammyNominationsCellClassesHandler = {
    downFont: (rowData: any, columnKey: any): boolean => rowData[columnKey] < 5,
    upFont: (rowData: any, columnKey: any): boolean => rowData[columnKey] >= 6
};
.upFont {
    color: green !important;
}

.downFont {
    color: red !important;
}

Demo

  • By using the IgrColumn input IgrColumn.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:

Using Cell Styles

Columns expose the CellStyles property which allows conditional styling of the column cells. Similar to IgrColumn.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:

const cellStylesHandler = {
    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;
    }
}
<IgrColumn cellStyles={cellStylesHandler}></IgrColumn>

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.
const backgroundClasses = {
    myBackground: (rowData: any, columnKey: string) => {
        return rowData.Col2 < 10;
    }
};

const editDone = (event: IgrGridEditEventArgs) => {
    backgroundClasses = {...backgroundClasses};
}

<IgrHierarchicalGrid id="grid1" height="500px" width="100%" onCellEdit={editDone}>
  <IgrColumn id="Col1" field="Col1" dataType="number" cellClasses={backgroundClasses}></IgrColumn>
  <IgrColumn id="Col2" field="Col2" dataType="number" editable={true} cellClasses={backgroundClasses}></IgrColumn>
  <IgrColumn id="Col3" field="Col3" header="Col3" dataType="string" cellClasses={backgroundClasses}></IgrColumn>
</IgrHierarchicalGrid>

API References

Additional Resources

Our community is active and always welcoming to new ideas.