Blazor Grid Bound Column by Field Name

前端 未结 1 444
死守一世寂寞
死守一世寂寞 2020-12-19 15:44

I\'m studying some paid grid components and they have a pretty cool \"Bound Column\" technique:


    
           


        
相关标签:
1条回答
  • 2020-12-19 16:17

    It's not a trivial solution, you should to understand how Blazor works with components. Basically, the way, is to register children MyColumn "templates" on outer MyGrid component, then, render grid dynamically with a bit of reflection. Below I post the recipe ready for copy-paste, test and learn.

    Step 0: Create the templated blazor demo:

    dotnet new blazorserver
    

    Step 1: Create Pages/MyColumn.razor component:

    @code {
        [Parameter]
        public string Field { get; set; }
    
        [CascadingParameter]
        private MyGrid Parent { get; set; }    
    
        protected override void OnInitialized()
        {
            base.OnInitialized();        
            Parent.AddColumn(this); //register child into parent
        }
    }
    

    Step 2: Create Pages/MyGrid.razor component:

    <CascadingValue Value="this">
    <table class="table">
        <tbody>
            @ChildContent
            @foreach (var item in DataSource)
            {
                @RowItem(item) 
            }
        </tbody>
    </table>
    </CascadingValue>
    
    @code {
        [Parameter]
        public RenderFragment ChildContent { get; set; }
    
        [Parameter]
        public IEnumerable<object> DataSource { get; set; }
    
        List<MyColumn> Columns = new List<MyColumn>();
        public void AddColumn(MyColumn column)
        {
            Columns.Add(column);
            StateHasChanged();
        }
    

    // Here the function to create each row dynamically:

        private RenderFragment RowItem(object row)
        {   
            int i = 0;
            return b =>          // create dynamically
            {
                b.OpenElement(++i, "tr");
                foreach(var column in Columns)
                {
                    b.OpenElement(++i, "td");
                    var val = row.GetType().GetProperty(column.Field)
                              .GetValue(row, null).ToString();                
                    b.AddContent(++i,val);
                    b.CloseElement();
                }
                b.CloseElement();
            };
        }
    }
    

    Step 3: Use your grid on Pages/FetchData.razor

    @page "/fetchdata"
    
    @using s.Data
    @inject WeatherForecastService ForecastService
    
    <h1>Weather forecast</h1>
    
    <p>This component demonstrates fetching data from a service.</p>
    
    @if (forecasts == null)
    {
        <p><em>Loading...</em></p>
    }
    else
    {
        <MyGrid DataSource=@forecasts>
           <MyColumn Field="Date"/>
           <MyColumn Field="Summary"/>
        </MyGrid>
    }
    
    @code {
        private WeatherForecast[] forecasts;
    
        protected override async Task OnInitializedAsync()
        {
            forecasts = await ForecastService.GetForecastAsync(DateTime.Now);
        }
    }
    

    Result:

    0 讨论(0)
提交回复
热议问题