Adding a checkbox to StringFilterForm to control whether an empty value should cause the filter to be skipped

This commit is contained in:
Benedek Farkas
2024-04-10 18:11:40 +02:00
parent fb1aa73475
commit 175408e4c3

View File

@@ -36,8 +36,14 @@ namespace Orchard.Projections.FilterEditors.Forms {
Title: T("Value"),
Classes: new[] { "text medium", "tokenized" },
Description: T("Enter the value the string should be.")
)
);
),
_IgnoreIfEmptyValue: Shape.Checkbox(
Id: "IgnoreFilterIfValueIsEmpty",
Name: "IgnoreFilterIfValueIsEmpty",
Title: "Ignore filter if value is empty",
Description: T("When enabled, the filter will not be applied if the provided value is or evaluates to empty."),
Value: "true"
));
f._Operator.Add(new SelectListItem { Value = Convert.ToString(StringOperator.Equals), Text = T("Is equal to").Text });
f._Operator.Add(new SelectListItem { Value = Convert.ToString(StringOperator.NotEquals), Text = T("Is not equal to").Text });
@@ -65,6 +71,11 @@ namespace Orchard.Projections.FilterEditors.Forms {
var op = (StringOperator)Enum.Parse(typeof(StringOperator), Convert.ToString(formState.Operator));
object value = Convert.ToString(formState.Value);
if (bool.TryParse(formState.IgnoreFilterIfValueIsEmpty?.ToString() ?? "", out bool ignoreIfEmpty)
&& ignoreIfEmpty
&& string.IsNullOrWhiteSpace(value as string))
return (ex) => { };
switch (op) {
case StringOperator.Equals:
return x => x.Eq(property, value);
@@ -154,4 +165,4 @@ namespace Orchard.Projections.FilterEditors.Forms {
NotContains,
ContainsAnyIfProvided
}
}
}