LongNeckTimmy
LongNeckTimmy

Reputation: 167

MudBlazor DataGrid - Issue Selecting All Items

I have a MudBlazor DataGrid and configured the SelectColumn/SelectedItems feature. I noticed that when I click the top checkbox to select all the items nothing was being added to my SelectedItems list.

I implemented the SelectedItemsChanged callback and threw in a break point to see if that was being called when I clicked the top checkbox. Turns out when I click the top check box that callback method is called twice. The first time its called the incoming HashSet has all the items in it. The second time its called the incoming HashSet has zero items, thus nulling out the SelectedItems list.

It works as expected when selecting individual items, but when I click the top checkbox it doesn't seem to add all the items to my list. I put together a code snippet to demonstrate this issue, does anyone have any idea what's going on here? Thanks! :)

https://try.mudblazor.com/snippet/mEQIOecHUKSGCnRG

Upvotes: 1

Views: 848

Answers (1)

Uerschel
Uerschel

Reputation: 827

Either use the two way bindable @bind-SelectedItems or set the selected Values in SelectedItemsChanged. If not the value of selectedItems is not overwritten

<MudDataGrid Items="@Items" T="Test" MultiSelection="true" @bind-SelectedItems="@SelectedItems">

Or

protected void SelectedItemsChangedCallback(HashSet<Test> SelItems) 
{
    SelectedItems = SelItems;
}

MudSnippet

In your case when you select all, SelectedItemsChanged is called with all items selected. After that the unchanged SelectedItems are read and overwrite the SelectedItems in the DataGrid. SelectedItemsChanged is again called with no selected items.

Upvotes: 0

Related Questions