0

jqGrid 是空的。jqGrid 寻呼机显示“没有要查看的记录”。服务器正在返回看起来是格式正确的 JSON 字符串。我定义了一个 JSONReader 来匹配我的 JSON 字符串格式,但我仍然得到一个空网格。任何帮助,将不胜感激。

这是来自服务器的 JSON 字符串:

{"JSONObj":{"totalpages":"1","currpage":"1","totalrecords":"1","rows":[{"id":"1","cell":["num1","Vendor1"]}]}}

这是来源:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-    strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>JSON Example</title>

    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

    <script type="text/javascript" src="jquery.min.js"></script>
    <script type="text/javascript" src="grid.locale-en.js"></script>
    <script type="text/javascript" src="jquery.jqGrid.min.js"></script>
    <link rel="stylesheet" type="text/css" href="jquery-ui-1.9.0.custom.css" />
    <link rel="stylesheet" type="text/css" href="ui.jqgrid.css" />

    <script type="text/javascript">

      jQuery(document).ready(function () {
         jQuery("#vendorGrid2").jqGrid({
             jsonReader : {
                 root:"rows",
                 total: "totalpages",
                 page: "currpage",
                 records: "totalrecords",
                   cell: "cell", 
                   id: "id"
                },
           pager: $("#vendorGrid2_pager"),
           rowNum:1,
           rowList:[10,20,30],
           datatype: "json",
           viewrecords:true,
           url: "getJSONVendorList",  // call the struts2 action in jsonexample.xml
           gridModel:"JSONObj",       // the object that gets returned containing the grid data
           height: 250,
           colNames:['ID', 'Name'],
           colModel:[
               {name:'num',index:'num', width:200, sorttype:"int"},
               {name:'name',index:'name', width:500, sorttype:"string"}
           ],
           multiselect: false,
           height: "100%",
           caption: "Vendor List",
              //loadComplete: function(data){alert('loaded');},
              loadError: function(xhr,status,error){alert(status+" "+error);}, 
       });

         // this works when datatype:"local"
         //jQuery("#vendorGrid2").addRowData("1", {num:"1", name:"Dallas Vendor"});
         //jQuery("#vendorGrid2").addRowData("2", {num:"2", name:"Ft. Worth Vendor"});
     });

    </script>

</head>
<body>

JSON Example

<div id="gridInfo">
    <table id="vendorGrid2"></table>
    <div id="vendorGrid2_pager"></div>
</div>

</body>

</html>
4

1 回答 1

0

为了其他可能有同样问题的人的利益,我将回答我自己的问题:

Java 类正在使用 JSON 库 (jar) 构建一个带有名为 JSONObj 的变量的 JSON 对象。这样做会在 JSON 字符串中创建一个 JSON 字符串,因此在 Object 开头的“额外”字符串“JSONObj”。这不是要走的路。
正确的方法是使用 struts2 JSON 插件(struts2-json-plugin-2.1.8.jar)。该插件采用整个 java 类并将其转换为 JSON 对象。必须使用类属性和公共 getter 和 setter(POJO/java bean)才能正确实现类。无需在类中“构建” JSON 对象。这更像是一个 java/struts2/JSON 概念,而不是一个特定的代码问题,但我还是发布了 java 类:

package autobasic;

import java.util.ArrayList;
import java.util.List;
import org.apache.log4j.Logger;
import autobasic.beans.VendorBean;
import autobasic.db.DbVendorList;
import autobasic.exception.AutobasicException.AutobasicDBException;
import com.opensymphony.xwork2.ActionSupport;

public class VendorListActionSupport extends ActionSupport
{

   private static final long serialVersionUID = 1L;
   private static Logger log = Logger.getLogger(VendorListActionSupport.class);
   private String currpage = "1";
   private String totalpages = "1"; 
   private String totalrecords = "2"; 
   private ArrayList<ArrayList<String>> rows = new ArrayList<ArrayList<String>>();

   public String returnJSON()
   {
      getVendorList();
      return SUCCESS;
   }

   void getVendorList()
   {
      try
      {
         DbVendorList dbVendorList = new DbVendorList();
         List<VendorBean> vendorList = null;
         vendorList = dbVendorList.getVendorList();  // load the list
         totalrecords = Integer.toString(vendorList.size());

         ArrayList<String> items = null;

         for (VendorBean vb : vendorList)
         {
            items = new ArrayList<String>();
            items.add(vb.getID());
            items.add(vb.getName());
            items.add(vb.getCity());
            items.add(vb.getPhone());
            rows.add(items);
         }
      }
      catch (AutobasicDBException e)
      {
         e.printStackTrace();
      }
   }

   public String getCurrpage()
   {
      return currpage;
   }

   public String getTotalpages()
   {
      return totalpages;
   }
   public String getTotalrecords()
   {
      return totalrecords;
   }

   public ArrayList<ArrayList<String>> getRows()
   {
      return rows;
   }
}
于 2012-12-03T21:22:34.607 回答