问题
I'm using drop-down tag but I want to update another fields after selected an option in this. I'm using it in Forms. I know we can using ajax to submit and update but in ftl, I can't find which attribute of field or drop-down can help ?
Thanks!
回答1:
I found a way to resolve this problem. We will use js to post, send a request and get data with json format. Example:
- my Forms:
<field name="firstFieldId"> <drop-down allow-empty="false"> <list-options key-name="firstFieldId" list-name="listOptions1" description="${des1}"/> </drop-down> </field> <field name="secondFieldId"> <drop-down allow-empty="false"> <list-options key-name="secondFieldId" list-name="listOptions" description="${des2}"/> </drop-down> </field>
$(document).ready(function(){ $("select[name='firstFieldId']").change(function(){ update($(this).val()); }); }); function update(id) { jQuery.ajax({ url: 'myRequest', type: "POST", data:{ firstFieldId: id }, success: function(res) { var data = res.listOptions; renderHtml(data); } }); } function renderHtml(data){ var y = ""; for (var x in data){ y += "<option value='" + data[x].secondFieldId + "'>"; y += data[x].des2 + "</option>"; } $("select[name='secondFieldId']").html(y); }
- Screens:
<actions> <set field="layoutSettings.javaScripts[+0]" value="/delys/images/js/logistics.js" global="true"/> </actions>
Controller.xml :
<request-map uri="myRequest"> <security auth="true" https="true"/> <event type="service" invoke="myService"></event> <response name="success" type="request" value="json"></response> </request-map>
json request in common-controller.xml:
<request-map uri="json"> <security direct-request="false"/> <event type="java" path="org.ofbiz.common.CommonEvents" invoke="jsonResponseFromRequestAttributes"/> <response name="success" type="none"/> </request-map>
来源:https://stackoverflow.com/questions/25865147/how-to-use-ajax-in-ofbiz-forms