Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-apps-script/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用JSON清空jqGrid_Javascript_Json_Jsp - Fatal编程技术网

Javascript 使用JSON清空jqGrid

Javascript 使用JSON清空jqGrid,javascript,json,jsp,Javascript,Json,Jsp,jqGrid是空的。jqGrid寻呼机显示“没有要查看的记录”。服务器返回的是格式正确的JSON字符串。我定义了一个JSONReader来匹配我的JSON字符串格式,但仍然得到一个空网格。任何帮助都将不胜感激 以下是来自服务器的JSON字符串: {"JSONObj":{"totalpages":"1","currpage":"1","totalrecords":"1","rows":[{"id":"1","cell":["num1","Vendor1"]}]}} 资料来源如下: <!DO

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>

JSON示例
jQuery(文档).ready(函数(){
jQuery(“#vendorGrid2”).jqGrid({
jsonReader:{
根:“行”,
总计:“总计页数”,
第页:“第页”,
记录:“totalrecords”,
细胞:“细胞”,
id:“id”
},
传呼机:$(“供应商传呼机”),
rowNum:1,
行列表:[10,20,30],
数据类型:“json”,
viewrecords:是的,
url:“getJSONVendorList”,//在jsonexample.xml中调用struts2操作
gridModel:“JSONObj”,//返回的包含网格数据的对象
身高:250,
colNames:['ID','Name'],
colModel:[
{名称:'num',索引:'num',宽度:200,排序类型:“int”},
{名称:'name',索引:'name',宽度:500,排序类型:“string”}
],
多选:错,
高度:“100%”,
标题:“供应商名单”,
//loadComplete:函数(数据){alert('loaded');},
loadError:函数(xhr,status,error){alert(status+“”+error);},
});
//当数据类型为“local”时,此选项起作用
//jQuery(“#vendorGrid2”).addRowData(“1”),{num:“1”,name:“Dallas Vendor”});
//jQuery(“#vendorGrid2”).addRowData(“2”,{num:“2”,name:“Ft.Worth Vendor”});
});
JSON示例

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

Java类正在使用JSON库(jar)构建一个JSON对象,其中包含一个名为JSONObj的变量。这样做会在JSON字符串中创建一个JSON字符串,因此在对象的开头会出现“额外”字符串“JSONObj”。这不是一条路。
正确的方法是使用struts2 JSON插件(struts2-JSON-plugin-2.1.8.jar)。该插件获取整个java类并将其转换为JSON对象。必须使用类属性和公共getter和setter(POJO/JavaBean)才能正确实现该类。不需要在类中“构建”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;
   }
}
封装autobase;
导入java.util.ArrayList;
导入java.util.List;
导入org.apache.log4j.Logger;
导入autobasic.beans.VendorBean;
导入autobase.db.DbVendorList;
导入autobasic.exception.AutobasicException.AutobasicDBException;
导入com.opensymphony.xwork2.ActionSupport;
公共类VendorListActionSupport扩展了ActionSupport
{
私有静态最终长serialVersionUID=1L;
私有静态记录器log=Logger.getLogger(VendorListActionSupport.class);
专用字符串currpage=“1”;
私有字符串totalpages=“1”;
私有字符串totalrecords=“2”;
私有ArrayList行=新ArrayList();
公共字符串returnJSON()
{
getVendorList();
回归成功;
}
void getVendorList()
{
尝试
{
DbVendorList DbVendorList=新的DbVendorList();
列表供应商列表=空;
vendorList=dbVendorList.getVendorList();//加载列表
totalrecords=Integer.toString(vendorList.size());
arraylistitems=null;
for(VendorBean vb:vendorList)
{
items=newarraylist();
items.add(vb.getID());
items.add(vb.getName());
items.add(vb.getCity());
items.add(vb.getPhone());
行。添加(项);
}
}
捕获(AutobasicDBException e)
{
e、 printStackTrace();
}
}
公共字符串getCurrpage()
{
返回页面;
}
公共字符串getTotalpages()
{
返回总页数;
}
公共字符串getTotalrecords()
{
返回所有记录;
}
公共ArrayList getRows()
{
返回行;
}
}