IvanJosipovic / BlazorTable

Blazor Table Component with Sorting, Paging and Filtering

Home Page:https://BlazorTable.netlify.app

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Paging Debugging

KevinBurton opened this issue · comments

I am trying to see what the flow is for a custom loader as in the example on paging. I have implemented this method:

public async Task<PaginationResult<T>> LoadDataAsync(FilterData parameters)

I put a breakpoint at the beginning of the procedure and right at the end. I stop on the end breakpoint without ever stopping on the beginning breakpoint. I have having a hard time figuring out how it skipped all the code to get to the end. Also I would like a definitive statement on the order of calls to LoadDataAsync. As far as I can tell it is called once with the argument 'parameters' set to null. Then it gets called again with the FilterData set to what is specified by the component razor parameters (which is what I was expecting). How is this function called and when?

One more question on this same topic. Can the 'loader' handle when the TableItem is a built-in type like 'int' or 'string'? Something like

@using BlazorTable
<h2>Projects</h2>
<Table TableItem="int" DataLoader="_loader" Items="data" PageSize="5" SelectionType="selectionType" RowClickAction="RowClick" SelectedItems="selectedItems" ShowSearchBar="false">
    <Column TableItem="int" Title="Project Id" Field="@(x => x)" Sortable="true" Width="10%" DefaultSortColumn="true" />
    <Pager ShowPageNumber="true" ShowTotalCount="true" />
</Table>

This is the code behind for one of my components

@using BlazorTable
<h2>Drugs</h2>
<Table TableItem="Drug" DataLoader="_loader" Items="data" PageSize="5" SelectionType="selectionType" RowClickAction="RowClick" SelectedItems="selectedItems" ShowSearchBar="false">
    <Column TableItem="Drug" Title="Id" Field="@(x => x.DrugId)" Sortable="true" Width="10%" DefaultSortColumn="true" />
    <Column TableItem="Drug" Title="Subclass Id" Field="@(x => x.DrugSubclassId)" Sortable="true" Width="5%" />
    <Column TableItem="Drug" Title="Name" Field="@(x => x.DrugName)" Sortable="true" Width="20%" />
    <Column TableItem="Drug" Title="Abbr" Field="@(x => x.DrugAbbr)" Sortable="true" Width="5%" />
    <Column TableItem="Drug" Title="Public" Field="@(x => x.ForPublicUse)" Sortable="true" Width="5%" />
    <Column TableItem="Drug" Title="Created By" Field="@(x => x.CreatedBy)" Sortable="true" Width="5%" />
    <Column TableItem="Drug" Title="Created Date" Field="@(x => x.CreatedDate)" Sortable="true" Width="5%">
        <Template>
            @(context.CreatedDate.ToShortDateString())
        </Template>
    </Column>
    <Column TableItem="Drug" Title="Combination Drug" Field="@(x => x.CombinationDrug)" Sortable="true" Width="5%" />
    <Column TableItem="Drug" Title="Fixed Ratio" Field="@(x => x.FixedRatio)" Sortable="true" Width="5%" />
    <Column TableItem="Drug" Title="Proportion" Field="@(x => x.Proportion)" Sortable="true" Width="10%" />
    <Column TableItem="Drug" Title="Active" Field="@(x => x.Active)" Sortable="true" Width="20%" />
    <Pager ShowPageNumber="true" ShowTotalCount="true" />
</Table>
    public partial class ActiveDrugs
    {
        [Inject]
        private IBreakpointManagementDataService dataService { get; set; }

        private IDataLoader<Drug> _loader;

        private IEnumerable<Drug> data;

        private Drug selected;

        private SelectionType selectionType = SelectionType.Single;

        private List<Drug> selectedItems = new List<Drug>();
        
        protected override async Task OnInitializedAsync()
        {
            _loader = new DrugDataLoader(dataService);
            data = (await _loader.LoadDataAsync(null)).Records;
        }
        public void RowClick(Drug data)
        {
            selected = data;
            StateHasChanged();
        }
    }

    public class DrugDataLoader : IDataLoader<Drug>
    {
        private readonly IBreakpointManagementDataService _dataService;
        public DrugDataLoader(IBreakpointManagementDataService dataService)
        {
            _dataService = dataService;
        }
        public async Task<PaginationResult<Drug>> LoadDataAsync(FilterData parameters)
        {
            IList<Drug> results;
            if (parameters == null)
            {
                results = await _dataService.GetAllDrugs();
            }
            else if (parameters.Top == null)
            {
                results = await _dataService.GetAllDrugs();
            }
            else if (string.IsNullOrWhiteSpace(parameters.OrderBy))
            {
                results = await _dataService.GetAllDrugs(parameters.Top.Value, parameters.Skip.Value);
            }
            else
            {
                var order = parameters.OrderBy.Split(" ");
                if (order.Length >= 2)
                {
                    results = await _dataService.GetAllDrugs(parameters.Top.Value, parameters.Skip.Value, order[0]);
                }
                else
                {
                    results = await _dataService.GetAllDrugs(parameters.Top.Value, parameters.Skip.Value);
                }
            }
            var count = await _dataService.GetDrugCount();
            return new PaginationResult<Drug>
            {
                Records = results,
                Skip = parameters?.Skip ?? 0,
                Total = int.Parse(count),
                Top = parameters?.Top ?? 0
            };
        }
    }

@KevinBurton Have you managed to fetch the paginated data from a service ? I am getting NULL exception at _dataService.Get Method as _dataService is coming as NULL
Also i am curious to know any way to pass additional parameters that can be used for filtering other than FilterData