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

fix(DataTable): failed to update the selection by Selected parameter #2130

Merged
merged 1 commit into from
Sep 4, 2024
Merged
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
28 changes: 17 additions & 11 deletions src/Masa.Blazor/Components/DataIterator/MDataIterator.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,7 @@ public partial class MDataIterator<TItem> : MData<TItem>
/// Gets or sets the selected items.
/// </summary>
[Parameter, Obsolete("Use Selected instead.")]
public IEnumerable<TItem>? Value
{
get => GetValue<IEnumerable<TItem>>();
set => SetValue(value);
}
public IEnumerable<TItem>? Value { get; set; }

[Parameter, Obsolete("Use SelectedChanged instead.")]
public EventCallback<IEnumerable<TItem>> ValueChanged { get; set; }
Expand Down Expand Up @@ -112,6 +108,9 @@ public IEnumerable<TItem>? Value
[Parameter]
public EventCallback<(IEnumerable<TItem>, bool)> OnToggleSelectAll { get; set; }

private IEnumerable<TItem>? _prevValue;
private IEnumerable<string>? _prevSelected;

public bool EveryItem => SelectableItems.Any() && SelectableItems.All(IsSelected);

public bool SomeItems => SelectableItems.Any(IsSelected);
Expand All @@ -130,7 +129,7 @@ protected override void OnInitialized()
{
base.OnInitialized();

UpdateSelection();
UpdateSelection(init: true);
}

public override Task SetParametersAsync(ParameterView parameters)
Expand All @@ -142,16 +141,23 @@ public override Task SetParametersAsync(ParameterView parameters)
return base.SetParametersAsync(parameters);
}

protected override void RegisterWatchers(PropertyWatcher watcher)
protected override void OnParametersSet()
{
base.RegisterWatchers(watcher);
base.OnParametersSet();

watcher.Watch<IEnumerable<TItem>>(nameof(Value), UpdateSelection)
.Watch<IEnumerable<string>>(nameof(Selected), UpdateSelection);
UpdateSelection();
}

private void UpdateSelection()
private void UpdateSelection(bool init = false)
{
if (!init && ReferenceEquals(_prevSelected, Selected) && ReferenceEquals(_prevValue, Value))
{
return;
}

_prevSelected = Selected;
_prevValue = Value;

List<string>? keys = null;

if (Value is not null)
Expand Down
Loading