How to use ajax in ofbiz Forms?

非 Y 不嫁゛ 提交于 2019-12-10 10:47:44

问题


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>
    - create test.js:
    $(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>
    In myService returns listOptions will parse to json format.
  • 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>
    This is my way, thanks!


来源:https://stackoverflow.com/questions/25865147/how-to-use-ajax-in-ofbiz-forms

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!