|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the Apache 2.0 License |
| 3 | +// See the LICENSE file in the project root for more information. |
| 4 | +// Maintainer: Argo Zhang([email protected]) Website: https://www.blazor.zone |
| 5 | + |
| 6 | +namespace BootstrapBlazor.Components; |
| 7 | + |
| 8 | +/// <summary> |
| 9 | +/// 多项选择下拉框过滤组件 |
| 10 | +/// </summary> |
| 11 | +public partial class MultiSelectFilter<TType> |
| 12 | +{ |
| 13 | + private string? FilterRowClassString => CssBuilder.Default("filter-row") |
| 14 | + .AddClass("active", TableColumnFilter.HasFilter()) |
| 15 | + .Build(); |
| 16 | + |
| 17 | + private TType? _value1; |
| 18 | + private FilterAction _action1 = FilterAction.Equal; |
| 19 | + |
| 20 | + /// <summary> |
| 21 | + /// Gets or sets the filter items. |
| 22 | + /// </summary> |
| 23 | + [Parameter] |
| 24 | + public List<SelectedItem>? Items { get; set; } |
| 25 | + |
| 26 | + /// <summary> |
| 27 | + /// <inheritdoc/> |
| 28 | + /// </summary> |
| 29 | + public override void Reset() |
| 30 | + { |
| 31 | + _value1 = default; |
| 32 | + _action1 = FilterAction.GreaterThanOrEqual; |
| 33 | + Count = 0; |
| 34 | + Logic = FilterLogic.And; |
| 35 | + StateHasChanged(); |
| 36 | + } |
| 37 | + |
| 38 | + /// <summary> |
| 39 | + /// <inheritdoc/> |
| 40 | + /// </summary> |
| 41 | + /// <returns></returns> |
| 42 | + public override FilterKeyValueAction GetFilterConditions() |
| 43 | + { |
| 44 | + var filter = new FilterKeyValueAction() { FilterLogic = FilterLogic.Or }; |
| 45 | + if (_value1 is string v) |
| 46 | + { |
| 47 | + var items = v.Split(',', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries); |
| 48 | + foreach (var item in items) |
| 49 | + { |
| 50 | + filter.Filters.Add(new FilterKeyValueAction |
| 51 | + { |
| 52 | + FieldKey = FieldKey, |
| 53 | + FieldValue = item, |
| 54 | + FilterAction = _action1 |
| 55 | + }); |
| 56 | + } |
| 57 | + } |
| 58 | + else if (_value1 is IEnumerable<string> values) |
| 59 | + { |
| 60 | + foreach (var item in values) |
| 61 | + { |
| 62 | + filter.Filters.Add(new FilterKeyValueAction |
| 63 | + { |
| 64 | + FieldKey = FieldKey, |
| 65 | + FieldValue = item, |
| 66 | + FilterAction = _action1 |
| 67 | + }); |
| 68 | + } |
| 69 | + } |
| 70 | + return filter; |
| 71 | + } |
| 72 | + |
| 73 | + /// <summary> |
| 74 | + /// <inheritdoc/> |
| 75 | + /// </summary> |
| 76 | + public override async Task SetFilterConditionsAsync(FilterKeyValueAction filter) |
| 77 | + { |
| 78 | + var first = filter.Filters.FirstOrDefault() ?? filter; |
| 79 | + if (first.FieldValue is TType value) |
| 80 | + { |
| 81 | + _value1 = value; |
| 82 | + } |
| 83 | + else |
| 84 | + { |
| 85 | + _value1 = default; |
| 86 | + } |
| 87 | + _action1 = first.FilterAction; |
| 88 | + |
| 89 | + await base.SetFilterConditionsAsync(filter); |
| 90 | + } |
| 91 | +} |
0 commit comments