Blazor 그리드 행 선택

    The Ignite UI for Blazor Row Selection feature in Blazor Grid allows users to interactively select, highlight, or deselect a single or multiple rows of data. There are several selection modes available in the IgbGrid:

    • 없음 선택
    • 다중 선택
    • 단일 선택

    Blazor 행 선택 예제

    The sample below demonstrates the three types of IgbGrid's row selection behavior. Use the drop-down below to enable each of the available selection modes. Use the checkbox to hide or show the row selector checkboxes.

    설정

    In order to setup row selection in the IgbGrid, you just need to set the RowSelection property. This property accepts GridSelectionMode enumeration.

    GridSelectionMode exposes the following modes:

    • 없음
    • 하나의
    • 다수의

    아래에서는 각각에 대해 더 자세히 살펴보겠습니다.

    없음 선택

    In the IgbGrid by default row selection is disabled (RowSelection is None). So you can not select or deselect a row through interaction with the IgbGrid UI, the only way to complete these actions is to use the provided API methods.

    단일 선택

    Single row selection can now be easily set up, the only thing you need to do, is to set RowSelection to Single property. This gives you the opportunity to select only one row within a grid. You can select a row by clicking on a cell or pressing the SPACE key when you focus on a cell of the row, and of course you can select a row by clicking on the row selector field. When row is selected or deselected RowSelectionChanging event is emitted.

        <IgbGrid Width="100%"
                 Id="grid"
                 Height="100%"
                 RowSelection=GridSelectionMode.Single
                 PrimaryKey="Key"
                 @ref=Grid
                 AutoGenerate=true
                 RowSelectionChanging='RowSelectionChanging'
                 Data=northwindEmployees>
        </IgbGrid>
    @code {
        private void RowSelectionChanging(IgbRowSelectionEventArgs args)
        {
            // handler for selection change
        }
    }
    

    다중 선택

    To enable multiple row selection in the IgbGrid just set the RowSelection property to Multiple. This will enable a row selector field on each row and in the IgbGrid header. The row selector allows users to select multiple rows, with the selection persisting through scrolling, paging, and data operations, such as sorting and filtering. The row also can be selected by clicking on a cell or by pressing the SPACE key when a cell is focused. If you have selected one row and click on another while holding the SHIFT key, this will select the whole range of rows. In this selection mode, when you click on a single row, the previous selected rows will be deselected. If you click while holding the CTRL key, the row will be toggled and the previous selection will be preserved.

        <IgbGrid Width="100%"
                 Id="grid"
                 Height="100%"
                 RowSelection=GridSelectionMode.Multiple
                 PrimaryKey="Key"
                 @ref=Grid
                 AutoGenerate=true
                 Data=northwindEmployees>
        </IgbGrid>
    

    노트

    • Row selection will trigger RowSelectionChanging event. This event gives you information about the new selection, old selection, the rows that have been added and removed from the old selection. Also the event is cancellable, so this allows you to prevent selection.
    • When row selection is enabled row selectors are displayed, but if you don't want to show them, you can set HideRowSelectors to true.
    • 런타임 시 행 선택 모드 간에 전환하면 이전 행 선택 상태가 지워집니다.

    API 사용

    프로그래밍 방식으로 행 선택

    The code snippet below can be used to select one or multiple rows simultaneously (via PrimaryKey). Additionally, the second parameter of this method is a boolean property through which you may choose whether the previous row selection will be cleared or not. The previous selection is preserved by default.

        <IgbGrid Width="100%"
                 Id="grid"
                 Height="100%"
                 RowSelection=GridSelectionMode.Multiple
                 PrimaryKey="Key"
                 @ref=grid
                 AutoGenerate=true
                 Data=northwindEmployees>
        </IgbGrid>
        <IgbButton @onclick=Select>Select</IgbButton>
        @code {
            public IgbGrid grid;
            private async void Select()
            {
                object[] array = new object[] { 1,2, 5 };
                await this.grid.SelectRowsAsync(array, true);
            }
        }
    

    This will add the rows which correspond to the data entries with IDs 1, 2 and 5 to the IgbGrid selection.

    행 선택 취소

    If you need to deselect rows programmatically, you can use the DeselectRows method.

        <IgbGrid Width="100%"
                 Id="grid"
                 Height="100%"
                 RowSelection=GridSelectionMode.Multiple
                 PrimaryKey="Key"
                 @ref=grid
                 AutoGenerate=true
                 Data=northwindEmployees>
        </IgbGrid>
        <IgbButton @onclick=Deselect>Deselect</IgbButton>
        @code {
            public IgbGrid grid;
            private async void Deselect()
            {
                object[] array = new object[] { 1, 2, 5 };
                await this.grid.DeselectRowsAsync(array);
            }
        }
    

    행 선택 이벤트

    When there is some change in the row selection RowSelectionChanging event is emitted. RowSelectionChanging exposes the following arguments:

    • OldSelection - array of row IDs that contains the previous state of the row selection.
    • NewSelection - array of row IDs that match the new state of the row selection.
    • Added - array of row IDs that are currently added to the selection.
    • Removed - array of row IDs that are currently removed according old selection state.
    • Event- 행 선택 변경을 유발한 원래 이벤트.
    • Cancel- 행 선택 변경을 방지할 수 있습니다.
        <IgbGrid Width="100%"
                 Id="grid"
                 Height="100%"
                 RowSelection=GridSelectionMode.Multiple
                 PrimaryKey="Key"
                 @ref=Grid
                 AutoGenerate=true
                 RowSelectionChanging='RowSelectionChanging'
                 Data=northwindEmployees>
        </IgbGrid>
    @code {
        private void RowSelectionChanging(IgbRowSelectionEventArgs args)
        {
            // handler
        }
    }
    

    모든 행 선택

    Another useful API method that IgbGrid provides is SelectAllRows. By default this method will select all data rows, but if filtering is applied, it will select only the rows that match the filter criteria. If you call the method with false parameter, SelectAllRows(false) will always select all data in the grid, even if filtering is applied.

    Note Keep in mind that SelectAllRows will not select the rows that are deleted.

    모든 행 선택 취소

    IgbGrid provides a DeselectAllRows method, which by default will deselect all data rows, but if filtering is applied will deselect only the rows that match the filter criteria. If you call the method with false parameter, DeselectAllRows(false) will always clear all row selection state even if filtering is applied.

    선택한 행을 가져오는 방법

    If you need to see which rows are currently selected, you can get their row IDs with the SelectedRows getter.

        <IgbGrid Width="100%"
                 Id="grid"
                 Height="100%"
                 RowSelection=GridSelectionMode.Multiple
                 PrimaryKey="Key"
                 @ref=grid
                 AutoGenerate=true
                 Data=northwindEmployees>
        </IgbGrid>
        <IgbButton @onclick=GetSelected>Get selected</IgbButton>
        @code {
            public IgbGrid grid;
            private async void GetSelected()
            {
                var selected = this.grid.SelectedRows;
            }
        }
    

    Additionally, assigning row IDs to SelectedRows will allow you to change the grid's selection state.

    <IgbGrid Width="100%"
                 Id="grid"
                 Height="100%"
                 RowSelection=GridSelectionMode.Multiple
                 PrimaryKey="Key"
                 SelectedRows=selectedRows
                 @ref=Grid
                 AutoGenerate=true
                 Data=northwindEmployees>
    </IgbGrid>
    
    @code {
        public object[] selectedRows = new object[] { 1, 2, 5 };
    }
    

    행 선택기 템플릿

    You can template header and row selectors in the IgbGrid and also access their contexts which provide useful functionality for different scenarios.

    By default, the IgbGrid handles all row selection interactions on the row selector's parent container or on the row itself, leaving just the state visualization for the template. Overriding the base functionality should generally be done using the RowSelectionChanging event. In case you implement a custom template with a Click handler which overrides the base functionality, you should stop the event's propagation to preserve the correct row state.

    행 템플릿

    To create a custom row selector template, within the IgbGrid you can use the RowSelectorTemplate property. From the template you can access the implicitly provided context variable, with properties that give you information about the row's state.

    The Selected property shows whether the current row is selected or not while the Index property can be used to access the row index.

    igRegisterScript("WebGridRowSelectorTemplate", (ctx) => {
        var html = window.igTemplating.html;
        if (ctx.implicit.selected) {
            return html`<div style="justify-content: space-evenly;display: flex;width: 70px;">
        <span> ${ctx.implicit.index}</span>
    <igc-checkbox checked></igc-checkbox>
    </div>`;
        } else {
            return html`<div style="justify-content: space-evenly;display: flex;width: 70px;">
        <span> ${ctx.implicit.index}</span>
    <igc-checkbox></igc-checkbox>
    </div>`;
        }
    }, false);
    

    The RowID property can be used to get a reference of an IgbGrid row. This is useful when you implement a click handler on the row selector element.

    In the above example we are using an IgbCheckbox and we bind rowContext.selected to its Checked property. See this in action in our Row Numbering Demo.

    헤더 템플릿

    To create a custom header selector template, within the IgbGrid, you can use the HeadSelectorTemplate property. From the template you can access the implicitly provided context variable, with properties that give you information about the header's state.

    The SelectedCount property shows you how many rows are currently selected while TotalCount shows you how many rows there are in the IgbGrid in total.

    public RenderFragment<IgbHeadSelectorTemplateContext> Template = (context) =>
    {
        return @<div> <img style="min-width:80px;" src="https://ko.infragistics.com/angular-demos-lob/assets/images/card/avatars/igLogo.png"/></div>;
    };
    

    The SelectedCount and TotalCount properties can be used to determine if the head selector should be checked or indeterminate (partially selected).

    행 번호 매기기 데모

    This demo shows the usage of custom header and row selectors. The latter uses Index to display row numbers and an IgbCheckbox bound to Selected.

    Excel 스타일 행 선택기 데모

    이 데모에서는 Excel과 유사한 헤더 및 행 선택기와 유사한 사용자 지정 템플릿을 사용합니다.

    조건부 선택 데모

    이 데모는 이벤트와 선택 불가능한 행에 대한 체크박스가 비활성화된 커스텀 템플릿을 사용해RowSelectionChanging 일부 행이 선택되는 것을 방지합니다.

    API 참조

    추가 리소스

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