From bbc69f25afc226f428c376e472d70106b3e52784 Mon Sep 17 00:00:00 2001 From: James Yeung Date: Fri, 22 Jan 2021 00:45:15 +0800 Subject: [PATCH] feat(module: table): support multiple sorter (#1019) --- components/table/Column.razor.cs | 10 +++++++++- .../table/Internal/ColumnDataIndexHelper.cs | 14 +++++++++----- components/table/Table.razor.cs | 3 ++- .../Components/Table/demo/MultipleSorter.razor | 18 ++++++++++++++++++ .../Components/Table/demo/multiple-sorter.md | 15 +++++++++++++++ 5 files changed, 53 insertions(+), 7 deletions(-) create mode 100644 site/AntBlazor.Docs/Demos/Components/Table/demo/MultipleSorter.razor create mode 100644 site/AntBlazor.Docs/Demos/Components/Table/demo/multiple-sorter.md diff --git a/components/table/Column.razor.cs b/components/table/Column.razor.cs index 255ebe09..5f507504 100644 --- a/components/table/Column.razor.cs +++ b/components/table/Column.razor.cs @@ -39,6 +39,9 @@ namespace AntDesign [Parameter] public Func 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(_propertyReflector.Value.PropertyInfo, 1, Sort, SorterCompare); + SortModel = new SortModel(_propertyReflector.Value.PropertyInfo, SorterMultiple, Sort, SorterCompare); } } else diff --git a/components/table/Internal/ColumnDataIndexHelper.cs b/components/table/Internal/ColumnDataIndexHelper.cs index ec73a9a6..ae62bd01 100644 --- a/components/table/Internal/ColumnDataIndexHelper.cs +++ b/components/table/Internal/ColumnDataIndexHelper.cs @@ -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 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(dataIndex, propertySelector, 1, sort, sorterCompare); + sortModel = new DataIndexSortModel(dataIndex, propertySelector, sorterMultiple, sort, sorterCompare); } } @@ -69,30 +69,34 @@ namespace AntDesign.Internal internal readonly string Sort; + internal readonly int SorterMultiple; + internal readonly Func SorterCompare; internal static ColumnCacheKey Create(Column 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 sorterCompare) + internal ColumnCacheKey(Type itemType, Type propType, string dataIndex, bool sortable, string sort, int sorterMultiple, Func 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 sorterCompare) + internal void Deconstruct(out Type itemType, out Type propType, out string dataIndex, out bool sortable, out string sort, out int sorterMultiple, out Func sorterCompare) { itemType = ItemType; propType = PropType; dataIndex = DataIndex; sortable = Sortable; sort = Sort; + sorterMultiple = SorterMultiple; sorterCompare = SorterCompare; } } diff --git a/components/table/Table.razor.cs b/components/table/Table.razor.cs index 32dc10c4..b3ce549b 100644 --- a/components/table/Table.razor.cs +++ b/components/table/Table.razor.cs @@ -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); } diff --git a/site/AntBlazor.Docs/Demos/Components/Table/demo/MultipleSorter.razor b/site/AntBlazor.Docs/Demos/Components/Table/demo/MultipleSorter.razor new file mode 100644 index 00000000..3f839194 --- /dev/null +++ b/site/AntBlazor.Docs/Demos/Components/Table/demo/MultipleSorter.razor @@ -0,0 +1,18 @@ + + + + + +
+@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), + }; +} diff --git a/site/AntBlazor.Docs/Demos/Components/Table/demo/multiple-sorter.md b/site/AntBlazor.Docs/Demos/Components/Table/demo/multiple-sorter.md new file mode 100644 index 00000000..4a34423b --- /dev/null +++ b/site/AntBlazor.Docs/Demos/Components/Table/demo/multiple-sorter.md @@ -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. +