ant-design-blazor/components/table/TableModels/SortModel.cs
Zonciu Liang 4a680e6b76 fix(module: table): Unify FieldName, add DisplayAttribute for DiplayName, fix Filter for DataIndex (#1372)
* fix(module: table): Unify FieldName value

* fix(module: table): Get column name from DisplayAttribute

* fix(module: table): Filter for DataIndex

* docs(module: table): Intuitive sorterCompare for Filter demo

* fix(module: table): fix header title doesn't display when Sortable is false

Co-authored-by: James Yeung <shunjiey@hotmail.com>
2021-04-17 16:27:52 +00:00

67 lines
2.0 KiB
C#

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using AntDesign.Internal;
namespace AntDesign.TableModels
{
public class SortModel<TField> : ITableSortModel, IComparer<TField>
{
public int Priority { get; }
public string FieldName { get; }
public string Sort => _sortDirection?.Name;
SortDirection ITableSortModel.SortDirection => _sortDirection;
private readonly Func<TField, TField, int> _comparer;
private SortDirection _sortDirection;
private LambdaExpression _getFieldExpression;
public SortModel(LambdaExpression getFieldExpression, string fieldName, int priority, SortDirection defaultSortOrder, Func<TField, TField, int> comparer)
{
this.Priority = priority;
this._getFieldExpression = getFieldExpression;
this.FieldName = fieldName;
this._comparer = comparer;
this._sortDirection = defaultSortOrder ?? SortDirection.None;
}
void ITableSortModel.SetSortDirection(SortDirection sortDirection)
{
_sortDirection = sortDirection;
}
IOrderedQueryable<TItem> ITableSortModel.SortList<TItem>(IQueryable<TItem> source)
{
if (_sortDirection == SortDirection.None)
{
return source as IOrderedQueryable<TItem>;
}
var lambda = (Expression<Func<TItem, TField>>)_getFieldExpression;
if (_sortDirection == SortDirection.Ascending)
{
return _comparer == null ? source.OrderBy(lambda) : source.OrderBy(lambda, this);
}
else
{
return _comparer == null ? source.OrderByDescending(lambda) : source.OrderByDescending(lambda, this);
}
}
/// <inheritdoc />
public int Compare(TField x, TField y)
{
return _comparer?.Invoke(x, y) ?? 0;
}
}
}