使用ajax在显示标记中加载json数据

时间:2013-08-28 02:42:00

标签: jquery ajax json displaytag struts1

JQuery的

 jQuery.noConflict();
    jQuery(document).ready(function(){

        jQuery("#stallId").change(function(e){

            //prevent default action
            e.preventDefault();

            jQuery.ajax({

                url: "getProducts.html?method=Product&stallId="+document.getElementById("stallId").value,
                dataType: "json",
                success: function(json){
                    if(json.status == true){
                        var strHtml='';
                        strHtml=+"";                  
                        for(var i=0;i<json.promotionProductList.length;i++){

                        }

                    }                   

                },
                failure: function(){
                    alert( "FAILED" );
                }
            });

        });
    });

显示标记

 <display:table name="ProductList" id="product" class="table" export="false">
    <display:column escapeXml="true" titleKey="productForm.name">

    </display:column>
</display:table>

在动作类中

Map productMap = new HashMap();
productMap.put("id", "1");
productMap.put("name", "Coca Cola");                

List<Product> productList = new ArrayList<Product>();
productList.add(productMap);

jsonWriter.object()
    .key("status").value(true)                   
    .key("pList").value(productList)
    .endObject();

如何使用ajax在display标签中加载json数据?当我从下拉列表中选择一个档位时,它会将url发送到后端操作类并能够获取产品的地图列表,但我不知道如何使数据显示在display标签中。有人可以帮助我并告诉我如何加载数据?顺便说一句,我正在使用支柱1.

1 个答案:

答案 0 :(得分:1)

在使用firebug检查显示标签部件后,我发现显示标签将更改为普通的html表。所以在ajax:

success: function(json){
    if(json.status == true){
        var strHtml='';                
        for(var i=0;i<json.pList.length;i++){
        strHtml+='<tr><td>'"+json.pList[i].name+"'</td></tr>';  
        }
    jQuery("table#product tbody").html(strHtml);
    }                   

},

在jQuery(“table#product tbody”)中, “表”是指显示表标签, #product引用显示表ID。