mirror of
https://gitee.com/ant-design-blazor/ant-design-blazor.git
synced 2024-12-05 13:37:35 +08:00
080d4dda43
* feat: table column sort add sortway support singleness or mutiple * fix: error method name * add sort directions * delete useless property Co-authored-by: ElderJames <shunjiey@hotmail.com>
162 lines
4.8 KiB
C#
162 lines
4.8 KiB
C#
using System;
|
|
using System.Linq;
|
|
using System.Linq.Expressions;
|
|
using AntDesign.Core.Reflection;
|
|
using AntDesign.Internal;
|
|
using AntDesign.TableModels;
|
|
using Microsoft.AspNetCore.Components;
|
|
|
|
namespace AntDesign
|
|
{
|
|
public partial class Column<TData> : ColumnBase, IFieldColumn
|
|
{
|
|
[CascadingParameter(Name = "ItemType")]
|
|
public Type ItemType { get; set; }
|
|
|
|
[Parameter]
|
|
public EventCallback<TData> FieldChanged { get; set; }
|
|
|
|
[Parameter]
|
|
public Expression<Func<TData>> FieldExpression { get; set; }
|
|
|
|
[Parameter]
|
|
public RenderFragment<TData> CellRender { get; set; }
|
|
|
|
[Parameter]
|
|
public TData Field { get; set; }
|
|
|
|
[Parameter]
|
|
public string DataIndex { get; set; }
|
|
|
|
[Parameter]
|
|
public string Format { get; set; }
|
|
|
|
[Parameter]
|
|
public bool Sortable { get; set; }
|
|
|
|
[Parameter]
|
|
public Func<TData, TData, int> SorterCompare { get; set; }
|
|
|
|
[Parameter]
|
|
public int SorterMultiple { get; set; }
|
|
|
|
[Parameter]
|
|
public bool ShowSorterTooltip { get; set; } = true;
|
|
|
|
[Parameter]
|
|
public SortDirection[] SortDirections { get; set; }
|
|
|
|
[Parameter]
|
|
public SortDirection DefaultSortOrder { get; set; }
|
|
|
|
private PropertyReflector? _propertyReflector;
|
|
|
|
public string DisplayName => _propertyReflector?.DisplayName;
|
|
|
|
public string FieldName => _propertyReflector?.PropertyName;
|
|
|
|
public ITableSortModel SortModel { get; private set; }
|
|
|
|
private SortDirection _sortDirection;
|
|
|
|
public Func<RowData, TData> GetValue { get; private set; }
|
|
|
|
void IFieldColumn.ClearSorter() => SetSorter(SortDirection.None);
|
|
|
|
protected override void OnInitialized()
|
|
{
|
|
base.OnInitialized();
|
|
|
|
SortDirections ??= Table.SortDirections;
|
|
Sortable = Sortable || SorterMultiple != default || SorterCompare != default || DefaultSortOrder != default || SortDirections?.Any() == true;
|
|
|
|
if (IsHeader)
|
|
{
|
|
if (FieldExpression != null)
|
|
{
|
|
_propertyReflector = PropertyReflector.Create(FieldExpression);
|
|
}
|
|
|
|
if (Sortable)
|
|
{
|
|
if (_propertyReflector.HasValue)
|
|
{
|
|
SortModel = new SortModel<TData>(_propertyReflector.Value.PropertyInfo, SorterMultiple, DefaultSortOrder, SorterCompare);
|
|
}
|
|
else
|
|
{
|
|
(GetValue, SortModel) = ColumnDataIndexHelper<TData>.GetDataIndexConfig(this);
|
|
}
|
|
}
|
|
}
|
|
else if (IsBody)
|
|
{
|
|
SortModel = Context.HeaderColumns[ColIndex] is IFieldColumn fieldColumn ? fieldColumn.SortModel : null;
|
|
|
|
(GetValue, _) = ColumnDataIndexHelper<TData>.GetDataIndexConfig(this);
|
|
}
|
|
|
|
Sortable = Sortable || SortModel != null;
|
|
_sortDirection = SortModel?.SortDirection ?? DefaultSortOrder ?? SortDirection.None;
|
|
|
|
ClassMapper
|
|
.If("ant-table-column-has-sorters", () => Sortable)
|
|
.If($"ant-table-column-sort", () => Sortable && SortModel != null && SortModel.SortDirection.IsIn(SortDirection.Ascending, SortDirection.Descending));
|
|
}
|
|
|
|
private void HandelHeaderClick()
|
|
{
|
|
if (Sortable)
|
|
{
|
|
SetSorter(NextSortDirection());
|
|
Table.ColumnSorterChange(this);
|
|
}
|
|
}
|
|
|
|
private string SorterTooltip
|
|
{
|
|
get
|
|
{
|
|
var next = NextSortDirection();
|
|
return next?.Value switch
|
|
{
|
|
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)
|
|
{
|
|
return SortDirection.None;
|
|
}
|
|
|
|
return SortDirections[index + 1];
|
|
}
|
|
}
|
|
|
|
private void SetSorter(SortDirection sortDirection)
|
|
{
|
|
_sortDirection = sortDirection;
|
|
SortModel.SetSortDirection(sortDirection);
|
|
}
|
|
|
|
private void ToggleTreeNode()
|
|
{
|
|
RowData.Expanded = !RowData.Expanded;
|
|
Table?.Refresh();
|
|
}
|
|
}
|
|
}
|