mirror of
https://gitee.com/ant-design-blazor/ant-design-blazor.git
synced 2024-12-16 01:41:14 +08:00
a09a8e6ac3
* docs: correct parameter name * feat(module: table): add data index * refactor(module: table): make DataIndex expression cached * refactor(module: core): recursive method for PropertyAccessHelper.GetRootParameterExpression * refactor(module: table): make DataIndex feature clearer * feat(module: table): support DataIndex format * fix: sort column style Co-authored-by: ElderJames <shunjiey@hotmail.com>
92 lines
2.5 KiB
C#
92 lines
2.5 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Linq.Expressions;
|
|
using System.Reflection;
|
|
|
|
namespace AntDesign.TableModels
|
|
{
|
|
public class DataIndexSortModel<TField> : ITableSortModel, IComparer<TField>
|
|
{
|
|
private readonly LambdaExpression _propertySelect;
|
|
|
|
private readonly Func<TField, TField, int> _comparer;
|
|
|
|
public SortType SortType { get; private set; }
|
|
|
|
public int Priority { get; }
|
|
|
|
public string FieldName { get; }
|
|
|
|
public DataIndexSortModel(string dataIndex, LambdaExpression propertySelect, int priority, string sort, Func<TField, TField, int> comparer)
|
|
{
|
|
this.FieldName = dataIndex;
|
|
this._propertySelect = propertySelect;
|
|
this._comparer = comparer;
|
|
this.Priority = priority;
|
|
this.SortType = SortType.Parse(sort) ?? SortType.None;
|
|
}
|
|
|
|
IOrderedQueryable<TItem> ITableSortModel.Sort<TItem>(IQueryable<TItem> source)
|
|
{
|
|
if (SortType == SortType.None)
|
|
{
|
|
return source as IOrderedQueryable<TItem>;
|
|
}
|
|
|
|
var lambda = (Expression<Func<TItem, TField>>)_propertySelect;
|
|
|
|
if (SortType == SortType.Ascending)
|
|
{
|
|
return _comparer == null ? source.OrderBy(lambda) : source.OrderBy(lambda, this);
|
|
}
|
|
else
|
|
{
|
|
return _comparer == null ? source.OrderByDescending(lambda) : source.OrderByDescending(lambda, this);
|
|
}
|
|
}
|
|
|
|
void ITableSortModel.SwitchSortType()
|
|
{
|
|
SortType = GetNextType();
|
|
}
|
|
|
|
void ITableSortModel.SetSortType(SortType sortType)
|
|
{
|
|
this.SortType = sortType;
|
|
}
|
|
|
|
void ITableSortModel.SetSortType(string sortType)
|
|
{
|
|
this.SortType = SortType.Parse(sortType);
|
|
}
|
|
|
|
SortType ITableSortModel.NextType()
|
|
{
|
|
return GetNextType();
|
|
}
|
|
|
|
private SortType GetNextType()
|
|
{
|
|
if (SortType == SortType.None)
|
|
{
|
|
return SortType.Ascending;
|
|
}
|
|
else if (SortType == SortType.Ascending)
|
|
{
|
|
return SortType.Descending;
|
|
}
|
|
else
|
|
{
|
|
return SortType.None;
|
|
}
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public int Compare(TField x, TField y)
|
|
{
|
|
return _comparer?.Invoke(x, y) ?? 0;
|
|
}
|
|
}
|
|
}
|