2020-07-08 16:12:49 +08:00
|
|
|
|
using System;
|
2021-01-12 00:39:40 +08:00
|
|
|
|
using System.Collections.Generic;
|
2021-04-02 23:54:56 +08:00
|
|
|
|
using System.ComponentModel.DataAnnotations;
|
2020-07-08 16:12:49 +08:00
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Linq.Expressions;
|
2020-09-01 17:23:31 +08:00
|
|
|
|
using System.Reflection;
|
2021-04-02 23:54:56 +08:00
|
|
|
|
using AntDesign.Internal;
|
2020-07-08 16:12:49 +08:00
|
|
|
|
|
|
|
|
|
namespace AntDesign.TableModels
|
|
|
|
|
{
|
2021-01-12 00:39:40 +08:00
|
|
|
|
public class SortModel<TField> : ITableSortModel, IComparer<TField>
|
2020-07-08 16:12:49 +08:00
|
|
|
|
{
|
|
|
|
|
public int Priority { get; }
|
|
|
|
|
|
|
|
|
|
public string FieldName { get; }
|
|
|
|
|
|
2021-02-14 20:15:53 +08:00
|
|
|
|
public string Sort => _sortDirection?.Name;
|
2021-01-24 13:52:57 +08:00
|
|
|
|
|
|
|
|
|
SortDirection ITableSortModel.SortDirection => _sortDirection;
|
|
|
|
|
|
2021-02-27 23:55:48 +08:00
|
|
|
|
private readonly Func<TField, TField, int> _comparer;
|
|
|
|
|
|
2021-01-24 13:52:57 +08:00
|
|
|
|
private SortDirection _sortDirection;
|
|
|
|
|
|
2021-04-02 23:54:56 +08:00
|
|
|
|
private LambdaExpression _getFieldExpression;
|
|
|
|
|
|
2021-04-18 00:27:52 +08:00
|
|
|
|
public SortModel(LambdaExpression getFieldExpression, string fieldName, int priority, SortDirection defaultSortOrder, Func<TField, TField, int> comparer)
|
2020-07-08 16:12:49 +08:00
|
|
|
|
{
|
|
|
|
|
this.Priority = priority;
|
2021-04-02 23:54:56 +08:00
|
|
|
|
this._getFieldExpression = getFieldExpression;
|
2021-04-18 00:27:52 +08:00
|
|
|
|
this.FieldName = fieldName;
|
2021-02-27 23:55:48 +08:00
|
|
|
|
this._comparer = comparer;
|
2021-01-24 13:52:57 +08:00
|
|
|
|
this._sortDirection = defaultSortOrder ?? SortDirection.None;
|
2020-07-08 16:12:49 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-01-24 13:52:57 +08:00
|
|
|
|
void ITableSortModel.SetSortDirection(SortDirection sortDirection)
|
2020-07-08 16:12:49 +08:00
|
|
|
|
{
|
2021-01-24 13:52:57 +08:00
|
|
|
|
_sortDirection = sortDirection;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
IOrderedQueryable<TItem> ITableSortModel.SortList<TItem>(IQueryable<TItem> source)
|
|
|
|
|
{
|
|
|
|
|
if (_sortDirection == SortDirection.None)
|
2020-07-08 16:12:49 +08:00
|
|
|
|
{
|
|
|
|
|
return source as IOrderedQueryable<TItem>;
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-02 23:54:56 +08:00
|
|
|
|
var lambda = (Expression<Func<TItem, TField>>)_getFieldExpression;
|
2020-07-08 16:12:49 +08:00
|
|
|
|
|
2021-01-24 13:52:57 +08:00
|
|
|
|
if (_sortDirection == SortDirection.Ascending)
|
2020-09-01 17:23:31 +08:00
|
|
|
|
{
|
2021-01-12 00:39:40 +08:00
|
|
|
|
return _comparer == null ? source.OrderBy(lambda) : source.OrderBy(lambda, this);
|
2020-09-01 17:23:31 +08:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2021-01-12 00:39:40 +08:00
|
|
|
|
return _comparer == null ? source.OrderByDescending(lambda) : source.OrderByDescending(lambda, this);
|
2020-09-01 17:23:31 +08:00
|
|
|
|
}
|
2020-07-08 16:12:49 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-01-12 00:39:40 +08:00
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
public int Compare(TField x, TField y)
|
|
|
|
|
{
|
|
|
|
|
return _comparer?.Invoke(x, y) ?? 0;
|
|
|
|
|
}
|
2020-07-08 16:12:49 +08:00
|
|
|
|
}
|
|
|
|
|
}
|