2021-06-15 18:22:07 +08:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2020-07-08 16:12:49 +08:00
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-15 18:22:07 +08:00
|
|
|
|
_selectedRows = _dataSourceCache.Values.Where(x => x.Selected).Select(x => x.Data);
|
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;
|
|
|
|
|
|
2021-06-15 18:22:07 +08:00
|
|
|
|
private void RowDataSelectedChanged(RowData rowData, bool selected)
|
|
|
|
|
{
|
|
|
|
|
_selectedRows = _dataSourceCache.Values.Where(x => x.Selected).Select(x => x.Data);
|
|
|
|
|
if (SelectedRowsChanged.HasDelegate)
|
|
|
|
|
{
|
|
|
|
|
SelectedRowsChanged.InvokeAsync(_selectedRows);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-10 17:57:20 +08:00
|
|
|
|
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)
|
|
|
|
|
{
|
2021-06-15 18:22:07 +08:00
|
|
|
|
if (_selection == null)
|
|
|
|
|
{
|
|
|
|
|
if (keys == null || !keys.Any())
|
|
|
|
|
{
|
|
|
|
|
_dataSourceCache.Values.ForEach(x => x.Selected = false);
|
|
|
|
|
StateHasChanged();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
throw new NotSupportedException("To use SetSelection method for a table, you should add a Selection component to the column definition.");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
_selection.SetSelection(keys);
|
|
|
|
|
}
|
2020-07-10 17:57:20 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-08-01 23:45:48 +08:00
|
|
|
|
void ITable.SelectionChanged() => SelectionChanged();
|
|
|
|
|
|
|
|
|
|
private void SelectionChanged()
|
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
|
|
|
|
}
|
2021-06-15 18:22:07 +08:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
StateHasChanged();
|
|
|
|
|
}
|
2020-07-08 16:12:49 +08:00
|
|
|
|
}
|
2020-06-19 23:06:33 +08:00
|
|
|
|
}
|
|
|
|
|
}
|