feat(module: table): support multiple sorter (#1019)

This commit is contained in:
James Yeung 2021-01-22 00:45:15 +08:00 committed by GitHub
parent e59a2b2540
commit bbc69f25af
5 changed files with 53 additions and 7 deletions

View File

@ -39,6 +39,9 @@ namespace AntDesign
[Parameter]
public Func<TData, TData, int> SorterCompare { get; set; }
[Parameter]
public int SorterMultiple { get; set; }
[Parameter]
public bool ShowSorterTooltip { get; set; } = true;
@ -56,6 +59,11 @@ namespace AntDesign
{
base.OnInitialized();
if (!Sortable)
{
Sortable = SorterMultiple != default || SorterCompare != default || Sort != default;
}
if (IsHeader)
{
if (FieldExpression != null)
@ -63,7 +71,7 @@ namespace AntDesign
_propertyReflector = PropertyReflector.Create(FieldExpression);
if (Sortable)
{
SortModel = new SortModel<TData>(_propertyReflector.Value.PropertyInfo, 1, Sort, SorterCompare);
SortModel = new SortModel<TData>(_propertyReflector.Value.PropertyInfo, SorterMultiple, Sort, SorterCompare);
}
}
else

View File

@ -27,7 +27,7 @@ namespace AntDesign.Internal
private static ColumnCacheItem CreateDataIndexConfig(ColumnCacheKey key)
{
var (itemType, propType, dataIndex, sortable, sort, sorterCompare) = key;
var (itemType, propType, dataIndex, sortable, sort, sorterMultiple, sorterCompare) = key;
Func<RowData, TProp> getValue = null;
ITableSortModel sortModel = null;
var properties = dataIndex?.Split(".");
@ -50,7 +50,7 @@ namespace AntDesign.Internal
var propertySelector = isNullable
? PropertyAccessHelper.BuildNullablePropertyAccessExpression(itemType, properties)
: PropertyAccessHelper.BuildPropertyAccessExpression(itemType, properties);
sortModel = new DataIndexSortModel<TProp>(dataIndex, propertySelector, 1, sort, sorterCompare);
sortModel = new DataIndexSortModel<TProp>(dataIndex, propertySelector, sorterMultiple, sort, sorterCompare);
}
}
@ -69,30 +69,34 @@ namespace AntDesign.Internal
internal readonly string Sort;
internal readonly int SorterMultiple;
internal readonly Func<TProp, TProp, int> SorterCompare;
internal static ColumnCacheKey Create(Column<TProp> column)
{
return new(column.ItemType, typeof(TProp), column.DataIndex, column.Sortable, column.Sort, column.SorterCompare);
return new(column.ItemType, typeof(TProp), column.DataIndex, column.Sortable, column.Sort, column.SorterMultiple, column.SorterCompare);
}
internal ColumnCacheKey(Type itemType, Type propType, string dataIndex, bool sortable, string sort, Func<TProp, TProp, int> sorterCompare)
internal ColumnCacheKey(Type itemType, Type propType, string dataIndex, bool sortable, string sort, int sorterMultiple, Func<TProp, TProp, int> sorterCompare)
{
ItemType = itemType;
PropType = propType;
DataIndex = dataIndex;
Sortable = sortable;
Sort = sort;
SorterMultiple = sorterMultiple;
SorterCompare = sorterCompare;
}
internal void Deconstruct(out Type itemType, out Type propType, out string dataIndex, out bool sortable, out string sort, out Func<TProp, TProp, int> sorterCompare)
internal void Deconstruct(out Type itemType, out Type propType, out string dataIndex, out bool sortable, out string sort, out int sorterMultiple, out Func<TProp, TProp, int> sorterCompare)
{
itemType = ItemType;
propType = PropType;
dataIndex = DataIndex;
sortable = Sortable;
sort = Sort;
sorterMultiple = SorterMultiple;
sorterCompare = SorterCompare;
}
}

View File

@ -178,7 +178,8 @@ namespace AntDesign
if (_dataSource != null)
{
var query = _dataSource.AsQueryable();
foreach (var sort in queryModel.SortModel)
var orderedSortModels = queryModel.SortModel.OrderBy(x => x.Priority);
foreach (var sort in orderedSortModels)
{
query = sort.Sort(query);
}

View File

@ -0,0 +1,18 @@
<Table DataSource="data">
<Column Title="Name" DataIndex="name" TData="string" />
<Column Title="Chinese Score" DataIndex="chinese" TData="int" SorterMultiple="3" SorterCompare="(a,b)=>a-b"/>
<Column Title="Math Score" DataIndex="math" TData="int" SorterMultiple="2" SorterCompare="(a,b)=>a-b"/>
<Column Title="English Score" DataIndex="english" TData="int" SorterMultiple="1" SorterCompare="(a,b)=>a-b"/>
</Table>
@code {
public record Data(string Name,int Chinese,int Math,int English);
public Data[] data = new Data[]
{
new("John Brown",98,60,70),
new("Jim Green",98,66,89),
new("Joe Black",98,90,70),
new("Jim Red",88,99,89),
};
}

View File

@ -0,0 +1,15 @@
---
order: 7
title:
en-US: Multiple sorter
zh-CN: 多列排序
---
## zh-CN
`Column` 支持 `SorterMultiple` 字段以配置多列排序优先级。通过 `SorterCompare` 配置排序逻辑,你可以通过不设置该函数只启动多列排序的交互形式。
## en-US
`Column` support `SorterMultiple` to config the priority of sort columns. Though `SorterCompare` to customize compare function. You can also leave it empty to use the interactive only.