2020-05-30 01:46:41 +08:00
|
|
|
|
using System;
|
2021-03-01 23:47:16 +08:00
|
|
|
|
using System.Collections.Generic;
|
2021-01-24 13:52:57 +08:00
|
|
|
|
using System.Linq;
|
2021-02-27 23:55:48 +08:00
|
|
|
|
using System.Collections.Generic;
|
2020-05-30 01:46:41 +08:00
|
|
|
|
using System.Linq.Expressions;
|
2020-09-01 17:23:31 +08:00
|
|
|
|
using AntDesign.Core.Reflection;
|
2021-01-20 23:35:57 +08:00
|
|
|
|
using AntDesign.Internal;
|
2020-07-08 16:12:49 +08:00
|
|
|
|
using AntDesign.TableModels;
|
2020-05-30 01:46:41 +08:00
|
|
|
|
using Microsoft.AspNetCore.Components;
|
|
|
|
|
|
|
|
|
|
namespace AntDesign
|
|
|
|
|
{
|
2020-06-19 23:06:33 +08:00
|
|
|
|
public partial class Column<TData> : ColumnBase, IFieldColumn
|
2020-05-30 01:46:41 +08:00
|
|
|
|
{
|
2021-01-20 23:35:57 +08:00
|
|
|
|
[CascadingParameter(Name = "ItemType")]
|
|
|
|
|
public Type ItemType { get; set; }
|
|
|
|
|
|
2020-05-30 01:46:41 +08:00
|
|
|
|
[Parameter]
|
2020-06-05 16:06:23 +08:00
|
|
|
|
public EventCallback<TData> FieldChanged { get; set; }
|
2020-05-30 01:46:41 +08:00
|
|
|
|
|
|
|
|
|
[Parameter]
|
2020-06-05 16:06:23 +08:00
|
|
|
|
public Expression<Func<TData>> FieldExpression { get; set; }
|
2020-05-30 01:46:41 +08:00
|
|
|
|
|
|
|
|
|
[Parameter]
|
2020-06-05 16:06:23 +08:00
|
|
|
|
public RenderFragment<TData> CellRender { get; set; }
|
2020-06-02 19:15:15 +08:00
|
|
|
|
|
|
|
|
|
[Parameter]
|
2020-06-19 23:06:33 +08:00
|
|
|
|
public TData Field { get; set; }
|
2020-05-30 01:46:41 +08:00
|
|
|
|
|
2021-01-20 23:35:57 +08:00
|
|
|
|
[Parameter]
|
|
|
|
|
public string DataIndex { get; set; }
|
|
|
|
|
|
2020-07-05 00:09:53 +08:00
|
|
|
|
[Parameter]
|
|
|
|
|
public string Format { get; set; }
|
|
|
|
|
|
2020-07-08 16:12:49 +08:00
|
|
|
|
[Parameter]
|
|
|
|
|
public bool Sortable { get; set; }
|
|
|
|
|
|
2021-01-12 00:39:40 +08:00
|
|
|
|
[Parameter]
|
|
|
|
|
public Func<TData, TData, int> SorterCompare { get; set; }
|
|
|
|
|
|
2021-01-22 00:45:15 +08:00
|
|
|
|
[Parameter]
|
|
|
|
|
public int SorterMultiple { get; set; }
|
|
|
|
|
|
2020-07-08 16:12:49 +08:00
|
|
|
|
[Parameter]
|
|
|
|
|
public bool ShowSorterTooltip { get; set; } = true;
|
|
|
|
|
|
2021-01-24 13:52:57 +08:00
|
|
|
|
[Parameter]
|
|
|
|
|
public SortDirection[] SortDirections { get; set; }
|
|
|
|
|
|
|
|
|
|
[Parameter]
|
|
|
|
|
public SortDirection DefaultSortOrder { get; set; }
|
|
|
|
|
|
2021-03-01 23:47:16 +08:00
|
|
|
|
[Parameter]
|
|
|
|
|
public Func<RowData, Dictionary<string, object>> OnCell { get; set; }
|
|
|
|
|
|
|
|
|
|
[Parameter]
|
|
|
|
|
public Func<Dictionary<string, object>> OnHeaderCell { get; set; }
|
|
|
|
|
|
2021-02-27 23:55:48 +08:00
|
|
|
|
[Parameter]
|
|
|
|
|
public IEnumerable<TableFilter<TData>> Filters { get; set; }
|
|
|
|
|
|
|
|
|
|
[Parameter]
|
|
|
|
|
public bool FilterMultiple { get; set; } = true;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Function that determines if the row is displayed when filtered
|
|
|
|
|
/// <para>
|
|
|
|
|
/// Parameter 1: The value of the filter item
|
|
|
|
|
/// </para>
|
|
|
|
|
/// <para>
|
|
|
|
|
/// Parameter 2: The value of the column
|
|
|
|
|
/// </para>
|
|
|
|
|
/// </summary>
|
|
|
|
|
[Parameter]
|
|
|
|
|
public Expression<Func<TData, TData, bool>> OnFilter { get; set; }
|
|
|
|
|
|
2020-09-01 17:23:31 +08:00
|
|
|
|
private PropertyReflector? _propertyReflector;
|
2020-05-30 01:46:41 +08:00
|
|
|
|
|
2020-09-01 17:23:31 +08:00
|
|
|
|
public string DisplayName => _propertyReflector?.DisplayName;
|
2020-05-30 01:46:41 +08:00
|
|
|
|
|
2020-09-01 17:23:31 +08:00
|
|
|
|
public string FieldName => _propertyReflector?.PropertyName;
|
2020-05-30 01:46:41 +08:00
|
|
|
|
|
2020-07-08 16:12:49 +08:00
|
|
|
|
public ITableSortModel SortModel { get; private set; }
|
|
|
|
|
|
2021-02-27 23:55:48 +08:00
|
|
|
|
public ITableFilterModel FilterModel { get; private set; }
|
|
|
|
|
|
2021-01-24 13:52:57 +08:00
|
|
|
|
private SortDirection _sortDirection;
|
|
|
|
|
|
2021-01-20 23:35:57 +08:00
|
|
|
|
public Func<RowData, TData> GetValue { get; private set; }
|
|
|
|
|
|
2021-01-24 13:52:57 +08:00
|
|
|
|
void IFieldColumn.ClearSorter() => SetSorter(SortDirection.None);
|
|
|
|
|
|
2021-02-27 23:55:48 +08:00
|
|
|
|
private static readonly EventCallbackFactory _callbackFactory = new EventCallbackFactory();
|
|
|
|
|
|
|
|
|
|
private bool _filterOpened;
|
|
|
|
|
private bool _hasFilterSelected;
|
|
|
|
|
private string[] _selectedFilterValues;
|
|
|
|
|
|
|
|
|
|
private ElementReference _filterTriggerRef;
|
|
|
|
|
|
2020-05-30 01:46:41 +08:00
|
|
|
|
protected override void OnInitialized()
|
|
|
|
|
{
|
2020-06-19 23:06:33 +08:00
|
|
|
|
base.OnInitialized();
|
|
|
|
|
|
2021-01-24 13:52:57 +08:00
|
|
|
|
Sortable = Sortable || SorterMultiple != default || SorterCompare != default || DefaultSortOrder != default || SortDirections?.Any() == true;
|
2021-01-22 00:45:15 +08:00
|
|
|
|
|
2021-01-20 23:35:57 +08:00
|
|
|
|
if (IsHeader)
|
2020-05-30 01:46:41 +08:00
|
|
|
|
{
|
2021-01-20 23:35:57 +08:00
|
|
|
|
if (FieldExpression != null)
|
2020-07-08 16:12:49 +08:00
|
|
|
|
{
|
2021-01-20 23:35:57 +08:00
|
|
|
|
_propertyReflector = PropertyReflector.Create(FieldExpression);
|
2020-07-08 16:12:49 +08:00
|
|
|
|
}
|
2021-01-24 13:52:57 +08:00
|
|
|
|
|
|
|
|
|
if (Sortable)
|
2021-01-20 23:35:57 +08:00
|
|
|
|
{
|
2021-01-24 13:52:57 +08:00
|
|
|
|
if (_propertyReflector.HasValue)
|
|
|
|
|
{
|
|
|
|
|
SortModel = new SortModel<TData>(_propertyReflector.Value.PropertyInfo, SorterMultiple, DefaultSortOrder, SorterCompare);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
(GetValue, SortModel) = ColumnDataIndexHelper<TData>.GetDataIndexConfig(this);
|
|
|
|
|
}
|
2021-01-20 23:35:57 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if (IsBody)
|
|
|
|
|
{
|
|
|
|
|
SortModel = Context.HeaderColumns[ColIndex] is IFieldColumn fieldColumn ? fieldColumn.SortModel : null;
|
2021-01-24 13:52:57 +08:00
|
|
|
|
|
2021-01-20 23:35:57 +08:00
|
|
|
|
(GetValue, _) = ColumnDataIndexHelper<TData>.GetDataIndexConfig(this);
|
2020-07-08 16:12:49 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-01-30 22:21:26 +08:00
|
|
|
|
SortDirections ??= Table.SortDirections;
|
|
|
|
|
|
2021-01-24 13:52:57 +08:00
|
|
|
|
Sortable = Sortable || SortModel != null;
|
|
|
|
|
_sortDirection = SortModel?.SortDirection ?? DefaultSortOrder ?? SortDirection.None;
|
|
|
|
|
|
2020-07-08 16:12:49 +08:00
|
|
|
|
ClassMapper
|
2021-01-24 13:52:57 +08:00
|
|
|
|
.If("ant-table-column-has-sorters", () => Sortable)
|
|
|
|
|
.If($"ant-table-column-sort", () => Sortable && SortModel != null && SortModel.SortDirection.IsIn(SortDirection.Ascending, SortDirection.Descending));
|
2020-07-08 16:12:49 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-03-01 23:47:16 +08:00
|
|
|
|
private void HandleSort()
|
2020-07-08 16:12:49 +08:00
|
|
|
|
{
|
|
|
|
|
if (Sortable)
|
|
|
|
|
{
|
2021-01-24 13:52:57 +08:00
|
|
|
|
SetSorter(NextSortDirection());
|
|
|
|
|
Table.ColumnSorterChange(this);
|
2020-07-08 16:12:49 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private string SorterTooltip
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2021-01-24 13:52:57 +08:00
|
|
|
|
var next = NextSortDirection();
|
|
|
|
|
return next?.Value switch
|
2020-07-08 16:12:49 +08:00
|
|
|
|
{
|
2021-01-24 13:52:57 +08:00
|
|
|
|
0 => Table.Locale.CancelSort,
|
|
|
|
|
1 => Table.Locale.TriggerAsc,
|
|
|
|
|
2 => Table.Locale.TriggerDesc,
|
|
|
|
|
_ => Table.Locale.CancelSort
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private SortDirection NextSortDirection()
|
|
|
|
|
{
|
|
|
|
|
if (_sortDirection == SortDirection.None)
|
|
|
|
|
{
|
|
|
|
|
return SortDirections[0];
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
var index = Array.IndexOf(SortDirections, _sortDirection);
|
|
|
|
|
if (index >= SortDirections.Length - 1)
|
2020-07-08 16:12:49 +08:00
|
|
|
|
{
|
2021-01-24 13:52:57 +08:00
|
|
|
|
return SortDirection.None;
|
2020-07-08 16:12:49 +08:00
|
|
|
|
}
|
2021-01-24 13:52:57 +08:00
|
|
|
|
|
|
|
|
|
return SortDirections[index + 1];
|
2020-05-30 01:46:41 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2020-11-09 22:17:27 +08:00
|
|
|
|
|
2021-01-24 13:52:57 +08:00
|
|
|
|
private void SetSorter(SortDirection sortDirection)
|
|
|
|
|
{
|
|
|
|
|
_sortDirection = sortDirection;
|
|
|
|
|
SortModel.SetSortDirection(sortDirection);
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-09 22:17:27 +08:00
|
|
|
|
private void ToggleTreeNode()
|
|
|
|
|
{
|
|
|
|
|
RowData.Expanded = !RowData.Expanded;
|
|
|
|
|
Table?.Refresh();
|
|
|
|
|
}
|
2021-02-27 23:55:48 +08:00
|
|
|
|
|
|
|
|
|
private void FilterSelected(TableFilter<TData> filter)
|
|
|
|
|
{
|
|
|
|
|
if (!FilterMultiple)
|
|
|
|
|
{
|
|
|
|
|
Filters.ForEach(x => x.Selected = false);
|
|
|
|
|
filter.Selected = true;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
filter.Selected = !filter.Selected;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_selectedFilterValues = Filters.Where(x => x.Selected).Select(x => x.Value.ToString()).ToArray();
|
|
|
|
|
StateHasChanged();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void FilterConfirm()
|
|
|
|
|
{
|
|
|
|
|
_filterOpened = false;
|
|
|
|
|
_hasFilterSelected = Filters?.Any(x => x.Selected) == true;
|
|
|
|
|
|
|
|
|
|
FilterModel = _hasFilterSelected ? new FilterModel<TData>(_propertyReflector.Value.PropertyInfo, OnFilter, Filters.Where(x => x.Selected).ToList()) : null;
|
|
|
|
|
|
|
|
|
|
Table?.ReloadAndInvokeChange();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void FilterReset()
|
|
|
|
|
{
|
|
|
|
|
Filters.ForEach(x => x.Selected = false);
|
|
|
|
|
FilterConfirm();
|
|
|
|
|
}
|
2020-05-30 01:46:41 +08:00
|
|
|
|
}
|
2020-06-05 16:06:23 +08:00
|
|
|
|
}
|