Skip to content
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

Set selected items in grid view when using the grid to edit data #957

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
24 changes: 18 additions & 6 deletions blazorbootstrap/Components/Grid/Grid.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -186,10 +186,8 @@ internal async Task RefreshDataAsync(bool firstRender = false, CancellationToken

if (AllowSelection)
{
PrepareCheckboxIds();

if (!firstRender)
await RefreshSelectionAsync();
PrepareCheckboxIds();
await RefreshSelectionAsync();
}

requestInProgress = false;
Expand Down Expand Up @@ -383,6 +381,7 @@ private async Task OnHeaderCheckboxChanged(ChangeEventArgs args)
allItemsSelected = bool.TryParse(args?.Value?.ToString(), out var checkboxState) && checkboxState;
selectedItems = allItemsSelected ? new HashSet<TItem>(items!) : new HashSet<TItem>();
SelectedItemsCount = selectedItems.Count;
SetSelected = selectedItems;
await CheckOrUnCheckAll();

if (SelectedItemsChanged.HasDelegate)
Expand Down Expand Up @@ -456,8 +455,8 @@ private void PrepareCheckboxIds()
private async Task RefreshSelectionAsync()
{
selectedItems = (items?.Count ?? 0) == 0
? new HashSet<TItem>()
: selectedItems?.Intersect(items!).ToHashSet() ?? new HashSet<TItem>();
? new HashSet<TItem>()
: selectedItems?.Intersect(items!).ToHashSet() ?? new HashSet<TItem>();

SelectedItemsCount = selectedItems.Count;
allItemsSelected = SelectedItemsCount > 0 && items!.Count == SelectedItemsCount;
Expand Down Expand Up @@ -584,6 +583,19 @@ private void SetFilters(IEnumerable<FilterItem> filterItems)
[Parameter]
public bool AllowRowClick { get; set; }

/// <summary>
/// Gets or sets the grid set selection.
/// </summary>
/// <remarks>
/// Default value is false.
/// </remarks>
[Parameter]
public HashSet<TItem> SetSelected
{
get => selectedItems;
set => selectedItems = value;
}

/// <summary>
/// Gets or sets the grid selection.
/// </summary>
Expand Down