Use MudBlazor MudDataGrid ServerData to load paged data from an API – Blazor

by
Alexei Petrov
blazor mudblazor pagedlist

Quick Fix: To use MudBlazor MudDataGrid, define DTOs for request and response, implement ServerData and LoadGridData, handle row clicks with OnRowClick and EditItem. Add a SearchTerm property to filter data. For pre-setting Page and PageSize, use state management and OnAfterRenderAsync.

The Problem:

In a web application built using MudBlazor, I need to display and interact with paged data dynamically loaded from a remote API. However, the documentation for MudBlazor’s MudDataGrid component seems insufficient in explaining how to achieve this effectively. Specifically, I have questions about utilizing the ServerData attribute, handling row clicks, passing filter and search criteria to the API, and initializing the GridState object with desired page and page size.

The Solutions:

Solution 1: Use MudBlazor MudDataGrid ServerData to load paged data from an API

To use the MudBlazor MudDataGrid ServerData, you can follow these steps:

  1. Define DTOs to handle the user’s request and the response from the API. This includes the GridDataRequestDto for the request and AnimalListDto and AnimalListItemDto for the response.
  2. Implement the MudDataGrid component in your Blazor page, setting the ServerData attribute to call a method called LoadGridData.
  3. Define the LoadGridData method in your Blazor page code, which performs the API call and returns a GridData object with the requested data and total count.
  4. Create the GetAnimalList method that performs the API call, which should return the AnimalListDto result with the Items and ItemTotalCount properties populated.
  5. To handle row clicks, add a RowClick event handler to the MudDataGrid component and implement the OnRowClick method in your code to handle the click event.
  6. To send additional filter or search criteria on the API call, add a SearchTerm property to the GridDataRequestDto and add a form to the Blazor page above the grid to allow the user to enter search terms. Implement a Search method to handle the search submission and reload the grid data.
  7. To pre-set the Page and PageSize of the GridState object, inject a state manager into your Blazor page and load the state into the request DTO. Override the OnParametersSetAsync method to set the request DTO and use the OnAfterRenderAsync method to set the CurrentPage and RowsPerPage of the grid.

Solution 2: Handling row clicks in MudBlazor MudDataGrid with ServerData

To handle row clicks in MudBlazor MudDataGrid with ServerData, follow these steps:

  • Create a MudDataGrid<T> component:
<MudDataGrid [email protected]
               ServerData=@pagedData
               RowClick="OnRowClick" >

</MudDataGrid>
  • Define a RowClick event handler in your component:
private void OnRowClick(TableRowClickEventArgs<T> args)
{
    // your code here
}
  • In the OnRowClick event handler, you can access the data item of the clicked row using args.Item:
private void OnRowClick(TableRowClickEventArgs<T> args)
{
    // access the data item of the clicked row
    var dataItem = args.Item;

    // do something with the data item, e.g. navigate to a detail page
    NavigationManager.NavigateTo($"/details/{dataItem.Id}");
}

Q&A

How do I use the ServerData attribute?

Define DTOs to handle the user’s request and the response from the API, then implement the MudDataGrid component in your Blazor page.

How do I handle row clicks?

Set the Grid’s RowClick attribute, and implement methods to handle the click event.

How do I send additional filter / search criteria on the API call?

Add a search term property to your request DTO, create a form to allow the user to input the search term, and update the Search method to handle the form submission.

Video Explanation:

The following video, titled "How To Use MudBlazor Tables (Pagination, Sorting, Filtering ...", provides additional insights and in-depth exploration related to the topics discussed in this post.

Play video

... mudblazor.com/components/table#api. ... How To Use MudBlazor Tables (Pagination, Sorting, Filtering, Server Side Data).