2020-07-08 16:12:49 +08:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
2021-02-20 17:50:27 +08:00
|
|
|
|
using AntDesign.TableModels;
|
2020-07-10 17:57:20 +08:00
|
|
|
|
using Microsoft.AspNetCore.Components;
|
2020-06-19 23:06:33 +08:00
|
|
|
|
|
|
|
|
|
namespace AntDesign
|
|
|
|
|
{
|
2020-07-08 16:12:49 +08:00
|
|
|
|
public partial class Table<TItem> : ITable
|
2020-06-19 23:06:33 +08:00
|
|
|
|
{
|
2020-07-10 17:57:20 +08:00
|
|
|
|
[Parameter]
|
|
|
|
|
public IEnumerable<TItem> SelectedRows
|
2020-07-08 16:12:49 +08:00
|
|
|
|
{
|
2020-07-10 17:57:20 +08:00
|
|
|
|
get => _selectedRows;
|
|
|
|
|
set
|
2020-07-08 16:12:49 +08:00
|
|
|
|
{
|
2021-02-20 17:50:27 +08:00
|
|
|
|
_dataSourceCache ??= new Dictionary<int, RowData<TItem>>();
|
|
|
|
|
|
2020-07-10 17:57:20 +08:00
|
|
|
|
if (value != null && value.Any())
|
2020-07-08 16:12:49 +08:00
|
|
|
|
{
|
2020-07-10 17:57:20 +08:00
|
|
|
|
_dataSourceCache.Values.ForEach(x => x.Selected = x.Data.IsIn(value));
|
2020-07-08 16:12:49 +08:00
|
|
|
|
}
|
2020-07-10 17:57:20 +08:00
|
|
|
|
else if (_selectedRows != null)
|
|
|
|
|
{
|
|
|
|
|
_dataSourceCache.Values.ForEach(x => x.Selected = false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_selectedRows = value;
|
2020-07-08 16:12:49 +08:00
|
|
|
|
|
2020-07-10 17:57:20 +08:00
|
|
|
|
StateHasChanged();
|
2020-07-08 16:12:49 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-10 17:57:20 +08:00
|
|
|
|
[Parameter]
|
|
|
|
|
public EventCallback<IEnumerable<TItem>> SelectedRowsChanged { get; set; }
|
|
|
|
|
|
|
|
|
|
private ISelectionColumn _selection;
|
|
|
|
|
private IEnumerable<TItem> _selectedRows;
|
|
|
|
|
|
|
|
|
|
ISelectionColumn ITable.Selection
|
2020-07-08 16:12:49 +08:00
|
|
|
|
{
|
2020-07-10 17:57:20 +08:00
|
|
|
|
get => _selection;
|
|
|
|
|
set => _selection = value;
|
2020-07-08 16:12:49 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void SetSelection(string[] keys)
|
|
|
|
|
{
|
2020-07-10 17:57:20 +08:00
|
|
|
|
_selection.SetSelection(keys);
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-01 23:45:48 +08:00
|
|
|
|
void ITable.SelectionChanged() => SelectionChanged();
|
|
|
|
|
|
|
|
|
|
private void SelectionChanged()
|
2020-07-10 17:57:20 +08:00
|
|
|
|
{
|
|
|
|
|
foreach (var selection in _selection.RowSelections)
|
2020-07-08 16:12:49 +08:00
|
|
|
|
{
|
2020-11-09 22:17:27 +08:00
|
|
|
|
_dataSourceCache[selection.RowData.CacheKey].Selected = selection.Checked;
|
2020-07-08 16:12:49 +08:00
|
|
|
|
}
|
2020-07-10 17:57:20 +08:00
|
|
|
|
|
|
|
|
|
if (SelectedRowsChanged.HasDelegate)
|
2020-07-08 16:12:49 +08:00
|
|
|
|
{
|
2020-07-10 17:57:20 +08:00
|
|
|
|
_selectedRows = _dataSourceCache.Values.Where(x => x.Selected).Select(x => x.Data);
|
|
|
|
|
SelectedRowsChanged.InvokeAsync(_selectedRows);
|
2020-07-08 16:12:49 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2020-06-19 23:06:33 +08:00
|
|
|
|
}
|
|
|
|
|
}
|