Tree Grid Column Reordering & Moving

    The Web Components Tree Grid Column Moving feature in Ignite UI for Web Components allows quick and easy column reordering. This can be done through the Column Moving API or by dragging and dropping the headers to another position via mouse or touch gestures. In the Web Components Tree Grid, you can enable Column Moving for pinned and unpinned columns and for Multi-Column Headers as well.

    Reordering between columns and column groups is allowed only when they are at the same level in the hierarchy and both are in the same group. Moving is allowed between columns/column-groups, if they are top level columns.

    If a column header is templated and the Column Moving is enabled or the corresponding column is groupable, then the templated elements need to have the draggable attribute set to false!

    If the pinned area exceeds its maximum allowed width (80% of the total IgcTreeGridComponent width), a visual clue notifies the end user that the drop operation is forbidden and pinning is not possible. This means you won't be allowed to drop a column in the pinned area.

    public headerTemplate = (ctx: IgcCellTemplateContext) => {
        return html`
            <igc-icon draggable="false" @click="${() => this.onClick()}"></igc-icon>
        `;
    }
    ts

    Web Components Tree Grid Column Moving Overview Example

    EXAMPLE
    DATA
    TS
    HTML
    CSS

    Like this sample? Get access to our complete Ignite UI for Web Components toolkit and start building your own apps in minutes. Download it for free.

    Overview

    Column moving feature is enabled on a per-grid level, meaning that the IgcTreeGridComponent could have either movable or immovable columns. This is done via the moving input of the IgcTreeGridComponent.

    Ignite UI for Web Components | CTA Banner

    API

    In addition to the drag and drop functionality, the Column Moving feature also provides API methods to allow moving a column/reordering columns programmatically:

    moveColumn - Moves a column before or after another column (a target). The first parameter is the column to be moved, and the second parameter is the target column. Also accepts an optional third parameter Position (representing a DropPosition value), which determines whether to place the column before or after the target column.

    // Move the ID column after the Name column
    const idColumn = grid.getColumnByName("ID");
    const nameColumn = grid.getColumnByName("Name");
    
    grid.moveColumn(idColumn, nameColumn, DropPosition.AfterDropTarget);
    typescript

    move - Moves a column to a specified visible index. If the passed index parameter is invalid (is negative, or exceeds the number of columns), or if the column is not allowed to move to this index (if inside another group), no operation is performed.

    // Move the ID column at 3rd position.
    const idColumn = grid.getColumnByName("ID");
    idColumn.move(3);
    typescript

    Note that when using the column moving feature, the ColumnMovingEnd event will be emitted if the operation was successful. Also note that in comparison to the drag and drop functionality, using the column moving feature does not require setting the moving property to true.

    Events

    There are several events related to the column moving to provide a means for tapping into the columns' drag and drop operations. These are ColumnMovingStart, ColumnMoving and ColumnMovingEnd.

    You can subscribe to the ColumnMovingEnd event of the IgcTreeGridComponent to implement some custom logic when a column is dropped to a new position. For example, you can cancel dropping the Category column after the Change On Year(%) column in the following code snippet.

    <igc-tree-grid id="dataGrid" auto-generate="false" moving="true">
        <igc-column field="Category"></igc-column>
        <igc-column field="Change On Year(%)" data-type="Number" ></igc-column>
    </igc-tree-grid>
    html
    constructor() {
        var dataGrid = this.dataGrid = document.getElementById('dataGrid') as IgcTreeGridComponent;
        dataGrid.data = this.data;
        dataGrid.addEventListener("columnMovingEnd", this.onColumnMovingEnd);
    }
    typescript
    public onColumnMovingEnd(event) {
        if (event.detail.source.field === "Category" && event.detail.target.field === "Change On Year(%)") {
            event.detail.cancel = true;
        }
    }
    typescript

    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:

    <igc-tree-grid class="grid"></igc-tree-grid>
    html

    Then set the related CSS properties to this class:

    .grid {
        --ig-grid-ghost-header-text-color: #f4d45c;
        --ig-grid-ghost-header-background: #ad9d9d;
        --ig-grid-ghost-header-icon-color: #f4d45c;
    }
    css

    Demo

    EXAMPLE
    DATA
    TS
    HTML
    CSS

    API References

    Additional Resources

    Our community is active and always welcoming to new ideas.