Skip to content

Commit 7299c52

Browse files
committed
refactor: 增加 MultiSelectFilter 组件
1 parent a4f194f commit 7299c52

File tree

2 files changed

+107
-0
lines changed

2 files changed

+107
-0
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
@namespace BootstrapBlazor.Components
2+
@inherits FilterBase
3+
@typeparam TType
4+
5+
@if (IsHeaderRow)
6+
{
7+
<div class="@FilterRowClassString">
8+
<MultiSelect class="is-filter" @bind-Value="_value1" OnValueChanged="_ => OnFilterAsync()"
9+
ShowLabel="false" SkipValidate="true" IsPopover="true"></MultiSelect>
10+
<FilterButton Items="Items" @bind-Value="_action1" OnSelectedItemChanged="_ => OnFilterAsync()" OnClearFilter="OnClearFilter"></FilterButton>
11+
</div>
12+
}
13+
else
14+
{
15+
<MultiSelect Items="@Items" @bind-Value="@_value1" IsPopover="true" ShowLabel="false" SkipValidate="true"></MultiSelect>
16+
}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
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

Comments
 (0)