From 48dc642af382fd35e84681030383f2b9a8423431 Mon Sep 17 00:00:00 2001 From: capdiem Date: Tue, 2 Jul 2024 10:41:25 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20fix(DataTable):=20adding=20or=20?= =?UTF-8?q?deleting=20items=20doesn't=20refresh=20the=20table=20(#2014)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Masa.Blazor/Components/Data/MData.cs | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/src/Masa.Blazor/Components/Data/MData.cs b/src/Masa.Blazor/Components/Data/MData.cs index 1d8407be82..0ecce7c122 100644 --- a/src/Masa.Blazor/Components/Data/MData.cs +++ b/src/Masa.Blazor/Components/Data/MData.cs @@ -339,7 +339,7 @@ protected override void RegisterWatchers(PropertyWatcher watcher) private bool _prevDisableFiltering; private int _prevServerItemsLength; private IEnumerable>? _prevItemValues; - private IEnumerable? _prevItems; + private HashSet? _prevItems; private string? _prevSearch; private int _prevPageCount; @@ -359,7 +359,7 @@ private void CheckForUpdates() needUpdateComputedItems |= CheckAndUpdate(ref _prevServerItemsLength, ServerItemsLength); needUpdateComputedItems |= CheckAndUpdate(ref _prevDisableFiltering, DisableFiltering); needUpdateComputedItems |= CheckAndUpdate(ref _prevItemValues, ItemValues); - needUpdateComputedItems |= CheckAndUpdate(ref _prevItems, Items); + needUpdateComputedItems |= CheckItemsAndUpdate(ref _prevItems, Items); needUpdateComputedItems |= CheckAndUpdate(ref _prevSearch, Search); if (needUpdateComputedItems) @@ -376,6 +376,26 @@ private bool CheckAndUpdate(ref T prevValue, T currentValue) return true; } + private bool CheckItemsAndUpdate(ref HashSet? prevValue, IEnumerable? currentValue) + { + if (prevValue == null && currentValue == null) return false; + if (prevValue == null || currentValue == null) + { + prevValue = currentValue != null ? [..currentValue] : null; + return true; + } + + var currentSet = new HashSet(currentValue); + + if (!prevValue.SetEquals(currentSet)) + { + prevValue = currentSet; + return true; + } + + return false; + } + protected IList WrapperInArray(OneOf> val) { if (val.Value == null)