dotnet / aspnetcore

ASP.NET Core is a cross-platform .NET framework for building modern cloud-based web applications on Windows, Mac, or Linux.
https://asp.net
MIT License
35.19k stars 9.93k forks source link

[Blazor][Question] How to implement DataTable component in server-side Blazor #20014

Closed jemiller0 closed 4 years ago

jemiller0 commented 4 years ago

Someone asked how to create a DataTable component in Blazor at https://github.com/dotnet/aspnetcore/issues/14886.

Someone replied with an example that worked like the following.

    <DataTable Class="table" Data="@(folioContext.User2s.Take(10).Cast<dynamic>().ToList())">
        <DataTableColumn Name="Id" Field="Id" Type="typeof(Guid)"></DataTableColumn>
        <DataTableColumn Name="Name" Field="Name" Type="typeof(string)"></DataTableColumn>
        <DataTableColumn>
            <Template Context="item">
                <a href="/@item.Id" class="btn">text</a>
            </Template>
        </DataTableColumn>
    </DataTable>

The person said that it worked. However, I tried it with server-side Blazor and it is not working for me. The problem is that the child components, DataTableColumn aren't created until after the foreach loops in DataTable.razor are executed. Therefore, when the foreach loop iterates over the Columns colection, it is still empty at that point and doesn't render anything.

My question is, is trying to create a component like this kind of abusing how Blazor is supposed to work? In this example, the child components don't actually render anything. They are just there to act at parameter data/configuration information for the DataTable component.

I'm wondering what the recommendations are for implementing something like this? Telerik has commercial controls that work this way. I'm wondering if they are kind of breaking the rules on how this is designed?

It would be good if Microsoft could at least provide some example code on how to create more complex components like this.

Telerik's controls are great. I've been using them for years, but, there needs to be open source components that can be used in open source projects.

I would like to see Microsoft provide controls with Blazor by default. At least something basic should be provided. I don't think users should have to constantly reinvent the wheel every time they need a DataGrid.

I'm fine creating my own at this point, but, need to know what the recommended approach to implemented it is.

I want to create a component that has works like Telerik's does. I.e. I want built-in paging, filtering, sorting, etc. I think I could do it myself, but, currently, don't know what the recommended approach is.

I kind of wonder if the Blazor event life cycle is adequate to implement something along the lines of what ASP.NET Web Forms offers.

This is being sold as the replacement to Web Forms. I hope the designers of Blazor took components such as what Telerik has offered for years in Web Forms into account when designing how the components work.

mkArtakMSFT commented 4 years ago

Hi. It looks like this is a question about how to use ASP.NET Core. While we do our best to look through all the issues filed here, we are not a general-purpose forum. To get a faster response we suggest posting your questions to StackOverflow using the asp.net-core-blazor tag.