Skip to content

The SetSelection method doesn't work in OnAfterRender method or how select particular row after table rendered? #2282

@DemoS76

Description

@DemoS76

Describe the bug

The SetSelection method doesn't work in OnAfterRender method or how select particular row after component rendered?

Steps to reproduce (please include code)

Index page:

@page "/"

<h1>Hello, world!</h1>

<Button Type="primary" OnClick=onBtnClick>Show table</Button>

@if (this.visible)
{
    <TableCmp />
}

@code{
    private bool visible = false;

    private void onBtnClick()
    {
        this.visible = !this.visible;
    }
}

Table component (TableCmp ):

<Table @ref=table DataSource=tableData ScrollY="240px" PageSize="50" Size=@TableSize.Small @bind-SelectedRows=selectedRows>
    <Selection Key=@context.Id Type="radio" />
    <Column Title="Id" @bind-Field=@context.Id Width="150" />
    <Column Title="Data" @bind-Field=@context.Data Width="150" />
</Table>

<Text>Selected: @selectedRows?.FirstOrDefault()?.ToString()</Text>
<br />
<Button OnClick=OnBtnClick>Select 2nd row</Button>

@code {
    class TableData
    {
        public string Id { get; set; }
        public string Data { get; set; }

        public override string ToString()
        {
            return $"Id: {Id}, Data: {Data} ";
        }
    }

    private ITable table;
    private IEnumerable<TableData> selectedRows;

    private IEnumerable<TableData> tableData = new TableData[] {
        new TableData{ Id = "1", Data = "Data1" },
        new TableData{ Id = "2", Data = "Data2" },
        new TableData{ Id = "3", Data = "Data3" },
    };

    protected override async Task OnAfterRenderAsync(bool firstRender)
    {
        await base.OnAfterRenderAsync(firstRender);

        if (firstRender)
        {
            table.SetSelection(new[] { "2" });
        }
    }

    private void OnBtnClick()
    {
        table.SetSelection(new[] { "2" });
    }
}

Just rendered view:
image

When "Select 2nd row" button was clicked:
image

Further technical details

  • AntDesign Nuget Package version: 0.10.3.1

  • Include the output of dotnet --info:.net6
    .NET SDK (reflecting any global.json):
    Version: 6.0.101
    Commit: ef49f6213a
    Runtime Environment:
    OS Name: Windows
    OS Version: 10.0.19044
    OS Platform: Windows
    RID: win10-x64
    Base Path: C:\Program Files\dotnet\sdk\6.0.101\

  • The IDE (VS / VS Code/ VS4Mac) you're running on, and it's version: VS2022

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions