Web Components 계층형 그리드에서 행 끌기

    The Ignite UI for Web Components Row Dragging feature in Web Components Hierarchical Grid is easily configurable and is used for rearranging rows within the grid by dragging and dropping them to a new position using the mouse. It is initialized on the root IgcHierarchicalGridComponent component and is configurable via the rowDraggable input.

    Web Components Hierarchical Grid Row Drag Example

    구성

    IgcHierarchicalGridComponent 드래그를 활성화하려면 그리드rowDraggabletrue로 설정하기만 하면 됩니다. 이 기능이 활성화되면 각 행에 행 드래그 핸들이 표시됩니다. 이 핸들은 행 드래그를 시작하는 데 사용할 수 있습니다. 드래그 핸들을 클릭하고 버튼을 누르고 있는 상태로 커서를 움직이 면 그리드의RowDragStart 이벤트가 발동합니다. 클릭을 언제RowDragEnd 든 놓으면 이벤트가 발동됩니다.

    <igc-hierarchical-grid row-draggable="true">
    </igc-hierarchical-grid>
    

    Templating the Drag Icon

    드래그 핸들 아이콘은 그리드dragIndicatorIconTemplate를 사용해 템플릿화할 수 있습니다. 우리가 만들고 있는 예시에서, 아이콘을 기본값인 drag_indicator에서 drag_handle로 바꿔봅시다.

    <igc-hierarchical-grid row-draggable="true" id="grid">
    </igc-hierarchical-grid>
    
    constructor() {
        var grid = this.grid = document.getElementById('grid') as IgcHierarchicalGridComponent;
        grid.dragIndicatorIconTemplate = this.dragIndicatorIconTemplate;
    }
    
    public dragIndicatorIconTemplate = (ctx: IgcGridEmptyTemplateContext) => {
        return html`<igc-icon name="drag_handle" collection="material"></igc-icon>`;
    }
    

    Application Demo

    Row Reordering Demo

    그리드의 행 드래그 이벤트를 사용하면 행을 드래그하여 재정렬할 수 있는 그리드를 생성할 수 있습니다.

    <igc-hierarchical-grid id="hGrid" row-draggable="true" primary-key="ID">
    </igc-hierarchical-grid>
    
    constructor() {
        var hGrid = this.grihGridd = document.getElementById('hGrid') as IgcHierarchicalGridComponent;
        hGrid.addEventListener("rowDragEnd", this.webHierarchicalGridReorderRowHandler)
    }
    

    [!Note] Make sure that there is a primaryKey specified for the grid! The logic needs an unique identifier for the rows so they can be properly reordered.

    rowDraggable활성화되고 드롭존이 정의되면, 드롭 이벤트에 대한 간단한 핸들러를 구현해야 합니다. 행을 드래그할 때는 다음 사항을 확인하세요:

    • 행이 확장되었나요? 그렇다면 접으세요.
    • 행이 그리드 내부에 삭제되었습니까?
    • 그렇다면 드래그 행이 다른 어느 행에 드롭되었나요?
    • 목표 행을 찾으면 레코드들의 위치를 배열에서data 바꿔 놓습니다
    • 행이 처음에 선택되었습니까? 그렇다면 선택됨으로 표시하세요.

    아래에서 이것이 구현된 것을 볼 수 있습니다.

    public webGridReorderRowHandler(args: CustomEvent<IgcRowDragEndEventArgs>): void {
        const ghostElement = args.detail.dragDirective.ghostElement;
        const dragElementPos = ghostElement.getBoundingClientRect();
        const grid = document.getElementsByTagName("igc-hierarchical-grid")[0] as any;
        const rows = Array.prototype.slice.call(document.getElementsByTagName("igx-grid-row"));
        const currRowIndex = this.getCurrentRowIndex(rows,
        { x: dragElementPos.x, y: dragElementPos.y });
        if (currRowIndex === -1) { return; }
        // remove the row that was dragged and place it onto its new location
        grid.deleteRow(args.detail.dragData.key);
        grid.data.splice(currRowIndex, 0, args.detail.dragData.data);
    }
    public getCurrentRowIndex(rowList: any[], cursorPosition) {
        for (const row of rowList) {
            const rowRect = row.getBoundingClientRect();
            if (cursorPosition.y > rowRect.top + window.scrollY && cursorPosition.y < rowRect.bottom + window.scrollY &&
                cursorPosition.x > rowRect.left + window.scrollX && cursorPosition.x < rowRect.right + window.scrollX) {
                // return the index of the targeted row
                return parseInt(row.attributes["data-rowindex"].value);
            }
        }
        return -1;
    }
    

    이러한 몇 가지 간단한 단계를 통해 드래그/드롭을 통해 행을 재정렬할 수 있는 그리드를 구성했습니다! 다음 데모에서 위 코드가 실제로 작동하는 모습을 볼 수 있습니다.

    또한 행 선택이 활성화되어 있으며 드래그된 행을 삭제할 때 선택이 유지됩니다.

    Limitations

    현재로서는 알려진 제한 사항이rowDraggable 없습니다.

    API References

    Additional Resources

    우리 커뮤니티는 활동적이며 항상 새로운 아이디어를 환영합니다.