diff --git a/framework/example/widget/example/ExampleAjaxScreens.xml b/framework/example/widget/example/ExampleAjaxScreens.xml index 7f63f325ff..d4d0408a6b 100644 --- a/framework/example/widget/example/ExampleAjaxScreens.xml +++ b/framework/example/widget/example/ExampleAjaxScreens.xml @@ -24,6 +24,7 @@ under the License.
+ diff --git a/framework/example/widget/example/ExampleForms.xml b/framework/example/widget/example/ExampleForms.xml index 8f14f008c7..1fbe1e6abc 100644 --- a/framework/example/widget/example/ExampleForms.xml +++ b/framework/example/widget/example/ExampleForms.xml @@ -125,6 +125,23 @@ under the License. have these settings. -->
+ + + + + + + + + + + + + + + + + diff --git a/framework/images/webapp/images/selectall.js b/framework/images/webapp/images/selectall.js index db17946afe..e7b72371dd 100644 --- a/framework/images/webapp/images/selectall.js +++ b/framework/images/webapp/images/selectall.js @@ -323,6 +323,29 @@ function ajaxAutoCompleter(areaCsvString) { } } +/** Enable auto-completion for dorp-down elements. + * @param areaId The id of the drop-down + * @param data Choices for Autocompleter.Local + * @param options +*/ + +function ajaxAutoCompleteDropDown(element, data, options) { + var key = []; + var description = []; + for (var i=0; i < data.length; i++) { + currData = data[i]; + key[i] = currData[0]; + description[i] = currData[1]; + } + var update = element + "_autoCompleterOptions"; + $(element).insert({after: '
'}); + new Autocompleter.Local($(element), update, description, {autoSelect: options.autoSelect, frequency: options.frequency, minChars: options.minChars, choices: options.choices, partialSearch: options.partialSearch, partialChars: options.partialChars, ignoreCase: options.ignoreCase, fullSearch: options.fullSearch, afterUpdateElement: setKeyAsParameter}); + + function setKeyAsParameter() { + $(element).value = key[description.indexOf($F(element))]; + } +} + /** Toggle area visibility on/off. * @param link The element calling this function * @param areaId The id of the HTML container to toggle diff --git a/framework/widget/dtd/widget-form.xsd b/framework/widget/dtd/widget-form.xsd index f80c92cec7..433fb09442 100644 --- a/framework/widget/dtd/widget-form.xsd +++ b/framework/widget/dtd/widget-form.xsd @@ -612,6 +612,7 @@ under the License. + @@ -623,15 +624,6 @@ under the License. - - - - - - - - - @@ -1008,6 +1000,49 @@ under the License. + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/framework/widget/src/org/ofbiz/widget/form/ModelFormField.java b/framework/widget/src/org/ofbiz/widget/form/ModelFormField.java index 7aa3fef12f..49ef45cdb0 100644 --- a/framework/widget/src/org/ofbiz/widget/form/ModelFormField.java +++ b/framework/widget/src/org/ofbiz/widget/form/ModelFormField.java @@ -2197,6 +2197,92 @@ public class ModelFormField { this.useWhen = new FlexibleStringExpander(string); } } + + public static class AutoComplete { + protected String autoSelect; + protected String frequency; + protected String minChars; + protected String choices; + protected String partialSearch; + protected String partialChars; + protected String ignoreCase; + protected String fullSearch; + + public AutoComplete(Element element) { + this.autoSelect = element.getAttribute("auto-select"); + this.frequency = element.getAttribute("frequency"); + this.minChars = element.getAttribute("min-chars"); + this.choices = element.getAttribute("choices"); + this.partialSearch = element.getAttribute("partial-search"); + this.partialChars = element.getAttribute("partial-chars"); + this.ignoreCase = element.getAttribute("ignore-case"); + this.fullSearch = element.getAttribute("full-search"); + } + + public String getAutoSelect() { + return this.autoSelect; + } + + public String getFrequency() { + return this.frequency; + } + + public String getMinChars() { + return this.minChars; + } + + public String getChoices() { + return this.choices; + } + + public String getPartialSearch() { + return this.partialSearch; + } + + public String getPartialChars() { + return this.partialChars; + } + + public String getIgnoreCase() { + return this.ignoreCase; + } + + public String getFullSearch() { + return this.fullSearch; + } + + public void setAutoSelect(String string) { + this.autoSelect = string; + } + + public void setFrequency(String string) { + this.frequency = string; + } + + public void setMinChars(String string) { + this.minChars = string; + } + + public void setChoices(String string) { + this.choices = string; + } + + public void setPartialSearch(String string) { + this.partialSearch = string; + } + + public void setPartialChars(String string) { + this.partialChars = string; + } + + public void setIgnoreCase(String string) { + this.ignoreCase = string; + } + + public void setFullSearch(String string) { + this.fullSearch = string; + } + } public static class TextField extends FieldInfo { protected int size = 25; @@ -2575,6 +2661,7 @@ public class ModelFormField { protected FlexibleStringExpander currentDescription; protected SubHyperlink subHyperlink; protected int otherFieldSize = 0; + protected AutoComplete autoComplete; protected DropDownField() { super(); @@ -2615,6 +2702,11 @@ public class ModelFormField { if (subHyperlinkElement != null) { this.subHyperlink = new SubHyperlink(subHyperlinkElement); } + + Element autoCompleteElement = UtilXml.firstChildElement(element, "auto-complete"); + if (autoCompleteElement != null) { + this.autoComplete = new AutoComplete(autoCompleteElement); + } } public void renderFieldString(Appendable writer, Map context, FormStringRenderer formStringRenderer) throws IOException { @@ -2663,6 +2755,14 @@ public class ModelFormField { this.subHyperlink = newSubHyperlink; } + public AutoComplete getAutoComplete() { + return this.autoComplete; + } + + public void setAutoComplete(AutoComplete newAutoComplete) { + this.autoComplete = newAutoComplete; + } + public int getOtherFieldSize() { return this.otherFieldSize; } diff --git a/framework/widget/src/org/ofbiz/widget/html/HtmlFormRenderer.java b/framework/widget/src/org/ofbiz/widget/html/HtmlFormRenderer.java index 6139bab49f..b6fc6e7d61 100644 --- a/framework/widget/src/org/ofbiz/widget/html/HtmlFormRenderer.java +++ b/framework/widget/src/org/ofbiz/widget/html/HtmlFormRenderer.java @@ -582,11 +582,18 @@ public class HtmlFormRenderer extends HtmlWidgetRenderer implements FormStringRe public void renderDropDownField(Appendable writer, Map context, DropDownField dropDownField) throws IOException { ModelFormField modelFormField = dropDownField.getModelFormField(); ModelForm modelForm = modelFormField.getModelForm(); + ModelFormField.AutoComplete autoComplete = dropDownField.getAutoComplete(); + boolean ajaxEnabled = autoComplete != null && this.javaScriptEnabled; + List allOptionValues = dropDownField.getAllOptionValues(context, modelForm.getDelegator(context)); String event = modelFormField.getEvent(); String action = modelFormField.getAction(context); - writer.append(" 0) { - //writer.append(" onchange=\"alert('ONCHANGE, process_choice:' + process_choice)\""); - //writer.append(" onchange='test_js()' "); - writer.append(" onchange=\"process_choice(this,document."); - writer.append(modelForm.getName()); - writer.append("."); - writer.append(otherFieldName); - writer.append(")\" "); - } - - - if (UtilValidate.isNotEmpty(event) && UtilValidate.isNotEmpty(action)) { - writer.append(" "); - writer.append(event); - writer.append("=\""); - writer.append(action); - writer.append('"'); - } - - writer.append(" size=\"" + dropDownField.getSize() + "\">"); - - String currentValue = modelFormField.getEntry(context); - List allOptionValues = dropDownField.getAllOptionValues(context, modelForm.getDelegator(context)); - - // if the current value should go first, stick it in - if (UtilValidate.isNotEmpty(currentValue) && "first-in-list".equals(dropDownField.getCurrent())) { - writer.append(""); - String explicitDescription = dropDownField.getCurrentDescription(context); - if (UtilValidate.isNotEmpty(explicitDescription)) { - writer.append(explicitDescription); - } else { - writer.append(ModelFormField.FieldInfoWithOptions.getDescriptionForOptionKey(currentValue, allOptionValues)); + if (ajaxEnabled) { + writer.append("/>"); + + appendWhitespace(writer); + writer.append(""); + } else { + if (dropDownField.isAllowMultiple()) { + writer.append(" multiple=\"multiple\""); } - writer.append(" value=\""); - writer.append(optionValue.getKey()); - writer.append("\">"); - writer.append(optionValue.getDescription()); - writer.append(""); - } - - writer.append(""); - - // Adapted from work by Yucca Korpela - // http://www.cs.tut.fi/~jkorpela/forms/combo.html - if (otherFieldSize > 0) { - - String fieldName = modelFormField.getParameterName(context); - Map dataMap = modelFormField.getMap(context); - if (dataMap == null) { - dataMap = context; - } - Object otherValueObj = dataMap.get(otherFieldName); - String otherValue = (otherValueObj == null) ? "" : otherValueObj.toString(); - writer.append(""); - writer.append("\n"); + int otherFieldSize = dropDownField.getOtherFieldSize(); + String otherFieldName = dropDownField.getParameterNameOther(context); + if (otherFieldSize > 0) { + //writer.append(" onchange=\"alert('ONCHANGE, process_choice:' + process_choice)\""); + //writer.append(" onchange='test_js()' "); + writer.append(" onchange=\"process_choice(this,document."); + writer.append(modelForm.getName()); + writer.append("."); + writer.append(otherFieldName); + writer.append(")\" "); + } + + + if (UtilValidate.isNotEmpty(event) && UtilValidate.isNotEmpty(action)) { + writer.append(" "); + writer.append(event); + writer.append("=\""); + writer.append(action); + writer.append('"'); + } + + writer.append(" size=\"" + dropDownField.getSize() + "\">"); + + String currentValue = modelFormField.getEntry(context); + + // if the current value should go first, stick it in + if (UtilValidate.isNotEmpty(currentValue) && "first-in-list".equals(dropDownField.getCurrent())) { + writer.append(""); + String explicitDescription = dropDownField.getCurrentDescription(context); + if (UtilValidate.isNotEmpty(explicitDescription)) { + writer.append(explicitDescription); + } else { + writer.append(ModelFormField.FieldInfoWithOptions.getDescriptionForOptionKey(currentValue, allOptionValues)); + } + writer.append(""); + + // add a "separator" option + writer.append(""); + } + + // if allow empty is true, add an empty option + if (dropDownField.isAllowEmpty()) { + writer.append(""); + } + + // list out all options according to the option list + Iterator optionValueIter = allOptionValues.iterator(); + while (optionValueIter.hasNext()) { + ModelFormField.OptionValue optionValue = (ModelFormField.OptionValue) optionValueIter.next(); + String noCurrentSelectedKey = dropDownField.getNoCurrentSelectedKey(context); + writer.append(""); + writer.append(optionValue.getDescription()); + writer.append(""); + } + + writer.append(""); + + + // Adapted from work by Yucca Korpela + // http://www.cs.tut.fi/~jkorpela/forms/combo.html + if (otherFieldSize > 0) { + + String fieldName = modelFormField.getParameterName(context); + Map dataMap = modelFormField.getMap(context); + if (dataMap == null) { + dataMap = context; + } + Object otherValueObj = dataMap.get(otherFieldName); + String otherValue = (otherValueObj == null) ? "" : otherValueObj.toString(); + + writer.append(""); + writer.append("\n"); + } } + this.makeHyperlinkString(writer, dropDownField.getSubHyperlink(), context); this.appendTooltip(writer, context, modelFormField);