2020-05-30 01:46:41 +08:00
|
|
|
|
using System;
|
2021-03-01 23:47:16 +08:00
|
|
|
|
using System.Collections.Generic;
|
2021-01-24 13:52:57 +08:00
|
|
|
|
using System.Linq;
|
2021-04-02 23:54:56 +08:00
|
|
|
|
using System.ComponentModel;
|
2021-04-18 00:27:52 +08:00
|
|
|
|
using System.ComponentModel.DataAnnotations;
|
2020-05-30 01:46:41 +08:00
|
|
|
|
using System.Linq.Expressions;
|
2021-04-02 23:54:56 +08:00
|
|
|
|
using System.Reflection;
|
2021-01-20 23:35:57 +08:00
|
|
|
|
using AntDesign.Internal;
|
2020-07-08 16:12:49 +08:00
|
|
|
|
using AntDesign.TableModels;
|
2020-05-30 01:46:41 +08:00
|
|
|
|
using Microsoft.AspNetCore.Components;
|
2022-03-04 23:47:19 +08:00
|
|
|
|
using System.Text.Json;
|
|
|
|
|
using AntDesign.Core.Helpers;
|
2020-05-30 01:46:41 +08:00
|
|
|
|
|
|
|
|
|
namespace AntDesign
|
|
|
|
|
{
|
2020-06-19 23:06:33 +08:00
|
|
|
|
public partial class Column<TData> : ColumnBase, IFieldColumn
|
2020-05-30 01:46:41 +08:00
|
|
|
|
{
|
2021-09-22 22:07:56 +08:00
|
|
|
|
[CascadingParameter(Name = "AntDesign.Column.Blocked")]
|
|
|
|
|
public bool Blocked { get; set; }
|
|
|
|
|
|
2021-01-20 23:35:57 +08:00
|
|
|
|
[CascadingParameter(Name = "ItemType")]
|
|
|
|
|
public Type ItemType { get; set; }
|
|
|
|
|
|
2020-05-30 01:46:41 +08:00
|
|
|
|
[Parameter]
|
2020-06-05 16:06:23 +08:00
|
|
|
|
public EventCallback<TData> FieldChanged { get; set; }
|
2020-05-30 01:46:41 +08:00
|
|
|
|
|
|
|
|
|
[Parameter]
|
2020-06-05 16:06:23 +08:00
|
|
|
|
public Expression<Func<TData>> FieldExpression { get; set; }
|
2020-05-30 01:46:41 +08:00
|
|
|
|
|
2020-06-02 19:15:15 +08:00
|
|
|
|
[Parameter]
|
2021-04-02 23:54:56 +08:00
|
|
|
|
public TData Field
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return GetValue != null ? GetValue(RowData) : _field;
|
|
|
|
|
}
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
if (GetValue == null)
|
|
|
|
|
{
|
|
|
|
|
_field = value;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private TData _field;
|
2020-05-30 01:46:41 +08:00
|
|
|
|
|
2021-08-01 21:18:50 +08:00
|
|
|
|
public override string Title { get => base.Title ?? DisplayName ?? FieldName; set => base.Title = value; }
|
|
|
|
|
|
2021-01-20 23:35:57 +08:00
|
|
|
|
[Parameter]
|
|
|
|
|
public string DataIndex { get; set; }
|
|
|
|
|
|
2020-07-05 00:09:53 +08:00
|
|
|
|
[Parameter]
|
|
|
|
|
public string Format { get; set; }
|
|
|
|
|
|
2020-07-08 16:12:49 +08:00
|
|
|
|
[Parameter]
|
|
|
|
|
public bool Sortable { get; set; }
|
|
|
|
|
|
2021-01-12 00:39:40 +08:00
|
|
|
|
[Parameter]
|
|
|
|
|
public Func<TData, TData, int> SorterCompare { get; set; }
|
|
|
|
|
|
2021-01-22 00:45:15 +08:00
|
|
|
|
[Parameter]
|
|
|
|
|
public int SorterMultiple { get; set; }
|
|
|
|
|
|
2020-07-08 16:12:49 +08:00
|
|
|
|
[Parameter]
|
|
|
|
|
public bool ShowSorterTooltip { get; set; } = true;
|
|
|
|
|
|
2021-01-24 13:52:57 +08:00
|
|
|
|
[Parameter]
|
|
|
|
|
public SortDirection[] SortDirections { get; set; }
|
|
|
|
|
|
|
|
|
|
[Parameter]
|
|
|
|
|
public SortDirection DefaultSortOrder { get; set; }
|
|
|
|
|
|
2021-03-01 23:47:16 +08:00
|
|
|
|
[Parameter]
|
2021-09-02 12:34:30 +08:00
|
|
|
|
public Func<CellData, Dictionary<string, object>> OnCell { get; set; }
|
2021-03-01 23:47:16 +08:00
|
|
|
|
|
|
|
|
|
[Parameter]
|
|
|
|
|
public Func<Dictionary<string, object>> OnHeaderCell { get; set; }
|
|
|
|
|
|
2021-06-28 14:19:33 +08:00
|
|
|
|
private bool _filterable;
|
|
|
|
|
|
|
|
|
|
private bool _hasFilterableAttribute;
|
|
|
|
|
|
2021-04-11 01:11:19 +08:00
|
|
|
|
[Parameter]
|
2021-06-28 14:19:33 +08:00
|
|
|
|
public bool Filterable
|
|
|
|
|
{
|
|
|
|
|
get => _filterable;
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
_filterable = value;
|
|
|
|
|
_hasFilterableAttribute = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-04-11 01:11:19 +08:00
|
|
|
|
|
2021-06-08 16:51:30 +08:00
|
|
|
|
private IEnumerable<TableFilter> _filters;
|
|
|
|
|
|
2021-06-28 14:19:33 +08:00
|
|
|
|
private bool _hasFiltersAttribute;
|
|
|
|
|
|
2021-02-27 23:55:48 +08:00
|
|
|
|
[Parameter]
|
2021-06-08 16:51:30 +08:00
|
|
|
|
public IEnumerable<TableFilter<TData>> Filters
|
|
|
|
|
{
|
|
|
|
|
get => _filters as IEnumerable<TableFilter<TData>>;
|
2021-06-28 14:19:33 +08:00
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
_filters = value;
|
|
|
|
|
_hasFiltersAttribute = true;
|
|
|
|
|
}
|
2021-06-08 16:51:30 +08:00
|
|
|
|
}
|
2021-02-27 23:55:48 +08:00
|
|
|
|
|
|
|
|
|
[Parameter]
|
|
|
|
|
public bool FilterMultiple { get; set; } = true;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Function that determines if the row is displayed when filtered
|
|
|
|
|
/// <para>
|
|
|
|
|
/// Parameter 1: The value of the filter item
|
|
|
|
|
/// </para>
|
|
|
|
|
/// <para>
|
|
|
|
|
/// Parameter 2: The value of the column
|
|
|
|
|
/// </para>
|
|
|
|
|
/// </summary>
|
|
|
|
|
[Parameter]
|
|
|
|
|
public Expression<Func<TData, TData, bool>> OnFilter { get; set; }
|
|
|
|
|
|
2022-01-18 10:06:29 +08:00
|
|
|
|
[Parameter]
|
|
|
|
|
public virtual RenderFragment<CellData<TData>> CellRender { get; set; }
|
|
|
|
|
|
2021-04-11 01:11:19 +08:00
|
|
|
|
private TableFilterType _columnFilterType;
|
|
|
|
|
|
|
|
|
|
private Type _columnDataType;
|
|
|
|
|
|
2021-08-01 21:18:50 +08:00
|
|
|
|
public string DisplayName { get; private set; }
|
2020-05-30 01:46:41 +08:00
|
|
|
|
|
2021-04-02 23:54:56 +08:00
|
|
|
|
public string FieldName { get; private set; }
|
2020-05-30 01:46:41 +08:00
|
|
|
|
|
2020-07-08 16:12:49 +08:00
|
|
|
|
public ITableSortModel SortModel { get; private set; }
|
|
|
|
|
|
2021-02-27 23:55:48 +08:00
|
|
|
|
public ITableFilterModel FilterModel { get; private set; }
|
|
|
|
|
|
2021-01-24 13:52:57 +08:00
|
|
|
|
private SortDirection _sortDirection;
|
|
|
|
|
|
2022-08-20 13:54:47 +08:00
|
|
|
|
protected Func<RowData, TData> GetValue { get; set; }
|
2021-01-20 23:35:57 +08:00
|
|
|
|
|
2022-08-20 13:54:47 +08:00
|
|
|
|
protected LambdaExpression GetFieldExpression { get; set; }
|
2021-04-02 23:54:56 +08:00
|
|
|
|
|
2021-04-19 15:07:36 +08:00
|
|
|
|
void IFieldColumn.ClearSorter()
|
|
|
|
|
{
|
|
|
|
|
SetSorter(SortDirection.None);
|
|
|
|
|
if (FieldExpression == null)
|
|
|
|
|
{
|
|
|
|
|
StateHasChanged();
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-01-24 13:52:57 +08:00
|
|
|
|
|
2022-10-21 13:01:25 +08:00
|
|
|
|
private static readonly EventCallbackFactory _callbackFactory = new();
|
2021-02-27 23:55:48 +08:00
|
|
|
|
|
|
|
|
|
private bool _filterOpened;
|
2021-04-02 23:54:56 +08:00
|
|
|
|
|
2021-02-27 23:55:48 +08:00
|
|
|
|
private bool _hasFilterSelected;
|
2021-04-02 23:54:56 +08:00
|
|
|
|
|
2021-02-27 23:55:48 +08:00
|
|
|
|
private string[] _selectedFilterValues;
|
|
|
|
|
|
2020-05-30 01:46:41 +08:00
|
|
|
|
protected override void OnInitialized()
|
|
|
|
|
{
|
2020-06-19 23:06:33 +08:00
|
|
|
|
base.OnInitialized();
|
|
|
|
|
|
2021-01-24 13:52:57 +08:00
|
|
|
|
Sortable = Sortable || SorterMultiple != default || SorterCompare != default || DefaultSortOrder != default || SortDirections?.Any() == true;
|
2021-01-22 00:45:15 +08:00
|
|
|
|
|
2021-01-20 23:35:57 +08:00
|
|
|
|
if (IsHeader)
|
2020-05-30 01:46:41 +08:00
|
|
|
|
{
|
2021-01-20 23:35:57 +08:00
|
|
|
|
if (FieldExpression != null)
|
2020-07-08 16:12:49 +08:00
|
|
|
|
{
|
2021-05-08 13:03:07 +08:00
|
|
|
|
if (FieldExpression.Body is not MemberExpression memberExp)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentException("'Field' parameter must be child member");
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-02 23:54:56 +08:00
|
|
|
|
var paramExp = Expression.Parameter(ItemType);
|
2021-05-08 13:03:07 +08:00
|
|
|
|
var bodyExp = Expression.MakeMemberAccess(paramExp, memberExp.Member);
|
2021-04-02 23:54:56 +08:00
|
|
|
|
GetFieldExpression = Expression.Lambda(bodyExp, paramExp);
|
|
|
|
|
}
|
|
|
|
|
else if (DataIndex != null)
|
|
|
|
|
{
|
|
|
|
|
(_, GetFieldExpression) = ColumnDataIndexHelper<TData>.GetDataIndexConfig(this);
|
2020-07-08 16:12:49 +08:00
|
|
|
|
}
|
2021-01-24 13:52:57 +08:00
|
|
|
|
|
2021-04-18 00:27:52 +08:00
|
|
|
|
if (GetFieldExpression != null)
|
2021-01-20 23:35:57 +08:00
|
|
|
|
{
|
2021-04-18 00:27:52 +08:00
|
|
|
|
var member = ColumnExpressionHelper.GetReturnMemberInfo(GetFieldExpression);
|
2021-05-08 13:03:07 +08:00
|
|
|
|
DisplayName = member?.GetCustomAttribute<DisplayNameAttribute>(true)?.DisplayName
|
|
|
|
|
?? member?.GetCustomAttribute<DisplayAttribute>(true)?.GetName()
|
|
|
|
|
?? member?.Name;
|
|
|
|
|
FieldName = DataIndex ?? member?.Name;
|
2020-07-08 16:12:49 +08:00
|
|
|
|
}
|
2021-01-24 13:52:57 +08:00
|
|
|
|
|
2021-04-18 00:27:52 +08:00
|
|
|
|
if (Sortable && GetFieldExpression != null)
|
2021-01-20 23:35:57 +08:00
|
|
|
|
{
|
2022-03-04 23:47:19 +08:00
|
|
|
|
SortModel = new SortModel<TData>(this, GetFieldExpression, FieldName, SorterMultiple, DefaultSortOrder, SorterCompare);
|
2021-01-20 23:35:57 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if (IsBody)
|
|
|
|
|
{
|
|
|
|
|
SortModel = Context.HeaderColumns[ColIndex] is IFieldColumn fieldColumn ? fieldColumn.SortModel : null;
|
2021-01-24 13:52:57 +08:00
|
|
|
|
|
2021-04-24 13:07:27 +08:00
|
|
|
|
if (DataIndex != null)
|
|
|
|
|
{
|
|
|
|
|
(GetValue, _) = ColumnDataIndexHelper<TData>.GetDataIndexConfig(this);
|
|
|
|
|
}
|
2020-07-08 16:12:49 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-01-30 22:21:26 +08:00
|
|
|
|
SortDirections ??= Table.SortDirections;
|
|
|
|
|
|
2021-01-24 13:52:57 +08:00
|
|
|
|
Sortable = Sortable || SortModel != null;
|
|
|
|
|
_sortDirection = SortModel?.SortDirection ?? DefaultSortOrder ?? SortDirection.None;
|
2021-04-11 01:11:19 +08:00
|
|
|
|
|
2021-06-28 14:19:33 +08:00
|
|
|
|
if (IsHeader)
|
2021-04-11 01:11:19 +08:00
|
|
|
|
{
|
2021-06-28 14:19:33 +08:00
|
|
|
|
if (_hasFiltersAttribute)
|
2021-04-16 09:18:57 +08:00
|
|
|
|
{
|
2021-06-28 14:19:33 +08:00
|
|
|
|
if (!_hasFilterableAttribute) Filterable = true;
|
2021-04-16 09:18:57 +08:00
|
|
|
|
_columnFilterType = TableFilterType.List;
|
|
|
|
|
}
|
2021-06-28 14:19:33 +08:00
|
|
|
|
else if (_hasFilterableAttribute)
|
2021-05-08 13:00:52 +08:00
|
|
|
|
{
|
2021-06-28 14:19:33 +08:00
|
|
|
|
_columnDataType = THelper.GetUnderlyingType<TData>();
|
|
|
|
|
if (_columnDataType == typeof(bool))
|
2021-05-08 13:00:52 +08:00
|
|
|
|
{
|
2021-06-28 14:19:33 +08:00
|
|
|
|
_columnFilterType = TableFilterType.List;
|
|
|
|
|
|
|
|
|
|
_filters = new List<TableFilter>();
|
|
|
|
|
|
|
|
|
|
var trueFilterOption = GetNewFilter();
|
|
|
|
|
trueFilterOption.Text = Table.Locale.FilterOptions.True;
|
|
|
|
|
trueFilterOption.Value = true;
|
|
|
|
|
((List<TableFilter>)_filters).Add(trueFilterOption);
|
|
|
|
|
var falseFilterOption = GetNewFilter();
|
|
|
|
|
falseFilterOption.Text = Table.Locale.FilterOptions.False;
|
|
|
|
|
falseFilterOption.Value = false;
|
|
|
|
|
((List<TableFilter>)_filters).Add(falseFilterOption);
|
|
|
|
|
}
|
2021-07-15 22:34:05 +08:00
|
|
|
|
else if (_columnDataType.IsEnum && _columnDataType.GetCustomAttribute<FlagsAttribute>() == null)
|
|
|
|
|
{
|
|
|
|
|
_columnFilterType = TableFilterType.List;
|
|
|
|
|
|
|
|
|
|
_filters = new List<TableFilter>();
|
|
|
|
|
|
|
|
|
|
foreach (var enumValue in Enum.GetValues(_columnDataType))
|
|
|
|
|
{
|
|
|
|
|
var enumName = Enum.GetName(_columnDataType, enumValue);
|
|
|
|
|
var filterOption = GetNewFilter();
|
|
|
|
|
// use DisplayAttribute only, DisplayNameAttribute is not valid for enum values
|
|
|
|
|
filterOption.Text = _columnDataType.GetMember(enumName)[0].GetCustomAttribute<DisplayAttribute>()?.Name ?? enumName;
|
|
|
|
|
filterOption.Value = enumValue;
|
|
|
|
|
((List<TableFilter>)_filters).Add(filterOption);
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-06-28 14:19:33 +08:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
_columnFilterType = TableFilterType.FieldType;
|
|
|
|
|
InitFilters();
|
2021-05-08 13:00:52 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-07-15 22:34:05 +08:00
|
|
|
|
if (_columnFilterType == TableFilterType.List && THelper.IsTypeNullable<TData>())
|
|
|
|
|
{
|
|
|
|
|
var nullFilterOption = GetNewFilter();
|
|
|
|
|
nullFilterOption.Text = Table.Locale.FilterOptions.IsNull;
|
|
|
|
|
nullFilterOption.Value = null;
|
|
|
|
|
((List<TableFilter>)_filters).Add(nullFilterOption);
|
|
|
|
|
}
|
2021-05-08 13:00:52 +08:00
|
|
|
|
}
|
2021-08-21 01:34:25 +08:00
|
|
|
|
|
|
|
|
|
Context.HeaderColumnInitialed(this);
|
2021-04-11 01:11:19 +08:00
|
|
|
|
}
|
2021-01-24 13:52:57 +08:00
|
|
|
|
|
2020-07-08 16:12:49 +08:00
|
|
|
|
ClassMapper
|
2021-04-02 23:54:56 +08:00
|
|
|
|
.If("ant-table-column-has-sorters", () => Sortable)
|
|
|
|
|
.If($"ant-table-column-sort", () => Sortable && SortModel != null && SortModel.SortDirection.IsIn(SortDirection.Ascending, SortDirection.Descending));
|
2020-07-08 16:12:49 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-06-28 14:19:33 +08:00
|
|
|
|
protected override void OnParametersSet()
|
|
|
|
|
{
|
|
|
|
|
base.OnParametersSet();
|
|
|
|
|
|
|
|
|
|
if (IsHeader)
|
|
|
|
|
{
|
|
|
|
|
FilterModel = _filterable && _filters?.Any(x => x.Selected) == true ?
|
2022-03-04 23:47:19 +08:00
|
|
|
|
new FilterModel<TData>(this, GetFieldExpression, FieldName, OnFilter, _filters.Where(x => x.Selected).ToList(), _columnFilterType) :
|
2021-06-28 14:19:33 +08:00
|
|
|
|
null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-22 22:07:56 +08:00
|
|
|
|
protected override bool ShouldRender()
|
|
|
|
|
{
|
|
|
|
|
if (Blocked) return false;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-03 18:54:41 +08:00
|
|
|
|
private string NumberFormatter(object value)
|
2021-04-11 01:11:19 +08:00
|
|
|
|
{
|
|
|
|
|
if (value == null) return null;
|
|
|
|
|
return Convert.ToDouble(value).ToString(Format);
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-01 23:47:16 +08:00
|
|
|
|
private void HandleSort()
|
2020-07-08 16:12:49 +08:00
|
|
|
|
{
|
|
|
|
|
if (Sortable)
|
|
|
|
|
{
|
2021-01-24 13:52:57 +08:00
|
|
|
|
SetSorter(NextSortDirection());
|
|
|
|
|
Table.ColumnSorterChange(this);
|
2020-07-08 16:12:49 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private string SorterTooltip
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2021-01-24 13:52:57 +08:00
|
|
|
|
var next = NextSortDirection();
|
|
|
|
|
return next?.Value switch
|
2020-07-08 16:12:49 +08:00
|
|
|
|
{
|
2021-01-24 13:52:57 +08:00
|
|
|
|
0 => Table.Locale.CancelSort,
|
|
|
|
|
1 => Table.Locale.TriggerAsc,
|
|
|
|
|
2 => Table.Locale.TriggerDesc,
|
|
|
|
|
_ => Table.Locale.CancelSort
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private SortDirection NextSortDirection()
|
|
|
|
|
{
|
|
|
|
|
if (_sortDirection == SortDirection.None)
|
|
|
|
|
{
|
|
|
|
|
return SortDirections[0];
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
var index = Array.IndexOf(SortDirections, _sortDirection);
|
|
|
|
|
if (index >= SortDirections.Length - 1)
|
2020-07-08 16:12:49 +08:00
|
|
|
|
{
|
2021-01-24 13:52:57 +08:00
|
|
|
|
return SortDirection.None;
|
2020-07-08 16:12:49 +08:00
|
|
|
|
}
|
2021-01-24 13:52:57 +08:00
|
|
|
|
|
|
|
|
|
return SortDirections[index + 1];
|
2020-05-30 01:46:41 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2020-11-09 22:17:27 +08:00
|
|
|
|
|
2021-01-24 13:52:57 +08:00
|
|
|
|
private void SetSorter(SortDirection sortDirection)
|
|
|
|
|
{
|
|
|
|
|
_sortDirection = sortDirection;
|
2021-04-27 16:37:19 +08:00
|
|
|
|
SortModel?.SetSortDirection(sortDirection);
|
2021-01-24 13:52:57 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-06-03 18:54:41 +08:00
|
|
|
|
private void SetFilterCompareOperator(TableFilter filter, TableFilterCompareOperator compareOperator)
|
2021-04-11 01:11:19 +08:00
|
|
|
|
{
|
|
|
|
|
filter.FilterCompareOperator = compareOperator;
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-03 18:54:41 +08:00
|
|
|
|
private void SetFilterCondition(TableFilter filter, TableFilterCondition filterCondition)
|
2021-04-11 01:11:19 +08:00
|
|
|
|
{
|
|
|
|
|
filter.FilterCondition = filterCondition;
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-03 18:54:41 +08:00
|
|
|
|
private void SetFilterValue(TableFilter filter, object value)
|
2021-04-11 01:11:19 +08:00
|
|
|
|
{
|
|
|
|
|
filter.Value = value;
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-03 18:54:41 +08:00
|
|
|
|
private void FilterSelected(TableFilter filter)
|
2021-02-27 23:55:48 +08:00
|
|
|
|
{
|
2021-05-01 03:40:05 +08:00
|
|
|
|
if (_columnFilterType == TableFilterType.FieldType) return;
|
2021-02-27 23:55:48 +08:00
|
|
|
|
if (!FilterMultiple)
|
|
|
|
|
{
|
2021-06-08 16:51:30 +08:00
|
|
|
|
_filters.ForEach(x => x.Selected = false);
|
2021-02-27 23:55:48 +08:00
|
|
|
|
filter.Selected = true;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
filter.Selected = !filter.Selected;
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-08 16:51:30 +08:00
|
|
|
|
_selectedFilterValues = _filters.Where(x => x.Selected).Select(x => x.Value?.ToString()).ToArray();
|
2021-02-27 23:55:48 +08:00
|
|
|
|
StateHasChanged();
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-11 01:11:19 +08:00
|
|
|
|
private void FilterConfirm(bool isReset = false)
|
2021-02-27 23:55:48 +08:00
|
|
|
|
{
|
|
|
|
|
_filterOpened = false;
|
2021-06-03 18:54:41 +08:00
|
|
|
|
if (!isReset && _columnFilterType == TableFilterType.FieldType)
|
|
|
|
|
{
|
2021-06-08 16:51:30 +08:00
|
|
|
|
_filters?.ForEach(f =>
|
2021-06-03 18:54:41 +08:00
|
|
|
|
{
|
|
|
|
|
f.Selected =
|
|
|
|
|
f.Value != null ||
|
|
|
|
|
f.FilterCompareOperator == TableFilterCompareOperator.IsNotNull ||
|
|
|
|
|
f.FilterCompareOperator == TableFilterCompareOperator.IsNull;
|
|
|
|
|
});
|
|
|
|
|
}
|
2021-06-08 16:51:30 +08:00
|
|
|
|
_hasFilterSelected = _filters?.Any(x => x.Selected) == true;
|
2022-03-04 23:47:19 +08:00
|
|
|
|
FilterModel = _hasFilterSelected ? new FilterModel<TData>(this, GetFieldExpression, FieldName, OnFilter, _filters.Where(x => x.Selected).ToList(), _columnFilterType) : null;
|
2021-02-27 23:55:48 +08:00
|
|
|
|
|
|
|
|
|
Table?.ReloadAndInvokeChange();
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-11 01:11:19 +08:00
|
|
|
|
private void ResetFilters()
|
|
|
|
|
{
|
|
|
|
|
if (_columnFilterType == TableFilterType.List)
|
|
|
|
|
{
|
2021-06-08 16:51:30 +08:00
|
|
|
|
_filters.ForEach(x => x.Selected = false);
|
2021-04-11 01:11:19 +08:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
InitFilters();
|
|
|
|
|
}
|
|
|
|
|
FilterConfirm(true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void AddFilter()
|
|
|
|
|
{
|
2021-06-08 16:51:30 +08:00
|
|
|
|
((List<TableFilter>)_filters).Add(GetNewFilter());
|
2021-04-11 01:11:19 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-06-03 18:54:41 +08:00
|
|
|
|
private void RemoveFilter(TableFilter filter)
|
2021-04-11 01:11:19 +08:00
|
|
|
|
{
|
2021-06-08 16:51:30 +08:00
|
|
|
|
((List<TableFilter>)_filters).Remove(filter);
|
2021-04-11 01:11:19 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-06-03 18:54:41 +08:00
|
|
|
|
private TableFilter GetNewFilter()
|
2021-04-11 01:11:19 +08:00
|
|
|
|
{
|
2021-08-08 17:59:45 +08:00
|
|
|
|
if (_columnFilterType == TableFilterType.FieldType)
|
2021-04-11 01:11:19 +08:00
|
|
|
|
{
|
2021-08-08 17:59:45 +08:00
|
|
|
|
return new TableFilter()
|
|
|
|
|
{
|
|
|
|
|
FilterCondition = TableFilterCondition.And,
|
|
|
|
|
FilterCompareOperator = _columnDataType == typeof(string) ? TableFilterCompareOperator.Contains : TableFilterCompareOperator.Equals
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return new TableFilter()
|
|
|
|
|
{
|
|
|
|
|
FilterCondition = TableFilterCondition.Or,
|
|
|
|
|
FilterCompareOperator = TableFilterCompareOperator.Equals
|
|
|
|
|
};
|
|
|
|
|
}
|
2021-04-11 01:11:19 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void InitFilters()
|
2021-02-27 23:55:48 +08:00
|
|
|
|
{
|
2021-06-08 16:51:30 +08:00
|
|
|
|
_filters = new List<TableFilter>() { GetNewFilter() };
|
2021-02-27 23:55:48 +08:00
|
|
|
|
}
|
2022-03-04 23:47:19 +08:00
|
|
|
|
|
|
|
|
|
void IFieldColumn.ClearFilters() => ResetFilters();
|
|
|
|
|
|
|
|
|
|
void IFieldColumn.SetFilterModel(ITableFilterModel filterModel)
|
|
|
|
|
{
|
|
|
|
|
foreach (var filter in filterModel.Filters)
|
|
|
|
|
{
|
2022-10-21 13:01:25 +08:00
|
|
|
|
if (filter.Value is JsonElement jsonElement)
|
2022-03-04 23:47:19 +08:00
|
|
|
|
{
|
2022-10-21 13:01:25 +08:00
|
|
|
|
filter.Value = JsonElementHelper<TData>.GetValue(jsonElement);
|
2022-03-04 23:47:19 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (_columnFilterType == TableFilterType.List)
|
|
|
|
|
{
|
|
|
|
|
foreach (var filter in filterModel.Filters.Where(filter => filterModel.Filters.Any(x => x.Value == filter.Value)))
|
|
|
|
|
{
|
|
|
|
|
filter.Selected = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
_filters = filterModel.Filters;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FilterModel = new FilterModel<TData>(this, GetFieldExpression, FieldName, OnFilter, _filters.Where(x => x.Selected).ToList(), _columnFilterType);
|
|
|
|
|
|
|
|
|
|
_hasFilterSelected = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void IFieldColumn.SetSortModel(ITableSortModel sortModel)
|
|
|
|
|
{
|
|
|
|
|
SortModel = new SortModel<TData>(this, GetFieldExpression, FieldName, SorterMultiple, SortDirection.Parse(sortModel.Sort), SorterCompare);
|
|
|
|
|
this.SetSorter(SortDirection.Parse(sortModel.Sort));
|
|
|
|
|
}
|
2020-05-30 01:46:41 +08:00
|
|
|
|
}
|
2021-04-09 00:49:20 +08:00
|
|
|
|
}
|