2010-10-25 46 views
6

创建的jqGrid我在jQuery的非常新,json.i知道在我的项目工作JSP和Java 我要实现一个数据网格,同样喜欢的节目在http://www.trirand.com/blog/jqgrid/jqgrid.html# 我这个例子网站我需要使用一个DataGrid Json对象。 我在网上探索所有,但我无法找到任何帮助。如何使用JSP

请提供给我使用JSP使用的jqGrid JSON的例子-servlet

回答

7

我终于转换的PHP代码的jqGrid的JSP代码:

jQuery(document).ready(function() 
    { 

    jQuery("#list10").jqGrid({ 

     url:'griddata.jsp', 
    colNames:['Inv No','Date', 'Client'], 
     colModel:[{name:'id',index:'id', width:55},{name:'invdate',index:'invdate', width:90},{name:'name',index:'name', width:100}], 
    rowNum:10, 
    rowList:[5,7,10], 
    pager: '#pager10', 
    sortname: 'id', 
     datatype: 'json', 
     viewrecords: true, 
     sortorder: "desc", 
    multiselect: false, 
    imgpath: "themes/basic/images", 
    caption: "Invoice Header", 
    onSelectRow: function(ids) { 

         jQuery("#list10_d").jqGrid().setGridParam({url : 'getsubdata.jsp?id='+ids}).trigger("reloadGrid") 
         jQuery("#list10_d").jqGrid('setCaption',"Invoice Detail: "+ids) 

         } 

     }); 

    jQuery("#list10").jqGrid('navGrid','#pager10',{add:false,edit:false,del:false}); 


<!-- subgrid start--> 

    jQuery("#list10_d").jqGrid({ 
           url:'getsubdata.jsp?id=0', 
           datatype: 'json', 
           colNames:['No','Item', 'Qty', 'Unit','Line Total'], 
           colModel:[ 
             {name:'num',index:'num' }, 
             {name:'item',index:'item'}, 
             {name:'qty',index:'qty',align:"center"}, 
             {name:'unit',index:'unit', align:"center"},  
             {name:'linetotal',index:'linetotal',align:"center", sortable:false, search:false} 
           ], 
           rowNum:5, 
           rowList:[5,10,20], 
           pager: '#pager10_d', 
           sortname: 'item', 
           viewrecords: true, 
           sortorder: "asc", 
           multiselect: true, 
           imgpath: "themes/basic/images", 
           caption:"Invoice Detail" 
         }).navGrid('#pager10_d',{add:false,edit:false,del:false}); 


    }//function 
    );//ready 

在这段代码中我已经创建了两个JSP文件。

第一个代码如下

<%@ page import="java.sql.*,java.util.ArrayList,net.sf.json.*" %> 
<% 
     String rows=request.getParameter("rows"); 

     String pageno=request.getParameter("page"); 
     String cpage=pageno; 

     Connection connect = null; 
     Statement statement = null; 
     PreparedStatement preparedStatement = null; 
     ResultSet rs= null; 
     Class.forName("com.mysql.jdbc.Driver").newInstance(); 
     connect = DriverManager.getConnection("jdbc:mysql://localhost/test?user=root&password=root"); 
     statement = connect.createStatement(); 
     rs = statement.executeQuery("SELECT * FROM grid "); 

     int count=0; 
     rs.last(); 
     count=rs.getRow(); 

     int pageval=0; 
     pageval=(count/Integer.parseInt(rows)); 

     int limitstart=0; 

     limitstart=(Integer.parseInt(rows)*Integer.parseInt(pageno))-Integer.parseInt(rows); 
     int total=count/Integer.parseInt(rows); 
     String totalrow=String.valueOf(total+1); 

     rs = statement.executeQuery("SELECT * FROM grid limit "+limitstart+","+rows); 


     JSONObject responcedata=new JSONObject(); 
     net.sf.json.JSONArray cellarray=new net.sf.json.JSONArray(); 

     responcedata.put("total",totalrow); 
     responcedata.put("page",cpage); 
     responcedata.put("records",count); 

     net.sf.json.JSONArray cell=new net.sf.json.JSONArray(); 
     net.sf.json.JSONObject cellobj=new net.sf.json.JSONObject(); 

     int i=1; 
     while(rs.next()) 
      { 
       cellobj.put("id",rs.getString(1)); 
       cell.add(rs.getString(1)); 
       cell.add(rs.getString(2)); 
       cell.add(rs.getString(3)); 

     cellobj.put("cell",cell); 
     cell.clear(); 
     cellarray.add(cellobj); 
     i++; 
     } 
     responcedata.put("rows",cellarray); 
     out.println(responcedata); 

%> 

而且谢胜利,JSP meams getsubdata.jsp文件如下:

<%@ page import="java.sql.*,java.util.ArrayList,net.sf.json.*" %> 
<% 
     String id=request.getParameter("id"); 
     String rows=request.getParameter("rows"); 
     String pageno=request.getParameter("page"); 
     String cpage=pageno; 


     JSONObject mysubdata=new JSONObject(); 
     JSONArray subarray = new JSONArray(); 
     Connection connect = null; 
    Statement statement = null; 

