mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2025-11-24 08:46:48 +08:00
Adding a checkbox to StringFilterForm to control whether an empty value should cause the filter to be skipped
This commit is contained in:
@@ -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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user