Server data
Since 0.1.0-alpha.24Loading a level at a time
To load rows from a database as they open, set ItemsProvider instead of Items. The tree list asks for the top rows, then for the children of each open row, with that row's key in request.Parent. request.ToTreeResult answers it from a query, given the key and the parent key.
Static
Preview
Code
@* A file store's folders and files, one level at a time from the database: opening a folder asks for
its contents. Lisbon trip holds 640 photos, so it shows the first 200 and Show more. *@
<TreeList TItem="StoredFile" ItemsProvider="LoadAsync" ItemKey="f => f.Id.ToString()" ItemHasChildren="f => f.IsFolder"
ItemIcon="Icon" QueryPrefix="store-" Caption="Storage" MaxHeight="28.Rem" Grow>
<Columns>
<PropertyColumn Property="f => f.Name" Sortable Filterable />
<PropertyColumn Property="f => f.Modified" Format="MMM d, yyyy" Sortable Width="9.Rem" />
<TemplateColumn Title="Size" SortBy="f => f.Bytes" Align="CellAlign.End" Sortable Width="7.Rem">Size(context.Bytes)</TemplateColumn>
</Columns>
</TreeList>
{
// Stands in for a DbSet: with EF Core, pass db.Files to ToTreeResultAsync.
private static IQueryable<StoredFile> Database => Files.AsQueryable();
private ValueTask<DataResult<StoredFile>> LoadAsync(DataRequest<StoredFile> request) =>
ValueTask.FromResult(request.ToTreeResult(Database, f => f.Id, f => f.FolderId));
private static IconData? Icon(StoredFile file, bool open) =>
file.IsFolder ? open ? Lucide.FolderOpen : Lucide.Folder : file.Name.EndsWith(".jpg") ? Lucide.FileImage : Lucide.FileText;
private static string Size(long? bytes) => bytes switch
{
null => "",
< 1024 * 1024 => $"{bytes / 1024.0:0} KB",
_ => $"{bytes / (1024.0 * 1024):0.0} MB",
};
}Set ItemHasChildren so a row shows its chevron before its children load. Without it, every row gets one, and a row that opens to none loses it.
Sorting, filters and search go with every request. While filtering, ToTreeResult keeps the rows that match and the rows above them, which takes a query for each level above a match. With EF Core, use ToTreeResultAsync with CountAsync and ToListAsync.
A row shows at most 200 rows under it, then Show more, which adds as many again. Set ChildRowLimit to change it.
Virtual rows
Virtual rows draw only the rows in view, so a tree with thousands of rows open scrolls as easily as a short one. Set Virtualize, with a MaxHeight and a RowHeight; it works on interactive pages.
Always Interactive
Preview
Code
@* A company's 2,000-odd people by division, department and team, every branch open: only the
rows in view are drawn as the list scrolls, and opening or closing a row keeps the place. *@
<TreeList Items="People" ItemKey="p => p.Id" ItemParentKey="p => p.ManagerId" ExpandAll Virtualize RowHeight="40"
MaxHeight="28.Rem" Caption="Directory" QueryPrefix="directory-" Grow>
<Columns>
<PropertyColumn Property="p => p.Name" Filterable />
<PropertyColumn Property="p => p.Title" Width="14.Rem" />
<PropertyColumn Property="p => p.Location" Width="9.Rem" />
</Columns>
</TreeList>
{
}Every visible row is worked out from Items, open branches included, and opening or closing a row keeps the place in the list. There's no Show more, and each row is one line high.
On a static page the same tree pages by its top rows.