From 8affc03d428905c1055971d2db9fee04009f2215 Mon Sep 17 00:00:00 2001 From: = Date: Mon, 11 Sep 2023 13:53:35 +0200 Subject: [PATCH] fix remove hidden row --- .../Helpers/DataModel/DataModel.cs | 25 +++++++++++------ .../Internal/Expressions/LayoutEvaluator.cs | 27 ++++++++++++++++--- 2 files changed, 40 insertions(+), 12 deletions(-) diff --git a/src/Altinn.App.Core/Helpers/DataModel/DataModel.cs b/src/Altinn.App.Core/Helpers/DataModel/DataModel.cs index 38a351c5e..7cf4efff6 100644 --- a/src/Altinn.App.Core/Helpers/DataModel/DataModel.cs +++ b/src/Altinn.App.Core/Helpers/DataModel/DataModel.cs @@ -213,12 +213,6 @@ public void RemoveField(string key) var keys = keys_split[0..^1]; var (lastKey, lastGroupIndex) = ParseKeyPart(keys_split[^1]); - if (lastGroupIndex is not null) - { - // TODO: Consider implementing. Would be required for rowHidden on groups - throw new NotImplementedException($"Deleting elements in List is not implemented {key}"); - } - var containingObject = GetModelDataRecursive(keys, 0, _serviceModel, default); if (containingObject is null) { @@ -226,6 +220,7 @@ public void RemoveField(string key) return; } + if (containingObject is System.Collections.IEnumerable) { throw new NotImplementedException($"Tried to remove field {key}, ended in an enumerable"); @@ -238,9 +233,23 @@ public void RemoveField(string key) return; } - var nullValue = property.PropertyType.GetTypeInfo().IsValueType ? Activator.CreateInstance(property.PropertyType) : null; + if (lastGroupIndex is not null) + { + // Remove row from list + var propertyValue = property.GetValue(containingObject); + if (propertyValue is not System.Collections.IList listValue) + { + throw new ArgumentException($"Tried to remove row {key}, ended in a non-list ({propertyValue?.GetType().ToString()})"); + } - property.SetValue(containingObject, nullValue); + listValue.RemoveAt(lastGroupIndex.Value); + } + else + { + // Set property to null + var nullValue = property.PropertyType.GetTypeInfo().IsValueType ? Activator.CreateInstance(property.PropertyType) : null; + property.SetValue(containingObject, nullValue); + } } /// diff --git a/src/Altinn.App.Core/Internal/Expressions/LayoutEvaluator.cs b/src/Altinn.App.Core/Internal/Expressions/LayoutEvaluator.cs index 8dd76ef8b..eded0c5fb 100644 --- a/src/Altinn.App.Core/Internal/Expressions/LayoutEvaluator.cs +++ b/src/Altinn.App.Core/Internal/Expressions/LayoutEvaluator.cs @@ -33,7 +33,7 @@ private static void HiddenFieldsForRemovalRecurs(LayoutEvaluatorState state, Has // Hidden row for repeating group var hiddenRow = new Dictionary(); - if (context.Component is RepeatingGroupComponent && context.RowLength is not null) + if (context.Component is RepeatingGroupComponent repGroup && context.RowLength is not null && repGroup.HiddenRow is not null) { foreach (var index in Enumerable.Range(0, context.RowLength.Value)) { @@ -42,14 +42,33 @@ private static void HiddenFieldsForRemovalRecurs(LayoutEvaluatorState state, Has var rowContext = new ComponentContext(context.Component, rowIndices, null, childContexts); var rowHidden = ExpressionEvaluator.EvaluateBooleanExpression(state, rowContext, "hiddenRow", false); hiddenRow.Add(index, rowHidden); + + var indexedBinding = state.AddInidicies(repGroup.DataModelBindings["group"], rowContext); + if (rowHidden) + { + hiddenModelBindings.Add(indexedBinding); + } + else + { + nonHiddenModelBindings.Add(indexedBinding); + } } } foreach (var childContext in context.ChildContexts) { - var currentRow = childContext.RowIndices?.Last(); - var rowIsHidden = currentRow is not null && hiddenRow.GetValueOrDefault(currentRow.Value) == true; - HiddenFieldsForRemovalRecurs(state, hiddenModelBindings, nonHiddenModelBindings, childContext, hidden || rowIsHidden); + // Check if row is already hidden + if (context.Component is RepeatingGroupComponent) + { + var currentRow = childContext.RowIndices?.Last(); + var rowIsHidden = currentRow is not null && hiddenRow.GetValueOrDefault(currentRow.Value) == true; + if (rowIsHidden) + { + continue; + } + } + + HiddenFieldsForRemovalRecurs(state, hiddenModelBindings, nonHiddenModelBindings, childContext, hidden); } foreach (var (bindingName, binding) in context.Component.DataModelBindings)