ResultSet rs= null; 
     Class.forName("com.mysql.jdbc.Driver").newInstance(); 
     connect = DriverManager.getConnection("jdbc:mysql://localhost/test?user=root&password=root"); 
     statement = connect.createStatement(); 

     rs = statement.executeQuery("SELECT * FROM subgrid where gridid='"+id+"'"); 

     int count=0; 
     rs.last(); 
     count=rs.getRow(); 

     int pageval=0; 
     pageval=(count/Integer.parseInt(rows)); 

     int limitstart=0; 

     limitstart=(Integer.parseInt(rows)*Integer.parseInt(pageno))-Integer.parseInt(rows); 
     int total=count/Integer.parseInt(rows); 
     String totalrow=String.valueOf(total+1); 

     rs = statement.executeQuery("SELECT * FROM subgrid where gridid='"+id+"' limit "+limitstart+","+rows); 

     JSONObject responcedata=new JSONObject(); 
     net.sf.json.JSONArray cellarray=new net.sf.json.JSONArray(); 

     responcedata.put("total",totalrow); 
     responcedata.put("page",cpage); 
     responcedata.put("records",count); 

     net.sf.json.JSONArray cell=new net.sf.json.JSONArray(); 
     net.sf.json.JSONObject cellobj=new net.sf.json.JSONObject(); 

     int ii=1; 
     while(rs.next()) 

     { 
       cellobj.put("id",""+ii); 

       //cell.add(rs.getString(1)); 
       cell.add(rs.getString(1)); 
       cell.add(rs.getString(2)); 
       cell.add(rs.getString(3)); 
       cell.add(rs.getString(4)); 
       cell.add(rs.getString(4)); 

     cellobj.put("cell",cell); 
     cell.clear(); 
     cellarray.add(cellobj); 
     ii++; 
     } 
     responcedata.put("rows",cellarray); 
     out.println(responcedata); 

而且谢胜利,JSP meams getsubdata.jsp文件低于

<%@ page import="java.sql.*,java.util.ArrayList,net.sf.json.*" %> 
<% 
     String id=request.getParameter("id"); 
     String rows=request.getParameter("rows"); 
     String pageno=request.getParameter("page"); 
     String cpage=pageno; 


     JSONObject mysubdata=new JSONObject(); 
     JSONArray subarray = new JSONArray(); 
     Connection connect = null; 
    Statement statement = null; 

ResultSet rs= null; 
     Class.forName("com.mysql.jdbc.Driver").newInstance(); 
     connect = DriverManager.getConnection("jdbc:mysql://localhost/test?user=root&password=root"); 
     statement = connect.createStatement(); 

     rs = statement.executeQuery("SELECT * FROM subgrid where gridid='"+id+"'"); 

     int count=0; 
     rs.last(); 
     count=rs.getRow(); 

     int pageval=0; 
     pageval=(count/Integer.parseInt(rows)); 

     int limitstart=0; 

     limitstart=(Integer.parseInt(rows)*Integer.parseInt(pageno))-Integer.parseInt(rows); 
     int total=count/Integer.parseInt(rows); 
     String totalrow=String.valueOf(total+1); 

     rs = statement.executeQuery("SELECT * FROM subgrid where gridid='"+id+"' limit "+limitstart+","+rows); 

     JSONObject responcedata=new JSONObject(); 
     net.sf.json.JSONArray cellarray=new net.sf.json.JSONArray(); 

     responcedata.put("total",totalrow); 
     responcedata.put("page",cpage); 
     responcedata.put("records",count); 

     net.sf.json.JSONArray cell=new net.sf.json.JSONArray(); 
     net.sf.json.JSONObject cellobj=new net.sf.json.JSONObject(); 

     int ii=1; 
     while(rs.next()) 

     { 
       cellobj.put("id",""+ii); 

       //cell.add(rs.getString(1)); 
       cell.add(rs.getString(1)); 
       cell.add(rs.getString(2)); 
       cell.add(rs.getString(3)); 
       cell.add(rs.getString(4)); 
       cell.add(rs.getString(4)); 

     cellobj.put("cell",cell); 
     cell.clear(); 
     cellarray.add(cellobj); 
     ii++; 
     } 
     responcedata.put("rows",cellarray); 
     out.println(responcedata); 
+0

仍然任何人不能得到成功,请让我知道我会帮助他/她 谢谢 Dhrumil沙赫 – 2010-11-02 11:25:29

+0

嗨Dhrumil沙阿,你能告诉我表格strucre? – 2011-06-24 06:40:50

+1

@Rakesh 我不能让你。你想在网格中显示的数据库表struct.Columns的需求是什么 cell.add(rs.getString(1)); cell.add(rs.getString(2)); cell.add(rs.getString(3)); 这段代码也放了网格列模型的名字。你会得到结果。 我想你明白我的观点。 如果您仍然有问题,请详细询问。 – 2011-06-30 02:31:48

0

你真的探讨了整个互联网?令人印象深刻。 jqGrid演示页面上的所有演示显示了所需的代码,甚至是服务器端代码。您只需将其PHP转换为JSP/Servlet代码即可。

+0

雅男人谢谢你的回复。实际上我有问题要转换PHP代码在JSP我不知道的其他部分。如果你帮我转换它,它的哈特利欣赏。 Dhrumil Shah – 2010-10-26 05:16:56

+0

然后,您应该在新问题中发布您遇到问题的相关代码。或者用更多信息编辑这个问题。 – Gregg 2010-10-26 14:06:58