Server data
Since 0.1.0-alpha.20Loading from a database
To load rows from a database, set ItemsProvider. It gets a DataRequest with the page, sort and filters, and request.ToResultAsync runs them as one query. For grouping and footer totals, add a GroupsProvider, which DataGroupRequest.ToResultAsync answers the same way.
Static
Preview
| Status: Paid (5) | ||||
|---|---|---|---|---|
| Status: Pending (6) | ||||
| Status: Refunded (6) | ||||
| Status: Failed (6) | ||||
| Sum: $6,106.50 | ||||
Code
@* Rows, groups and the footer's total, each asked of the data source in its own query: the provider
loads one page of orders, and the groups provider counts and totals the groups the user opens. *@
IzzyBlazor.Site.Examples.Table
<DataGrid TItem="Order" ItemsProvider="LoadAsync" GroupsProvider="GroupAsync" ItemKey="o => o.Number.ToString()"
Caption="Orders from the database" PageSize="5" Groupable InitialGroup="status" QueryPrefix="db-" Grow>
<Columns>
<PropertyColumn Property="o => o.Number" Title="Order" Align="CellAlign.Start" Width="7.Rem" Sortable />
<PropertyColumn Property="o => o.Customer" Sortable Filterable />
<PropertyColumn Property="o => o.Status" Filterable Width="9.Rem" />
<PropertyColumn Property="o => o.Date" Format="MMM d" Sortable Width="8.Rem" />
<PropertyColumn Property="o => o.Total" Format="C" Sortable Width="9.Rem" Aggregate="DataAggregate.Sum" />
</Columns>
</DataGrid>
{
// Stands in for a DbSet: with EF Core, pass db.Orders and call ToResultAsync.
private static IQueryable<Order> Database => Orders.All.AsQueryable();
private ValueTask<DataResult<Order>> LoadAsync(DataRequest<Order> request) =>
ValueTask.FromResult(request.ToResult(Database));
private ValueTask<DataGroupResult> GroupAsync(DataGroupRequest<Order> request) =>
ValueTask.FromResult(request.ToResult(Database));
}To skip the providers, pass an EF Core IQueryable as Items; the grid builds the same queries. Table's Loading data page covers the request in full, including rows from an API.
Virtual rows
Virtual rows render only the rows in view, so 100,000 rows scroll as easily as 100. Set Virtualize, which works on interactive pages.
Static
Preview
| Select | Number | Customer | Total |
|---|---|---|---|
| 1 | Katherine Johnson | $108.00 | |
| 2 | Grace Hopper | $149.75 | |
| 3 | Barbara Liskov | $191.50 | |
| 4 | Linus Torvalds | $233.25 | |
| 5 | Alan Turing | $275.00 | |
| 6 | Ada Lovelace | $316.75 | |
| 7 | Margaret Hamilton | $358.50 | |
| 8 | Katherine Johnson | $400.25 | |
| 9 | Grace Hopper | $442.00 | |
| 10 | Barbara Liskov | $483.75 | |
| 11 | Linus Torvalds | $65.50 | |
| 12 | Alan Turing | $107.25 | |
| 13 | Ada Lovelace | $149.00 | |
| 14 | Margaret Hamilton | $190.75 | |
| 15 | Katherine Johnson | $232.50 | |
| 16 | Grace Hopper | $274.25 | |
| 17 | Barbara Liskov | $316.00 | |
| 18 | Linus Torvalds | $357.75 | |
| 19 | Alan Turing | $399.50 | |
| 20 | Ada Lovelace | $441.25 |
Code
@* 100,000 rows, of which only those in view are drawn, each window read as a database would be. Switch
this page to Interactive to see it; a static page loads more on scroll instead. *@
IzzyBlazor.Site.Examples.Table
<DataGrid TItem="Order" ItemsProvider="LoadAsync" ItemKey="o => o.Number.ToString()" Caption="Every order" Virtualize RowHeight="40" MaxHeight="24.Rem" PageSize="20" SelectionMode="SelectionMode.Multiple" SelectionName="every" QueryPrefix="every-" Grow>
<Columns>
<PropertyColumn Property="o => o.Number" Sortable Align="CellAlign.Start" Width="8.Rem" />
<PropertyColumn Property="o => o.Customer" />
<PropertyColumn Property="o => o.Total" Format="C" Width="8.Rem" />
</Columns>
</DataGrid>
{
private static readonly IReadOnlyList<Order> Many = Enumerable.Range(1, 100_000)
.Select(i => Orders.All[i % Orders.All.Count] with { Number = i })
.ToList();
private async ValueTask<DataResult<Order>> LoadAsync(DataRequest<Order> request)
{
// As long as a query takes, so you can see rows on their way.
if (RendererInfo.IsInteractive)
{
await Task.Delay(150, request.CancellationToken);
}
return request.ToResult(Many.AsQueryable());
}
}It needs a MaxHeight, and each row must fit one line of RowHeight. On static pages the same grid loads more rows as the user scrolls.
This example is static unless you switch the page to Interactive.