在Struts 2中没有获取jquery网格中的数据

时间:2013-01-20 14:53:47

标签: jquery json jqgrid struts2 struts2-json-plugin

Grid.jsp:

<html>
<head>
    <title>jqGrid Example Part 1</title>
    <link rel="stylesheet" href="jquery-ui-1.8.14.custom.css" type="text/css"/>
    <link rel="stylesheet" href="ui.jqgrid.css" type="text/css"/>
    <script src="jquery-1.6.2.min.js" type="text/javascript"></script>
    <script src="grid.locale-en.js" type="text/javascript"></script>
    <script src="jquery.jqGrid.min.js" type="text/javascript"></script>
    <script type="text/javascript">

        function getGrid() {
            alert("I was here");
            var jqDataUrl = 'getData.action';
            // Set up the jquery grid
            $("#jqTable").jqGrid({
                // Ajax related configurations
                url        : jqDataUrl,
                datatype   : "json",
                gridModel  : "gridModel",
                // Specify the column names
                colNames   : ["id", "name"],
                // Configure the columns
                colModel   : [
                    { name: "id", index: "id", width: 40, align: "left" },
                    { name: "name", index: "name", width: 100, align: "left" }
                ],
                // Grid total width and height
                width      : 550,
                height     : 200,
                // Paging
                toppager   : true,
                pager      : $("#jqTablePager"),
                rowNum     : 5,
                rowList    : [5, 10, 20],
                viewrecords: true, // Specify if "total number of records" is displayed
                // Default sorting
                sortname   : "Id",
                sortorder  : "asc",
                // Grid caption
                caption    : "A Basic jqGrid - Read Only"
            }).navGrid("#jqTablePager",
                    { refresh: true, add: false, edit: false, del: false },
                    {}, // settings for edit
                    {}, // settings for add
                    {}, // settings for delete
                    {sopt: ["cn"]} // Search options. Some options can be set on column level
            );

            alert("out");
        }
    </script>
</head>
<body onload="getGrid();">
<div>
    <p>hi</p>
    <table id="jqTable" class="scroll">
    </table>
    <div id="jqTablePager"></div>
</div>
</body>
</html>

我的Struts.xml就像这样

<action name="getData" class="net.viralpatel.contact.view.ContactAction" method="getData">
<result name="success" type="json" >gird.jsp</result>          
</action>

当我执行getData.action

时,我可以看到json结果
{
    "data"        : "success",
    "gridModel"   : [
        {"id": 1, "name": "akhilesh"},
        {"id": 0, "name": null}
    ],
    "page"        : 1,
    "records"     : 2,
    "rows"        : 2,
    "searchField" : null,
    "searchOper"  : null,
    "searchString": null,
    "sidx"        : null,
    "sord"        : null,
    "total"       : 2
}

我无法在Grid中获取值.gridModel正确映射到我的表..

1 个答案:

答案 0 :(得分:1)

而不是定义:

gridModel: "gridModel"

做的:

jsonReader : {
    repeatitems: false,
    root       : "gridModel"
},                // Specify the column names

所以它应该是:

$("#jqTable").jqGrid({
    // Ajax related configurations
    url        : "data.json",
    datatype   : "json",
    jsonReader: {
        repeatitems: false,
        root       : "gridModel"
    },                // Specify the column names
    colNames   : ["id", "name"],
    // Configure the columns
    colModel   : [
        { name: "id", index: "id", width: 40, align: "left" },
        { name: "name", index: "name", width: 100, align: "left" }
    ],
    // Grid total width and height
    width      : 550,
    height     : 200,
    // Paging
    toppager   : true,
    pager      : $("#jqTablePager"),
    rowNum     : 5,
    rowList    : [5, 10, 20],
    viewrecords: true, // Specify if "total number of records" is displayed
    // Default sorting
    sortname   : "Id",
    sortorder  : "asc",
    // Grid caption
    caption    : "A Basic jqGrid - Read Only"
}).navGrid("#jqTablePager",
        { refresh: true, add: false, edit: false, del: false },
        {}, // settings for edit
        {}, // settings for add
        {}, // settings for delete
        {sopt: ["cn"]} // Search options. Some options can be set on column level
);