[ofbiz-framework] branch trunk updated: Improved: Form widget "checkbox" do not persist selected value with multiple options checked (OFBIZ-12154)

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

[ofbiz-framework] branch trunk updated: Improved: Form widget "checkbox" do not persist selected value with multiple options checked (OFBIZ-12154)

jleroux@apache.org
This is an automated email from the ASF dual-hosted git repository.

jleroux pushed a commit to branch trunk
in repository https://gitbox.apache.org/repos/asf/ofbiz-framework.git


The following commit(s) were added to refs/heads/trunk by this push:
     new 000473c  Improved: Form widget "checkbox" do not persist selected value with multiple options checked (OFBIZ-12154)
000473c is described below

commit 000473c7ed7eb5d2d6118ec6da39aba43f58be02
Author: Jacques Le Roux <[hidden email]>
AuthorDate: Wed Mar 3 11:24:53 2021 +0100

    Improved: Form widget "checkbox" do not persist selected value with multiple options checked (OFBIZ-12154)
   
    I tried to use the form-widgets checkbox on a custom find screen.
    But after searching values with option/s checked, no checkbox remains selected.
   
    I found similar kind of ticket OFBIZ-11933.
    The checkbox was replaced by usage of the asmselect jQuery plugin
   
    This fixes the bug at the framework level.
   
    jleroux: fixes checkstyle issues: missing spaces after (3) if
   
    Thanks: Nameet Jain
---
 .../ofbiz/widget/renderer/macro/MacroFormRenderer.java    | 15 ++++++++++++++-
 .../widget/renderer/macro/MacroFormRendererTest.java      |  2 +-
 .../common-theme/template/macro/HtmlFormMacroLibrary.ftl  |  2 +-
 3 files changed, 16 insertions(+), 3 deletions(-)

diff --git a/framework/widget/src/main/java/org/apache/ofbiz/widget/renderer/macro/MacroFormRenderer.java b/framework/widget/src/main/java/org/apache/ofbiz/widget/renderer/macro/MacroFormRenderer.java
index eca47ff..17c4997 100644
--- a/framework/widget/src/main/java/org/apache/ofbiz/widget/renderer/macro/MacroFormRenderer.java
+++ b/framework/widget/src/main/java/org/apache/ofbiz/widget/renderer/macro/MacroFormRenderer.java
@@ -861,6 +861,16 @@ public final class MacroFormRenderer implements FormStringRenderer {
             }
         }
         String tabindex = modelFormField.getTabindex();
+
+        List<String> currentValueList = null;
+        if (UtilValidate.isNotEmpty(currentValue)) {
+            if (currentValue.startsWith("[")) {
+                currentValueList = StringUtil.toList(currentValue);
+            } else {
+                currentValueList = UtilMisc.toList(currentValue);
+            }
+        }
+
         List<ModelFormField.OptionValue> allOptionValues = checkField.getAllOptionValues(context, WidgetWorker.getDelegator(context));
         items.append("[");
         for (ModelFormField.OptionValue optionValue : allOptionValues) {
@@ -870,6 +880,9 @@ public final class MacroFormRenderer implements FormStringRenderer {
             items.append("{'value':'");
             items.append(optionValue.getKey());
             items.append("', 'description':'" + encode(optionValue.getDescription(), modelFormField, context));
+            if (UtilValidate.isNotEmpty(currentValueList) && currentValueList.contains(optionValue.getKey())) {
+                items.append("', 'checked':'" + Boolean.TRUE);
+            }
             items.append("'}");
         }
         items.append("]");
@@ -886,7 +899,7 @@ public final class MacroFormRenderer implements FormStringRenderer {
         sr.append("\" conditionGroup=\"");
         sr.append(conditionGroup);
         sr.append("\" allChecked=");
-        sr.append((allChecked != null ? Boolean.toString(allChecked) : "\"\""));
+        sr.append((allChecked != null && currentValueList == null ? Boolean.toString(allChecked) : "\"\""));
         sr.append(" currentValue=\"");
         sr.append(currentValue);
         sr.append("\" name=\"");
diff --git a/framework/widget/src/test/java/org/apache/ofbiz/widget/renderer/macro/MacroFormRendererTest.java b/framework/widget/src/test/java/org/apache/ofbiz/widget/renderer/macro/MacroFormRendererTest.java
index 68afb74..b601d9e 100644
--- a/framework/widget/src/test/java/org/apache/ofbiz/widget/renderer/macro/MacroFormRendererTest.java
+++ b/framework/widget/src/test/java/org/apache/ofbiz/widget/renderer/macro/MacroFormRendererTest.java
@@ -300,7 +300,7 @@ public class MacroFormRendererTest {
         assertAndGetMacroString("renderCheckField", ImmutableMap.of(
                 "currentValue", "KEY2",
                 "items", ImmutableList.of("{'value':'KEY1', 'description':'DESC1'}",
-                        "{'value':'KEY2', 'description':'DESC2'}")));
+                        "{'value':'KEY2', 'description':'DESC2', 'checked':'true'}")));
     }
 
     @Test
diff --git a/themes/common-theme/template/macro/HtmlFormMacroLibrary.ftl b/themes/common-theme/template/macro/HtmlFormMacroLibrary.ftl
index 741871e..22a912e 100644
--- a/themes/common-theme/template/macro/HtmlFormMacroLibrary.ftl
+++ b/themes/common-theme/template/macro/HtmlFormMacroLibrary.ftl
@@ -197,7 +197,7 @@ under the License.
       <input type="checkbox"<#if (item_index == 0)> id="${id}"</#if><#rt/><#if tabindex?has_content> tabindex="${tabindex}"</#if><#rt/>
         <#if disabled?has_content && disabled> disabled="disabled"</#if><#rt/>
         <#if allChecked?has_content && allChecked> checked="checked" <#elseif allChecked?has_content && !allChecked>
-          <#elseif currentValue?has_content && currentValue==item.value> checked="checked"</#if>
+          <#elseif item.checked?has_content && item.checked?boolean> checked="checked"</#if>
           name="${name?default("")?html}" value="${item.value?default("")?html}"<#if event?has_content> ${event}="${action}"</#if>/><#rt/>
         ${item.description?default("")}
     </span>