ant-design-blazor/components/table/Table.razor.RowData.cs
anranruye 45b47d49b6 fix(module: table): fix selection (#1973)
* fix(module:table): can not select all

* fix(module:table): do not remain selection after change pageIndex

* Update table.raz
2021-10-10 21:44:21 +08:00

49 lines
1.4 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using AntDesign.TableModels;
namespace AntDesign
{
public partial class Table<TItem> : ITable
{
private IDictionary<int, RowData<TItem>> _dataSourceCache;
private void FlushCache()
{
if (_dataSourceCache == null)
{
_dataSourceCache = new Dictionary<int, RowData<TItem>>();
}
else
{
_dataSourceCache.Clear();
}
}
int[] ITable.GetSelectedCacheKeys()
{
return _dataSourceCache.Where(x => x.Value.Selected).Select(x => x.Key).ToArray();
}
private void FinishLoadPage()
{
if (_selection == null)
return;
// Clear cached items that are not on current page
var currentPageCacheKeys = _selection.RowSelections.Select(x => x.RowData.CacheKey).ToHashSet();
var deletedCaches = _dataSourceCache.Where(x => !currentPageCacheKeys.Contains(x.Key)).ToList();
var needInvokeChange = deletedCaches.Any(x => x.Value.Selected);
deletedCaches.ForEach(x => _dataSourceCache.Remove(x));
_selection?.StateHasChanged();
if (needInvokeChange)
{
SelectionChanged();
}
}
}
}