Blazor Tree Grid Cell Editing

    The Ignite UI for Blazor Cell Editing in Blazor Tree Grid provides a great data manipulation capability of the content of individual cells within the Blazor Tree Grid component and comes with powerful API for React CRUD operations. It is a fundamental feature in apps like spreadsheets, data tables, and data grids, allowing users to add, edit, or update data within specific cells. By default, the Grid in Ignite UI for Blazor is used in cell editing. And due to the default cell editing template, there will be different editors based on the column data type Top of Form.

    In addition, you can define your own custom templates for update-data actions and to override the default behavior for committing and discarding any changes.

    Blazor Tree Grid Cell Editing and Edit Templates Example

    Cell Editing

    Editing through UI

    You can enter edit mode for specific cell, when an editable cell is focused in one of the following ways:

    • on double click;
    • on single click - Single click will enter edit mode only if the previously selected cell was in edit mode and currently selected cell is editable. If the previously selected cell was not in edit mode, single click will select the cell without entering edit mode;
    • on key press Enter;
    • on key press F2;

    You can exit edit mode without committing the changes in one of the following ways:

    • on key press Escape;
    • when you perform sorting, filtering, searching and hiding operations;

    You can exit edit mode and commit the changes in one of the following ways:

    • on key press Enter;
    • on key press F2;
    • on key press Tab;
    • on single click to another cell - when you click on another cell in the IgbTreeGrid, your changes will be submitted.
    • operations like paging, resize, pin or move will exit edit mode and changes will be submitted.

    [!Note] The cell remains in edit mode when you scroll vertically or horizontally or click outside the IgbTreeGrid. This is valid for both cell editing and row editing.

    Editing through API

    You can also modify the cell value through the IgbTreeGrid API but only if primary key is defined:

    @code {
        this.treeGrid.UpdateCell(newValue, rowID, 'ReorderLevel');
    }
    

    Another way to update cell is directly through Update method of Cell:

    @code {
        private UpdateCell() {
            IgbCell cell = this.treeGrid.GetCellByColumn(rowIndex, "Age");
            cell.Update(9999);
        }
    }
    

    Cell Editing Templates

    You can see and learn more for default cell editing templates in the general editing topic.

    If you want to provide a custom template which will be applied to a cell, you can pass such template either to the cell itself, or to its header. First create the column as you usually would:

    <IgbColumn
        Field="Category"
        DataType="GridColumnDataType.String"
        InlineEditorTemplateScript="WebGridCellEditCellTemplate"
        Editable="true"
        Name="column1"
        @ref="column1">
    </IgbColumn>
    

    and pass the template:

    *** In JavaScript ***
    
    igRegisterScript("WebGridCellEditCellTemplate", (ctx) => {
        let cellValues = [];
        let uniqueValues = [];
        for(const i of ctx.cell.grid.data){
            const field = ctx.cell.column.field;
            if(uniqueValues.indexOf(i[field]) === -1 )
            {
                cellValues.push(html`<igc-select-item value=${i[field]}>${(i[field])}</igc-select-item>`);
                uniqueValues.push(i[field]);
            }
        }
        return html`<div>
        <igc-select position-strategy="fixed" @igcChange=${ e => ctx.cell.editValue = e.detail.value}>
              ${cellValues}
        </igc-select>
    </div>`;
    }, false);
    

    Working sample of the above can be found here for further reference:

    CRUD operations

    [!Note] Please keep in mind that when you perform some CRUD operation all of the applied pipes like filtering, sorting and grouping will be re-applied and your view will be automatically updated.

    The IgbTreeGrid provides a straightforward API for basic CRUD operations.

    Adding a new record

    The IgbTreeGrid component exposes the AddRow method which will add the provided data to the data source itself.

    //Assuming we have a `GetNewRecord` method returning the new row data.
    const record = this.GetNewRecord();
    this.TreeGridRef.AddRow(record);
    

    Updating data in the Tree Grid

    Updating data in the Tree Grid is achieved through UpdateRow and UpdateCell methods but only if the PrimaryKey for the grid is defined. You can also directly update a cell and/or a row value through their respective update methods.

    @code {
        // Updating the whole row
        this.treeGrid.UpdateRow(newData, this.selectedCell.cellID.rowID);
    
        // Just a particular cell through the Tree Grid API
        this.treeGrid.UpdateCell(newData, this.selectedCell.cellID.rowID, this.selectedCell.column.field);
    
        // Directly using the cell `update` method
        this.selectedCell.Update(newData);
    
        // Directly using the row `update` method
        IgbRowType row = this.treeGrid.GetRowByKey(rowID);
        row.Update(newData);
    }
    

    Deleting data from the Tree Grid

    Please keep in mind that DeleteRow method will remove the specified row only if a PrimaryKey is defined.

    @code {
        // Delete row through Tree Grid API
        this.treeGrid.DeleteRow(this.selectedCell.cellID.rowID);
        // Delete row through row object
        IgbRowType row = this.treeGrid.GetRowByIndex(rowIndex);
        row.Del();
    }
    

    Cell Validation on Edit Event

    Using the IgbTreeGrid's editing events, we can alter how the user interacts with the IgbTreeGrid.

    In this example, we'll validate a cell based on the data entered in it by binding to the CellEdit event. If the new value of the cell does not meet our predefined criteria, we'll prevent it from reaching the data source by cancelling the event.

    The first thing we need to do is bind to the grid's event:

    <IgbTreeGrid CellEditScript="HandleCellEdit" />
    

    The CellEdit emits whenever any cell's value is about to be committed. In our CellEdit definition, we need to make sure that we check for our specific column before taking any action:

    *** In JavaScript ***
    igRegisterScript("HandleCellEdit", (ev) => {
        const column = event.detail.column;
    
    	if (column.field === 'Age') {
    		if (event.detail.newValue < 18) {
    			event.detail.cancel = true;
    			alert('Employees must be at least 18 years old!');
    		}
    	} else if (column.field === 'HireDate') {
    		if (event.detail.newValue > new Date().getTime()) {
    			event.detail.cancel = true;
    			alert('The employee hire date must be in the past!');
    		}
    	}
    }, false);
    

    If the value entered in a cell under the Age column is below 18 or the value in the HireDate column is in the future, the editing will be cancelled and the user will be alerted to the cancellation.

    The result of the above validation being applied to our IgbTreeGrid can be seen in the below demo:

    Styling

    In addition to the predefined themes, the grid could be further customized by setting some of the available CSS properties. In case you would like to change some of the colors, you need to set a class for the grid first:

    <IgbTreeGrid Class="treeGrid"></IgbTreeGrid>
    

    Then set the related CSS properties for that class:

    .treeGrid {
        --ig-grid-edit-mode-color: orange;
        --ig-grid-cell-editing-background: lightblue;
    }
    

    Styling Example

    API References

    Additional Resources