Skip to content

QuickGrid: Adds EmptyContentTemplate #62603

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
@namespace Microsoft.AspNetCore.Components.QuickGrid
@typeparam TGridItem

@{
InternalGridContext.Grid.SetEmptyContent(this);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using Microsoft.AspNetCore.Components.QuickGrid.Infrastructure;

namespace Microsoft.AspNetCore.Components.QuickGrid;

/// <summary>
/// A component that is displayed when the <see cref="QuickGrid{TGridItem}"/> has no data available.
/// </summary>
/// <typeparam name="TGridItem">The type of data represented by each row in the grid.</typeparam>
public partial class EmptyContentTemplate<TGridItem>
{
[CascadingParameter] internal InternalGridContext<TGridItem> InternalGridContext { get; set; } = default!;

/// <summary>
/// Defines the content to be rendered by <see cref="QuickGrid{TGridItem}"/> when no data is available.
/// </summary>
[Parameter] public RenderFragment? ChildContent { get; set; }
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
#nullable enable
~override Microsoft.AspNetCore.Components.QuickGrid.EmptyContentTemplate<TGridItem>.BuildRenderTree(Microsoft.AspNetCore.Components.Rendering.RenderTreeBuilder __builder) -> void
Microsoft.AspNetCore.Components.QuickGrid.EmptyContentTemplate<TGridItem>
Microsoft.AspNetCore.Components.QuickGrid.EmptyContentTemplate<TGridItem>.ChildContent.get -> Microsoft.AspNetCore.Components.RenderFragment?
Microsoft.AspNetCore.Components.QuickGrid.EmptyContentTemplate<TGridItem>.ChildContent.set -> void
Microsoft.AspNetCore.Components.QuickGrid.EmptyContentTemplate<TGridItem>.EmptyContentTemplate() -> void
Microsoft.AspNetCore.Components.QuickGrid.QuickGrid<TGridItem>.EmptyContent.get -> Microsoft.AspNetCore.Components.RenderFragment?
Microsoft.AspNetCore.Components.QuickGrid.QuickGrid<TGridItem>.EmptyContent.set -> void
Microsoft.AspNetCore.Components.QuickGrid.QuickGrid<TGridItem>.HideColumnOptionsAsync() -> System.Threading.Tasks.Task!
Microsoft.AspNetCore.Components.QuickGrid.QuickGrid<TGridItem>.RowClass.get -> System.Func<TGridItem, string?>?
Microsoft.AspNetCore.Components.QuickGrid.QuickGrid<TGridItem>.RowClass.set -> void
Microsoft.AspNetCore.Components.QuickGrid.QuickGrid<TGridItem>.RowClass.set -> void
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,13 @@
@if (Virtualize)
{
<Virtualize @ref="@_virtualizeComponent"
TItem="(int RowIndex, TGridItem Data)"
ItemSize="@ItemSize"
OverscanCount="@OverscanCount"
ItemsProvider="@ProvideVirtualizedItems"
ItemContent="@(item => builder => RenderRow(builder, item.RowIndex, item.Data))"
Placeholder="@(placeholderContext => builder => RenderPlaceholderRow(builder, placeholderContext))" />
TItem="(int RowIndex, TGridItem Data)"
ItemSize="@ItemSize"
OverscanCount="@OverscanCount"
ItemsProvider="@ProvideVirtualizedItems"
ItemContent="@(item => builder => RenderRow(builder, item.RowIndex, item.Data))"
EmptyContent="@EmptyContent"
Placeholder="@(placeholderContext => builder => RenderPlaceholderRow(builder, placeholderContext))" />
}
else
{
Expand All @@ -41,9 +42,17 @@
{
var initialRowIndex = 2; // aria-rowindex is 1-based, plus the first row is the header
var rowIndex = initialRowIndex;
foreach (var item in _currentNonVirtualizedViewItems)

if (EmptyContent != null && _currentNonVirtualizedViewItems.Count == 0)
{
RenderEmptyRow(__builder, rowIndex++);
}
else
{
RenderRow(__builder, rowIndex++, item);
foreach (var item in _currentNonVirtualizedViewItems)
{
RenderRow(__builder, rowIndex++, item);
}
}

// When pagination is enabled, by default ensure we render the exact number of expected rows per page,
Expand All @@ -63,6 +72,15 @@
}
}

private void RenderEmptyRow(RenderTreeBuilder __builder, int rowIndex)
{
<tr aria-rowindex="@rowIndex">
<td colspan="@_columns.Count">
@EmptyContent
</td>
</tr>
}

private void RenderRow(RenderTreeBuilder __builder, int rowIndex, TGridItem item)
{
var rowClass = RowClass?.Invoke(item);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,13 @@ public partial class QuickGrid<TGridItem> : IAsyncDisposable
// We cascade the InternalGridContext to descendants, which in turn call it to add themselves to _columns
// This happens on every render so that the column list can be updated dynamically
private readonly InternalGridContext<TGridItem> _internalGridContext;

/// <summary>
/// Gets or sets a <see cref="RenderFragment" /> that will be rendered when no data is available to display.
/// This allows derived components to specify a default if the user does not specify it.
/// </summary>
protected internal RenderFragment? EmptyContent { get; protected set; }

private readonly List<ColumnBase<TGridItem>> _columns;
private bool _collectingColumns; // Columns might re-render themselves arbitrarily. We only want to capture them at a defined time.

Expand Down Expand Up @@ -216,6 +223,11 @@ protected override async Task OnAfterRenderAsync(bool firstRender)
}
}

internal void SetEmptyContent(EmptyContentTemplate<TGridItem> emptyContentTemplate)
{
EmptyContent = emptyContentTemplate.ChildContent;
}

// Invoked by descendant columns at a special time during rendering
internal void AddColumn(ColumnBase<TGridItem> column, SortDirection? initialSortDirection, bool isDefaultSortColumn)
{
Expand Down
Loading