2011-02-15 63 views
0

我有一個Struts 2操作返回JSON結果。如何從JSON結果中設置struts2jquery Grid列名的結果

{"columns":["coupon","CM","CM+1","CM+2","CM+3"],"couponList":[{"coupon":3.0,"CM":"88.2323","CM+1":"89.45","CM+2":"132.3128125","CM+3":"32.82"},{"coupon":3.5,"CM":"25","CM+1":"3125","CM+2":"333","CM+3":"5"}],"Caption":"30 Yr Fixed"} 

我可以用數據填充網格。如何使用JSON結果中返回的「列」,「標題」屬性來填充列名稱和標題?

我正在使用S2Jquery標籤庫。 @JSP

<%@taglib prefix="s" uri="/struts-tags"%> 
    <%@taglib prefix="sj" uri="/struts-jquery-tags"%> 
    <%@taglib prefix="sjg" uri="/struts-jquery-grid-tags"%> 
    <link rel="stylesheet" href="../css/jqgrid_gcpm.css"> 
    <link rel="stylesheet" href="../css/gcp.css"> 
<sj:head /> 

<s:url id="actionurl" action="testAction"> 
    <s:set var="caption" value="Caption" /> 
</s:url> 

<sjg:grid id="gridtable" caption="%{caption}" dataType="json" 
    href="%{actionurl}" pager="false" gridModel="couponList" 
    rowNum="-1" rownumbers="false" altRows="true" autowidth="true" 
    resizable="true" shrinkToFit="true"> 
    <sjg:gridColumn name="coupon" index="coupon" title="Coupon" 
     sortable="false" width="30" /> 
    <sjg:gridColumn name="CM" title="CM" sortable="false" 
     formatter="htmlFormatter" /> 
    <sjg:gridColumn name="CM+1" title="CM+1" 
     sortable="false" formatter="htmlFormatter" /> 
    <sjg:gridColumn name="CM+2" title="CM+2" 
     sortable="false" formatter="htmlFormatter" /> 
    <sjg:gridColumn name="CM+3" title="CM+3" 
     sortable="false" formatter="htmlFormatter" /> 
</sjg:grid> 

<script type="text/javascript"> 
    function htmlFormatter(cellValue, opts, rowObject) { 
     if (cellValue == null) { 
      return ''; 
     } 
     else { 
      return cellValue; 
     } 
    } 
</script> 
+0

您是使用S2 jquery標記庫還是編寫javascript?如果前者發佈JSP,如果稍後從問題中刪除任何提及的S2並顯示該腳本。 – Quaternion 2011-02-16 17:06:36

+0

我正在使用S2Jquery標籤庫。 – silpa 2011-02-16 17:15:13

回答

0

我有同樣的問題,我的解決辦法是:

var myfirstrow = {description:"1"}; 
$("#cashReceiptLines").jqGrid('addRowData',"1", myfirstrow); 

這是我的網

<sjg:grid 
id="cashReceiptLines" 
caption="Cash receipt lines" 
gridModel="cashReceiptLines" 
rowNum="15" 
width="860" 
dataType= "local" 
rownumbers="true"> 
<sjg:gridColumn name="description" index="description" title="Description"/> 
</sjg:grid> 

亞歷克斯·查孔維森特希門尼斯 [email protected]。 com