mirror of
https://gitee.com/ant-design-blazor/ant-design-blazor.git
synced 2024-12-15 01:11:52 +08:00
4a680e6b76
* 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>
67 lines
2.0 KiB
C#
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;
|
|
}
|
|
}
|
|
}
|