How to use ajax in ofbiz Forms?

2019-05-24 21:53发布

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条回答
Animai°情兽
2楼-- · 2019-05-24 22:08

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!

查看更多
登录 后发表回答