Estilo condicional de cuadrícula Blazor

    The Ignite UI for Blazor Conditional Styling feature in Blazor Grid allows custom styling on a row or cell level. The IgbGrid 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.

    Grid Conditional Row Styling

    The IgbGrid component in Ignite UI for Blazor provides two ways to conditional styling of rows based on custom rules.

    Más adelante en este tema los cubriremos ambos con más detalle.

    Using Row Classes

    You can conditionally style the IgbGrid rows by setting the RowClasses input and define custom rules.

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

    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 IgbGrid 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 and RowClasses is:

    (row) => boolean
    

    Definamos nuestros estilos:

    igRegisterScript("WebGridRowStylesHandler", () => {
        return {
            'background': (row) => (+row.data['Change'] < 0 && +row.data['AnnualChange'] < 0) ? '#FF000088' : '#00000000',
            'border': (row) => (+row.data['Change'] < 0 && +row.data['AnnualChange'] < 0) ? '2px solid' : '1px solid',
            'border-color': (row) => (+row.data['Change'] < 0 && +row.data['AnnualChange'] < 0) ? '#FF000099' : '#E9E9E9'
        };
    }, true);
    
    <IgbGrid AutoGenerate="true" Id="grid" Data="CustomersData" Name="grid" RowStylesScript="WebGridRowStylesHandler" @ref="grid">
    </IgbGrid>
    

    Demo

    Grid Conditional Cell Styling

    Descripción general

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

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

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

    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("CellClassesHandler", () => {
        return {
            downFont: (rowData, columnKey, cellValue, rowIndex) => rowData[columnKey] <= 95,
            upFont: (rowData, columnKey, cellValue, rowIndex) => rowData[columnKey] > 95
        };
    }, true);
    
    .upFont {
        color: green !important;
    }
    
    .downFont {
        color: red !important;
    }
    

    Demo

    • By using the IgbColumn input 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 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).

    Definamos nuestros estilos:

    igRegisterScript("WebGridCellStylesHandler", () => {
        return {
            background: (rowData, columnKey, cellValue, rowIndex) => rowIndex % 2 === 0 ? "#EFF4FD" : null,
            color: (rowData, columnKey, cellValue, rowIndex) => {
                if (columnKey === "Position") {
                    switch (cellValue) {
                        case "up": return "#28a745";
                        case "down": return "#dc3545";
                        case "current": return "#17a2b8"
                    }
                }
            }
        };
    }, true);
    
    <IgbColumn CellStylesScript="WebGridCellStylesHandler">
    </IgbColumn>
    

    Demo

    Known issues and limitations

    • Si hay celdas vinculadas a la misma condición (de diferentes columnas) y una celda se actualiza, las otras celdas no se actualizarán según el nuevo valor, si se cumple la condición.

    API References

    Additional Resources

    Nuestra comunidad es activa y siempre da la bienvenida a nuevas ideas.