diff --git a/src/BootstrapBlazor/Components/Filters/FilterKeyValueAction.cs b/src/BootstrapBlazor/Components/Filters/FilterKeyValueAction.cs index 6586227bc66..ccae1945a18 100644 --- a/src/BootstrapBlazor/Components/Filters/FilterKeyValueAction.cs +++ b/src/BootstrapBlazor/Components/Filters/FilterKeyValueAction.cs @@ -1,4 +1,4 @@ -// Licensed to the .NET Foundation under one or more agreements. +// Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the Apache 2.0 License // See the LICENSE file in the project root for more information. // Maintainer: Argo Zhang(argo@live.ca) Website: https://www.blazor.zone @@ -20,6 +20,7 @@ public class FilterKeyValueAction /// /// 获得/设置 Filter 项字段值 /// + [JsonConverter(typeof(ObjectWithTypeConverter))] public object? FieldValue { get; set; } /// diff --git a/src/BootstrapBlazor/Converter/JsonQueryPageOptionConverter.cs b/src/BootstrapBlazor/Converter/JsonQueryPageOptionConverter.cs index d4c9620a95e..9de0c633ee6 100644 --- a/src/BootstrapBlazor/Converter/JsonQueryPageOptionConverter.cs +++ b/src/BootstrapBlazor/Converter/JsonQueryPageOptionConverter.cs @@ -1,4 +1,4 @@ -// Licensed to the .NET Foundation under one or more agreements. +// Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the Apache 2.0 License // See the LICENSE file in the project root for more information. // Maintainer: Argo Zhang(argo@live.ca) Website: https://www.blazor.zone @@ -104,80 +104,14 @@ public class JsonQueryPageOptionsConverter : JsonConverter reader.Read(); ret.IsVirtualScroll = reader.GetBoolean(); } - else if (propertyName == "searches") + + else if (propertyName == "filterKeyValueAction") { reader.Read(); - if (reader.TokenType == JsonTokenType.StartArray) - { - while (reader.Read()) - { - if (reader.TokenType == JsonTokenType.EndArray) - { - break; - } - var val = JsonSerializer.Deserialize(ref reader, options); - if (val != null) - { - ret.Searches.Add(val); - } - } - } - } - else if (propertyName == "customerSearches") - { - reader.Read(); - if (reader.TokenType == JsonTokenType.StartArray) - { - while (reader.Read()) - { - if (reader.TokenType == JsonTokenType.EndArray) - { - break; - } - var val = JsonSerializer.Deserialize(ref reader, options); - if (val != null) - { - ret.CustomerSearches.Add(val); - } - } - } - } - else if (propertyName == "advanceSearches") - { - reader.Read(); - if (reader.TokenType == JsonTokenType.StartArray) - { - while (reader.Read()) - { - if (reader.TokenType == JsonTokenType.EndArray) - { - break; - } - var val = JsonSerializer.Deserialize(ref reader, options); - if (val != null) - { - ret.AdvanceSearches.Add(val); - } - } - } - } - else if (propertyName == "filters") - { - reader.Read(); - if (reader.TokenType == JsonTokenType.StartArray) + var val = JsonSerializer.Deserialize(ref reader, options); + if (val != null) { - while (reader.Read()) - { - if (reader.TokenType == JsonTokenType.EndArray) - { - break; - } - var val = JsonSerializer.Deserialize(ref reader, options); - if (val != null) - { - ret.Filters.Add(val); - } - } + ret.FilterKeyValueAction = val; } } else if (propertyName == "isFirstQuery") @@ -252,41 +186,12 @@ public override void Write(Utf8JsonWriter writer, QueryPageOptions value, JsonSe { writer.WriteBoolean("isVirtualScroll", value.IsVirtualScroll); } - if (value.Searches.Count != 0) - { - writer.WriteStartArray("searches"); - foreach (var filter in value.Searches) - { - writer.WriteRawValue(JsonSerializer.Serialize(filter, options)); - } - writer.WriteEndArray(); - } - if (value.CustomerSearches.Count != 0) - { - writer.WriteStartArray("customerSearches"); - foreach (var filter in value.CustomerSearches) - { - writer.WriteRawValue(JsonSerializer.Serialize(filter, options)); - } - writer.WriteEndArray(); - } - if (value.AdvanceSearches.Count != 0) - { - writer.WriteStartArray("advanceSearches"); - foreach (var filter in value.AdvanceSearches) - { - writer.WriteRawValue(JsonSerializer.Serialize(filter, options)); - } - writer.WriteEndArray(); - } - if (value.Filters.Count != 0) + + if (value.Searches.Count != 0||value.CustomerSearches.Count != 0||value.AdvanceSearches.Count != 0|| value.Filters.Count != 0) { - writer.WriteStartArray("filters"); - foreach (var filter in value.Filters) - { - writer.WriteRawValue(JsonSerializer.Serialize(filter, options)); - } - writer.WriteEndArray(); + writer.WritePropertyName("filterKeyValueAction"); + var filterKeyValueAction = value.ToFilter(); + writer.WriteRawValue(JsonSerializer.Serialize(filterKeyValueAction, options)); } if (value.IsFirstQuery) { diff --git a/src/BootstrapBlazor/Converter/ObjectWithTypeConverter.cs b/src/BootstrapBlazor/Converter/ObjectWithTypeConverter.cs new file mode 100644 index 00000000000..4833decaf0f --- /dev/null +++ b/src/BootstrapBlazor/Converter/ObjectWithTypeConverter.cs @@ -0,0 +1,34 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the Apache 2.0 License +// See the LICENSE file in the project root for more information. +// Maintainer: Argo Zhang(argo@live.ca) Website: https://www.blazor.zone + +using System.Text.Json; +using System.Text.Json.Serialization; + +namespace BootstrapBlazor.Components; + +internal class ObjectWithTypeConverter : JsonConverter +{ + public override object? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) + { + using var doc = JsonDocument.ParseValue(ref reader); + + if (!doc.RootElement.TryGetProperty("$type", out var typeProp)) + return doc.RootElement.Clone(); // 无类型信息 + + var type = Type.GetType(typeProp.GetString()!)!; + + var valueElement = doc.RootElement.GetProperty("value"); + return JsonSerializer.Deserialize(valueElement.GetRawText(), type, options); + } + + public override void Write(Utf8JsonWriter writer, object value, JsonSerializerOptions options) + { + writer.WriteStartObject(); + writer.WriteString("$type", value.GetType().AssemblyQualifiedName); + writer.WritePropertyName("value"); + JsonSerializer.Serialize(writer, value, value.GetType(), options); + writer.WriteEndObject(); + } +} diff --git a/src/BootstrapBlazor/Extensions/QueryPageOptionsExtensions.cs b/src/BootstrapBlazor/Extensions/QueryPageOptionsExtensions.cs index cf3c52ce585..3a83629ac8b 100644 --- a/src/BootstrapBlazor/Extensions/QueryPageOptionsExtensions.cs +++ b/src/BootstrapBlazor/Extensions/QueryPageOptionsExtensions.cs @@ -1,4 +1,4 @@ -// Licensed to the .NET Foundation under one or more agreements. +// Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the Apache 2.0 License // See the LICENSE file in the project root for more information. // Maintainer: Argo Zhang(argo@live.ca) Website: https://www.blazor.zone @@ -19,6 +19,9 @@ public static class QueryPageOptionsExtensions /// public static FilterKeyValueAction ToFilter(this QueryPageOptions option) { + // 后续再更改 + if (option.FilterKeyValueAction != null) return option.FilterKeyValueAction; + var filter = new FilterKeyValueAction(); // 处理模糊搜索 diff --git a/src/BootstrapBlazor/Options/QueryPageOptions.cs b/src/BootstrapBlazor/Options/QueryPageOptions.cs index 9e41df89652..1df052e63c8 100644 --- a/src/BootstrapBlazor/Options/QueryPageOptions.cs +++ b/src/BootstrapBlazor/Options/QueryPageOptions.cs @@ -1,4 +1,4 @@ -// Licensed to the .NET Foundation under one or more agreements. +// Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the Apache 2.0 License // See the LICENSE file in the project root for more information. // Maintainer: Argo Zhang(argo@live.ca) Website: https://www.blazor.zone @@ -41,6 +41,7 @@ public class QueryPageOptions /// /// 获得 搜索条件绑定模型 未设置 时为 泛型模型 /// + [JsonConverter(typeof(ObjectWithTypeConverter))] public object? SearchModel { get; set; } /// @@ -79,6 +80,7 @@ public class QueryPageOptions /// /// 获得 通过列集合中的 列与 拼装 IFilterAction 集合 /// + [JsonIgnore] public List Searches { get; } = new(20); /// @@ -91,6 +93,7 @@ public class QueryPageOptions /// /// 获得 中过滤条件 模板中的条件请使用 获得 /// + [JsonIgnore] public List CustomerSearches { get; } = new(20); /// @@ -103,13 +106,21 @@ public class QueryPageOptions /// /// 获得 中过滤条件 /// + [JsonIgnore] public List AdvanceSearches { get; } = new(20); /// /// 获得 过滤条件集合 等同于 值 /// + [JsonIgnore] public List Filters { get; } = new(20); + /// + /// Gets or sets the action to take when filtering key-value pairs during processing. + /// + [JsonIgnore] + internal FilterKeyValueAction? FilterKeyValueAction { get; set; } + /// /// 获得 是否为首次查询 默认 false ///