diff --git a/DbFirst.BlazorWebApp/Components/BandGridBase.cs b/DbFirst.BlazorWebApp/Components/BandGridBase.cs index 3c37f4d..d8c4525 100644 --- a/DbFirst.BlazorWebApp/Components/BandGridBase.cs +++ b/DbFirst.BlazorWebApp/Components/BandGridBase.cs @@ -257,6 +257,7 @@ public abstract class BandGridBase : ComponentBase ctx => b => { _filterContexts[fieldName] = ctx; + SyncDateFilterFromContext(fieldName, ctx.FilterCriteria); int s = 0; b.OpenElement(s++, "div"); b.AddAttribute(s++, "class", "date-filter-menu p-2"); @@ -296,6 +297,39 @@ public abstract class BandGridBase : ComponentBase b.CloseElement(); }; + private void SyncDateFilterFromContext(string fieldName, CriteriaOperator? criteria) + { + if (criteria == null) + { + _filterFrom[fieldName] = null; + _filterTo[fieldName] = null; + return; + } + + DateTime? from = null; + DateTime? to = null; + + if (criteria is GroupOperator group) + { + foreach (var op in group.Operands.OfType()) + ParseDateOperand(op, ref from, ref to); + } + else if (criteria is BinaryOperator binary) + { + ParseDateOperand(binary, ref from, ref to); + } + + _filterFrom[fieldName] = from; + _filterTo[fieldName] = to; + } + + private static void ParseDateOperand(BinaryOperator op, ref DateTime? from, ref DateTime? to) + { + if (op.RightOperand is not OperandValue val || val.Value is not DateTime dt) return; + if (op.OperatorType == BinaryOperatorType.GreaterOrEqual) from = dt; + else if (op.OperatorType == BinaryOperatorType.Less) to = dt.AddDays(-1); + } + private void OnFilterFromChanged(string fieldName, DateTime? value) { _filterFrom[fieldName] = value;