如何从JSON结果中设置struts2 jquery Grid列名

时间:2011-02-15 22:46:00

标签: jquery jqgrid struts2

我有一个Struts 2操作返回JSON结果。

{"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结果中返回的“columns”,“Caption”属性填充列名称和标题?

我正在使用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>

1 个答案:

答案 0 :(得分: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>

AlexVicenteChacónJiménez alex.chacon@software-colombia.com