Author: jleroux
Date: Thu May 21 13:09:01 2015 New Revision: 1680865 URL: http://svn.apache.org/r1680865 Log: A patch from Youssef Khaye updated by Wei Zhang for "Adding a new attribute ignore-when (verry similar to use-when) that enable rendering or not a field in list or multi form." https://issues.apache.org/jira/browse/OFBIZ-4481 Or on a form of type list or multi, it is not possible to completely ignore a field via use-when attribute. Instead it hides the title and the column header became shifted from the table content. For example I want to hide the description column from my form when exampleName is choosen as sort-field. This also modifies example/widget/example/ExampleForms.xml#ListExamples to illustrate this functionality Modified: ofbiz/trunk/framework/widget/dtd/widget-form.xsd ofbiz/trunk/framework/widget/src/org/ofbiz/widget/model/ModelFormField.java ofbiz/trunk/framework/widget/src/org/ofbiz/widget/model/ModelFormFieldBuilder.java ofbiz/trunk/framework/widget/src/org/ofbiz/widget/renderer/FormRenderer.java ofbiz/trunk/specialpurpose/example/widget/example/ExampleForms.xml Modified: ofbiz/trunk/framework/widget/dtd/widget-form.xsd URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/widget/dtd/widget-form.xsd?rev=1680865&r1=1680864&r2=1680865&view=diff ============================================================================== --- ofbiz/trunk/framework/widget/dtd/widget-form.xsd (original) +++ ofbiz/trunk/framework/widget/dtd/widget-form.xsd Thu May 21 13:09:01 2015 @@ -827,6 +827,15 @@ under the License. </xs:documentation> </xs:annotation> </xs:attribute> + <xs:attribute name="ignore-when" type="xs:string"> + <xs:annotation> + <xs:documentation> + This attribute is defined to enable ignoring a field on a form of type list or multi, + which is not possible using use-when attribute. Like use-when it should be written using java syntax. + The condition should be evaluated in the form context without looking for data coming from list that feeds the form. + </xs:documentation> + </xs:annotation> + </xs:attribute> <xs:attribute name="encode-output" default="true"> <xs:annotation> <xs:documentation> Modified: ofbiz/trunk/framework/widget/src/org/ofbiz/widget/model/ModelFormField.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/widget/src/org/ofbiz/widget/model/ModelFormField.java?rev=1680865&r1=1680864&r2=1680865&view=diff ============================================================================== --- ofbiz/trunk/framework/widget/src/org/ofbiz/widget/model/ModelFormField.java (original) +++ ofbiz/trunk/framework/widget/src/org/ofbiz/widget/model/ModelFormField.java Thu May 21 13:09:01 2015 @@ -135,6 +135,7 @@ public class ModelFormField { private final FlexibleStringExpander tooltip; private final String tooltipStyle; private final FlexibleStringExpander useWhen; + private final FlexibleStringExpander ignoreWhen; private final String widgetAreaStyle; private final String widgetStyle; private final String parentFormName; @@ -187,6 +188,7 @@ public class ModelFormField { this.tooltip = builder.getTooltip(); this.tooltipStyle = builder.getTooltipStyle(); this.useWhen = builder.getUseWhen(); + this.ignoreWhen = builder.getIgnoreWhen(); this.widgetAreaStyle = builder.getWidgetAreaStyle(); this.widgetStyle = builder.getWidgetStyle(); this.parentFormName = builder.getParentFormName(); @@ -644,6 +646,12 @@ public class ModelFormField { return ""; } + public String getIgnoreWhen(Map<String, Object> context) { + if (UtilValidate.isNotEmpty(this.ignoreWhen)) + return this.ignoreWhen.expandString(context); + return ""; + } + public String getWidgetAreaStyle() { if (UtilValidate.isNotEmpty(this.widgetAreaStyle)) return this.widgetAreaStyle; @@ -3348,6 +3356,36 @@ public class ModelFormField { } } + public boolean shouldIgnore(Map<String, Object> context) { + boolean shouldIgnore = true; + String ignoreWhen = this.getIgnoreWhen(context); + if (UtilValidate.isEmpty(ignoreWhen)) return false; + + try { + Interpreter bsh = (Interpreter) context.get("bshInterpreter"); + if (bsh == null) { + bsh = BshUtil.makeInterpreter(context); + context.put("bshInterpreter", bsh); + } + + Object retVal = bsh.eval(StringUtil.convertOperatorSubstitutions(ignoreWhen)); + + if (retVal instanceof Boolean) { + shouldIgnore =(Boolean) retVal; + } else { + throw new IllegalArgumentException("Return value from ignore-when condition eval was not a Boolean: " + (retVal != null ? retVal.getClass().getName() : "null") + " [" + retVal + "] on the field " + this.name + " of form " + this.modelForm.getName()); + } + + } catch (EvalError e) { + String errMsg = "Error evaluating BeanShell ignore-when condition [" + ignoreWhen + "] on the field " + this.name + " of form " + this.modelForm.getName() + ": " + e.toString(); + Debug.logError(e, errMsg, module); + throw new IllegalArgumentException(errMsg); + } + + return shouldIgnore; + + } + /** * Models the <submit> element. * Modified: ofbiz/trunk/framework/widget/src/org/ofbiz/widget/model/ModelFormFieldBuilder.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/widget/src/org/ofbiz/widget/model/ModelFormFieldBuilder.java?rev=1680865&r1=1680864&r2=1680865&view=diff ============================================================================== --- ofbiz/trunk/framework/widget/src/org/ofbiz/widget/model/ModelFormFieldBuilder.java (original) +++ ofbiz/trunk/framework/widget/src/org/ofbiz/widget/model/ModelFormFieldBuilder.java Thu May 21 13:09:01 2015 @@ -102,6 +102,7 @@ public class ModelFormFieldBuilder { private FlexibleStringExpander tooltip = FlexibleStringExpander.getInstance("");; private String tooltipStyle = ""; private FlexibleStringExpander useWhen = FlexibleStringExpander.getInstance("");; + private FlexibleStringExpander ignoreWhen = FlexibleStringExpander.getInstance(""); private String widgetAreaStyle = ""; private String widgetStyle = ""; private String parentFormName = ""; @@ -152,6 +153,7 @@ public class ModelFormFieldBuilder { this.tooltip = FlexibleStringExpander.getInstance(fieldElement.getAttribute("tooltip")); this.tooltipStyle = fieldElement.getAttribute("tooltip-style"); this.useWhen = FlexibleStringExpander.getInstance(fieldElement.getAttribute("use-when")); + this.ignoreWhen = FlexibleStringExpander.getInstance(fieldElement.getAttribute("ignore-when")); this.widgetAreaStyle = fieldElement.getAttribute("widget-area-style"); this.widgetStyle = fieldElement.getAttribute("widget-style"); this.parentFormName = fieldElement.getAttribute("form-name"); @@ -458,6 +460,10 @@ public class ModelFormFieldBuilder { return useWhen; } + public FlexibleStringExpander getIgnoreWhen() { + return ignoreWhen; + } + public String getWidgetAreaStyle() { return widgetAreaStyle; } @@ -740,6 +746,8 @@ public class ModelFormFieldBuilder { this.action = builder.getAction(); if (UtilValidate.isNotEmpty(builder.getUseWhen())) this.useWhen = builder.getUseWhen(); + if (UtilValidate.isNotEmpty(builder.getIgnoreWhen())) + this.ignoreWhen = builder.getIgnoreWhen(); if (builder.getFieldInfo() != null) this.setFieldInfo(builder.getFieldInfo()); if (UtilValidate.isNotEmpty(builder.getHeaderLink())) Modified: ofbiz/trunk/framework/widget/src/org/ofbiz/widget/renderer/FormRenderer.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/widget/src/org/ofbiz/widget/renderer/FormRenderer.java?rev=1680865&r1=1680864&r2=1680865&view=diff ============================================================================== --- ofbiz/trunk/framework/widget/src/org/ofbiz/widget/renderer/FormRenderer.java (original) +++ ofbiz/trunk/framework/widget/src/org/ofbiz/widget/renderer/FormRenderer.java Thu May 21 13:09:01 2015 @@ -340,6 +340,10 @@ public class FormRenderer { continue; } + if(modelFormField.shouldIgnore(context)) { + continue; + } + if (fieldInfo.getFieldType() != FieldInfo.DISPLAY && fieldInfo.getFieldType() != FieldInfo.DISPLAY_ENTITY && fieldInfo.getFieldType() != FieldInfo.HYPERLINK) { @@ -548,6 +552,11 @@ public class FormRenderer { while (innerDisplayHyperlinkFieldIter.hasNext()) { boolean cellOpen = false; ModelFormField modelFormField = innerDisplayHyperlinkFieldIter.next(); + + if(modelFormField.shouldIgnore(localContext)) { + continue; + } + // span columns only if this is the last column in the row (not just in this first list) if (fieldCount.get(modelFormField.getName()) < 2) { if ((innerDisplayHyperlinkFieldIter.hasNext() || numOfCells > innerDisplayHyperlinkFieldsBegin.size())) { Modified: ofbiz/trunk/specialpurpose/example/widget/example/ExampleForms.xml URL: http://svn.apache.org/viewvc/ofbiz/trunk/specialpurpose/example/widget/example/ExampleForms.xml?rev=1680865&r1=1680864&r2=1680865&view=diff ============================================================================== --- ofbiz/trunk/specialpurpose/example/widget/example/ExampleForms.xml (original) +++ ofbiz/trunk/specialpurpose/example/widget/example/ExampleForms.xml Thu May 21 13:09:01 2015 @@ -50,6 +50,7 @@ under the License. <field-map field-name="viewIndex" from-field="viewIndex"/> <field-map field-name="viewSize" from-field="viewSize"/> </service> + <set field="sortField" from-field="parameters.sortField"/> </actions> <alt-row-style use-when=""EXST_APPROVED".equals(statusId)" style="Validate"/> <alt-row-style use-when=""EXST_CANCELLED".equals(statusId)" style="Warn"/> @@ -62,6 +63,7 @@ under the License. <field name="exampleTypeId" title="${uiLabelMap.CommonType}"><display-entity entity-name="ExampleType"/></field> <field name="statusId" title="${uiLabelMap.CommonStatus}"><display-entity entity-name="StatusItem"/></field> <field name="description" title="${uiLabelMap.CommonDescription}" sort-field="true"><display/></field> + <field name="conditionalDesc" ignore-when=""exampleName".equals(sortField)" title="my desc"><display description="${description}"/></field> </form> <!-- Typically, this extended form wouldn't be necessary. The parent form (ListExamples) would |
Free forum by Nabble | Edit this page |