Skip to content

Commit 58210d4

Browse files
QuickGrid: Adds EmptyContentTemplate #59078 #50498
1 parent 062e663 commit 58210d4

File tree

4 files changed

+75
-29
lines changed

4 files changed

+75
-29
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
@namespace Microsoft.AspNetCore.Components.QuickGrid
2+
@typeparam TGridItem
3+
4+
@{
5+
InternalGridContext.Grid.SetEmptyContent(this);
6+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
using Microsoft.AspNetCore.Components.QuickGrid.Infrastructure;
2+
3+
namespace Microsoft.AspNetCore.Components.QuickGrid;
4+
5+
public partial class EmptyContentTemplate<TGridItem>
6+
{
7+
[CascadingParameter] internal InternalGridContext<TGridItem> InternalGridContext { get; set; } = default!;
8+
9+
[Parameter] public RenderFragment ChildContent { get; set; }
10+
}

src/Components/QuickGrid/Microsoft.AspNetCore.Components.QuickGrid/src/QuickGrid.razor

Lines changed: 47 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,13 @@
2020
@if (Virtualize)
2121
{
2222
<Virtualize @ref="@_virtualizeComponent"
23-
TItem="(int RowIndex, TGridItem Data)"
24-
ItemSize="@ItemSize"
25-
OverscanCount="@OverscanCount"
26-
ItemsProvider="@ProvideVirtualizedItems"
27-
ItemContent="@(item => builder => RenderRow(builder, item.RowIndex, item.Data))"
28-
Placeholder="@(placeholderContext => builder => RenderPlaceholderRow(builder, placeholderContext))" />
23+
TItem="(int RowIndex, TGridItem Data)"
24+
ItemSize="@ItemSize"
25+
OverscanCount="@OverscanCount"
26+
ItemsProvider="@ProvideVirtualizedItems"
27+
ItemContent="@(item => builder => RenderRow(builder, item.RowIndex, item.Data))"
28+
EmptyContent="@EmptyContent"
29+
Placeholder="@(placeholderContext => builder => RenderPlaceholderRow(builder, placeholderContext))" />
2930
}
3031
else
3132
{
@@ -41,9 +42,17 @@
4142
{
4243
var initialRowIndex = 2; // aria-rowindex is 1-based, plus the first row is the header
4344
var rowIndex = initialRowIndex;
44-
foreach (var item in _currentNonVirtualizedViewItems)
45+
46+
if (EmptyContent != null && _currentNonVirtualizedViewItems.Count == 0)
4547
{
46-
RenderRow(__builder, rowIndex++, item);
48+
RenderEmptyRow(__builder, rowIndex++);
49+
}
50+
else
51+
{
52+
foreach (var item in _currentNonVirtualizedViewItems)
53+
{
54+
RenderRow(__builder, rowIndex++, item);
55+
}
4756
}
4857

4958
// When pagination is enabled, by default ensure we render the exact number of expected rows per page,
@@ -63,39 +72,48 @@
6372
}
6473
}
6574

75+
private void RenderEmptyRow(RenderTreeBuilder __builder, int rowIndex)
76+
{
77+
<tr aria-rowindex="@rowIndex">
78+
<td colspan="@_columns.Count">
79+
@EmptyContent
80+
</td>
81+
</tr>
82+
}
83+
6684
private void RenderRow(RenderTreeBuilder __builder, int rowIndex, TGridItem item)
6785
{
6886
var rowClass = RowClass?.Invoke(item);
6987
<tr @key="@(ItemKey(item))" aria-rowindex="@rowIndex" class="@rowClass">
7088
@foreach (var col in _columns)
7189
{
7290
<td class="@ColumnClass(col)" @key="@col">@{ col.CellContent(__builder, item); }</td>
73-
}
74-
</tr>
75-
}
91+
}
92+
</tr>
93+
}
7694

77-
private void RenderPlaceholderRow(RenderTreeBuilder __builder, PlaceholderContext placeholderContext)
78-
{
79-
<tr aria-rowindex="@(placeholderContext.Index + 1)">
80-
@foreach (var col in _columns)
81-
{
82-
<td class="grid-cell-placeholder @ColumnClass(col)" @key="@col">@{ col.RenderPlaceholderContent(__builder, placeholderContext); }</td>
83-
}
84-
</tr>
95+
private void RenderPlaceholderRow(RenderTreeBuilder __builder, PlaceholderContext placeholderContext)
96+
{
97+
<tr aria-rowindex="@(placeholderContext.Index + 1)">
98+
@foreach (var col in _columns)
99+
{
100+
<td class="grid-cell-placeholder @ColumnClass(col)" @key="@col">@{ col.RenderPlaceholderContent(__builder, placeholderContext); }</td>
101+
}
102+
</tr>
85103
}
86104

87105
private void RenderColumnHeaders(RenderTreeBuilder __builder)
106+
{
107+
foreach (var col in _columns)
88108
{
89-
foreach (var col in _columns)
90-
{
91-
<th class="@ColumnHeaderClass(col)" aria-sort="@AriaSortValue(col)" @key="@col" scope="col">
92-
<div class="col-header-content">@col.HeaderContent</div>
109+
<th class="@ColumnHeaderClass(col)" aria-sort="@AriaSortValue(col)" @key="@col" scope="col">
110+
<div class="col-header-content">@col.HeaderContent</div>
93111

94-
@if (col == _displayOptionsForColumn)
95-
{
96-
<div class="col-options">@col.ColumnOptions</div>
97-
}
98-
</th>
99-
}
112+
@if (col == _displayOptionsForColumn)
113+
{
114+
<div class="col-options">@col.ColumnOptions</div>
115+
}
116+
</th>
100117
}
101118
}
119+
}

src/Components/QuickGrid/Microsoft.AspNetCore.Components.QuickGrid/src/QuickGrid.razor.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,13 @@ public partial class QuickGrid<TGridItem> : IAsyncDisposable
124124
// We cascade the InternalGridContext to descendants, which in turn call it to add themselves to _columns
125125
// This happens on every render so that the column list can be updated dynamically
126126
private readonly InternalGridContext<TGridItem> _internalGridContext;
127+
128+
/// <summary>
129+
/// Gets or sets a <see cref="RenderFragment" /> that will be rendered when no data is available to display.
130+
/// This allows derived components to specify a default if the user does not specify it.
131+
/// </summary>
132+
protected internal RenderFragment? EmptyContent { get; protected set; }
133+
127134
private readonly List<ColumnBase<TGridItem>> _columns;
128135
private bool _collectingColumns; // Columns might re-render themselves arbitrarily. We only want to capture them at a defined time.
129136

@@ -216,6 +223,11 @@ protected override async Task OnAfterRenderAsync(bool firstRender)
216223
}
217224
}
218225

226+
internal void SetEmptyContent(EmptyContentTemplate<TGridItem> emptyContentTemplate)
227+
{
228+
EmptyContent = emptyContentTemplate.ChildContent;
229+
}
230+
219231
// Invoked by descendant columns at a special time during rendering
220232
internal void AddColumn(ColumnBase<TGridItem> column, SortDirection? initialSortDirection, bool isDefaultSortColumn)
221233
{

0 commit comments

Comments
 (0)