Skip to content

Commit 888fbe7

Browse files
authored
feat(blazorui): add LoadingTemplate parameter to BitDataGrid #11311 (#11312)
1 parent 2724b88 commit 888fbe7

File tree

5 files changed

+1045
-797
lines changed

5 files changed

+1045
-797
lines changed

src/BlazorUI/Bit.BlazorUI.Extras/Components/DataGrid/BitDataGrid.razor

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
@{
66
StartCollectingColumns();
77
}
8-
@ChildContent
8+
@(Columns ?? ChildContent)
99
<BitDataGridDefer>
1010
@{
1111
FinishCollectingColumns();
@@ -26,11 +26,22 @@
2626
TItem="(int RowIndex, TGridItem Data)"
2727
ItemsProvider="@ProvideVirtualizedItems"
2828
ItemContent="@(item => builder => RenderRow(builder, item.RowIndex, item.Data))"
29-
Placeholder="@(placeholderContext => builder => RenderPlaceholderRow(builder, placeholderContext))" />
29+
Placeholder="@(placeholderContext => builder => RenderPlaceholderRow(builder, placeholderContext))" />
3030
}
3131
else
3232
{
33-
@_renderNonVirtualizedRows
33+
if (IsLoading && LoadingTemplate is not null)
34+
{
35+
<tr>
36+
<td colspan="100">
37+
@LoadingTemplate
38+
</td>
39+
</tr>
40+
}
41+
else
42+
{
43+
@_renderNonVirtualizedRows
44+
}
3445
}
3546
</tbody>
3647
</table>

src/BlazorUI/Bit.BlazorUI.Extras/Components/DataGrid/BitDataGrid.razor.cs

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ public BitDataGrid()
7676
columnsFirstCollectedSubscriber.SubscribeOrMove(_internalGridContext.ColumnsFirstCollected);
7777
}
7878

79+
private bool IsLoading => _pendingDataLoadCancellationTokenSource is not null;
80+
7981

8082

8183
/// <summary>
@@ -90,15 +92,9 @@ public BitDataGrid()
9092
[Parameter] public string? Class { get; set; }
9193

9294
/// <summary>
93-
/// A queryable source of data for the grid.
94-
///
95-
/// This could be in-memory data converted to queryable using the
96-
/// <see cref="System.Linq.Queryable.AsQueryable(System.Collections.IEnumerable)"/> extension method,
97-
/// or an EntityFramework DataSet or an <see cref="IQueryable"/> derived from it.
98-
///
99-
/// You should supply either <see cref="Items"/> or <see cref="ItemsProvider"/>, but not both.
95+
/// Alias of the ChildContent parameter.
10096
/// </summary>
101-
[Parameter] public IQueryable<TGridItem>? Items { get; set; }
97+
[Parameter] public RenderFragment? Columns { get; set; }
10298

10399
/// <summary>
104100
/// Optionally defines a value for @key on each rendered row. Typically this should be used to specify a
@@ -113,11 +109,15 @@ public BitDataGrid()
113109
[Parameter] public Func<TGridItem, object> ItemKey { get; set; } = x => x!;
114110

115111
/// <summary>
116-
/// A callback that supplies data for the rid.
112+
/// A queryable source of data for the grid.
113+
///
114+
/// This could be in-memory data converted to queryable using the
115+
/// <see cref="System.Linq.Queryable.AsQueryable(System.Collections.IEnumerable)"/> extension method,
116+
/// or an EntityFramework DataSet or an <see cref="IQueryable"/> derived from it.
117117
///
118118
/// You should supply either <see cref="Items"/> or <see cref="ItemsProvider"/>, but not both.
119119
/// </summary>
120-
[Parameter] public BitDataGridItemsProvider<TGridItem>? ItemsProvider { get; set; }
120+
[Parameter] public IQueryable<TGridItem>? Items { get; set; }
121121

122122
/// <summary>
123123
/// This is applicable only when using <see cref="Virtualize"/>. It defines an expected height in pixels for
@@ -126,6 +126,18 @@ public BitDataGrid()
126126
/// </summary>
127127
[Parameter] public float ItemSize { get; set; } = 50;
128128

129+
/// <summary>
130+
/// A callback that supplies data for the rid.
131+
///
132+
/// You should supply either <see cref="Items"/> or <see cref="ItemsProvider"/>, but not both.
133+
/// </summary>
134+
[Parameter] public BitDataGridItemsProvider<TGridItem>? ItemsProvider { get; set; }
135+
136+
/// <summary>
137+
/// The custom template to render while loading the new items.
138+
/// </summary>
139+
[Parameter] public RenderFragment? LoadingTemplate { get; set; }
140+
129141
/// <summary>
130142
/// Optionally links this <see cref="BitDataGrid{TGridItem}"/> instance with a <see cref="BitDataGridPaginationState"/> model,
131143
/// causing the grid to fetch and render only the current page of data.
@@ -419,7 +431,7 @@ private string AriaSortValue(BitDataGridColumnBase<TGridItem> column)
419431
: ColumnClass(column);
420432

421433
private string GridClass()
422-
=> $"bitdatagrid {Class} {(_pendingDataLoadCancellationTokenSource is null ? null : "loading")}";
434+
=> $"bitdatagrid {Class} {((IsLoading && LoadingTemplate is null) ? "loading" : null)}".Trim();
423435

424436
private void CloseColumnOptions()
425437
{

src/BlazorUI/Demo/Client/Bit.BlazorUI.Demo.Client.Core/Pages/Components/Extras/DataGrid/BitDataGridDemo.razor

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,5 +114,27 @@
114114
Placeholder="Search on Name" />
115115
</div>
116116
</DemoExample>
117+
118+
<DemoExample Title="LoadingTemplate" RazorCode="@example5RazorCode" CsharpCode="@example5CsharpCode" Id="example5">
119+
<div class="grid3">
120+
<BitDataGrid ItemKey="(p => p.Id)" ItemsProvider="@productsItemsProvider" TGridItem="ProductDto" Pagination="pagination3" @ref="productsDataGrid">
121+
<Columns>
122+
<BitDataGridPropertyColumn Property="@(p => p.Id)" Sortable="true" IsDefaultSort="BitDataGridSortDirection.Ascending" />
123+
<BitDataGridPropertyColumn Property="@(p => p.Name)" Sortable="true" />
124+
<BitDataGridPropertyColumn Property="@(p => p.Price)" Sortable="true" />
125+
</Columns>
126+
<LoadingTemplate>
127+
<BitStack Alignment="BitAlignment.Center" Style="height:185px">
128+
<b>Loading items...</b>
129+
<BitOrbitingDotsLoading Size="BitSize.Large" />
130+
</BitStack>
131+
</LoadingTemplate>
132+
</BitDataGrid>
133+
</div>
134+
<BitDataGridPaginator Value="@pagination3" SummaryFormat="@(v => $"Total: {v.TotalItemCount?.ToString("N0")}")">
135+
<TextTemplate Context="state">@(state.CurrentPageIndex + 1) / @(state.LastPageIndex + 1)</TextTemplate>
136+
</BitDataGridPaginator>
137+
</DemoExample>
138+
117139
</Examples>
118140
</DemoPage>

0 commit comments

Comments
 (0)