A Blazor interop library for DataTables
$ dotnet add package Soenneker.Blazor.DataTables
Soenneker.Blazor.DataTablesThis library simplifies the integration of DataTables into Blazor applications, providing access to options, events, etc. A demo project showcasing common usages is included.
Diligence was taken to align the Blazor API with JS. Refer to the DataTables documentation for details. This is a work-in-progress; contribution is welcomed.
dotnet add package Soenneker.Blazor.DataTables
Startup.cs filepublic void ConfigureServices(IServiceCollection services)
{
services.AddDataTablesInteropAsScoped();
}
@using Soenneker.Blazor.DataTables
<DataTable Options="_options">
<thead>
<tr>
<th>Name</th>
<th>Position</th>
<th>Office</th>
<th>Age</th>
<th>Start date</th>
<th>Salary</th>
</tr>
</thead>
<tbody>
<tr>
<td>John Doe</td>
<td>Developer</td>
<td>London</td>
<td>28</td>
<td>2017/04/25</td>
<td>$320,800</td>
</tr>
</tbody>
</DataTable>
@code{
private readonly DataTableOptions _options_ = new()
{
Searching = true,
LengthChange = false,
Info = false,
Paging = false,
Order = [new object[] {0, "asc"}]
};
}
When using server-side processing, you can override the default DataTables processing indicator with custom Blazor content. This feature allows you to:
<DataTable Options="tableOptions" OnServerSideRequest="HandleServerSideRequest">
<ProcessingIndicator>
<div class="text-center">
<div class="spinner-border text-primary" role="status"></div>
<div class="mt-2">Loading data from server...</div>
</div>
</ProcessingIndicator>
<ChildContent>
<thead>
<tr>
<th data-data="id">ID</th>
<th data-data="name">Name</th>
<th data-data="email">Email</th>
</tr>
</thead>
<tbody>
</tbody>
</ChildContent>
</DataTable>The DataTables component supports continuation token-based pagination, which is useful for services like Azure Table Storage, Cosmos DB, or other cloud storage solutions that use continuation tokens instead of traditional offset-based pagination.
The component automatically emulates traditional DataTables pagination behavior while using continuation tokens under the hood:
null for the continuation token<DataTable @ref="_dataTable"
OnServerSideRequest="HandleServerSideRequest"
Options="_tableOptions">
<thead>
<tr>
<th data-data="id">ID</th>
<th data-data="name">Name</th>
<th data-data="email">Email</th>
</tr>
</thead>
<tbody>
</tbody>
</DataTable>
@code {
private DataTable _dataTable = null!;
private readonly DataTableOptions _tableOptions = new()
{
ServerSide = true,
PageLength = 10
};
private async Task<DataTableServerResponse> HandleServerSideRequest(DataTableServerSideRequest request)
{
// The request.ContinuationToken will contain the token from the previous response
// or null for the first request
var result = await YourService.GetDataAsync(
pageSize: request.Length,
continuationToken: request.ContinuationToken,
searchTerm: request.Search?.Value,
orderBy: GetOrderByColumn(request.Order)
);
// Return the response with the continuation token for the next page
// Note: The component automatically calculates appropriate TotalRecords and TotalFilteredRecords
// to make DataTables think it's using traditional pagination
return DataTableServerResponse.Success(
draw: request.Draw,
recordsTotal: result.TotalRecords,
recordsFiltered: result.TotalFilteredRecords,
data: result.Data,
continuationToken: result.NextContinuationToken // null when no more pages
);
}
}You can programmatically control continuation tokens using the DataTable component methods:
// Reset pagination to start from the beginning
_dataTable.ResetContinuationToken();
// Set a specific continuation token for the next request
_dataTable.SetContinuationToken("your-custom-token");The component automatically stores continuation tokens for visited pages, enabling backward navigation:
This ensures that your server-side logic receives the same continuation tokens when users navigate back to previously visited pages, maintaining consistency with your data source.
TotalRecords and TotalFilteredRecords values to make DataTables think it's using traditional offset-based pagination