【问题标题】:how to create Jqgrid using jsp如何使用jsp创建Jqgrid
【发布时间】:2010-10-25 13:10:27
【问题描述】:

我是 jquery 和 json 的新手。我知道 jsp 和 java 在我的项目工作中,我必须实现与http://www.trirand.com/blog/jqgrid/jqgrid.html# 中显示的相同的数据网格 在这个示例站点中,我需要一个使用 Json 对象的数据网格。 我在互联网上探索了所有内容,但找不到任何帮助。

请提供一个使用 jsp -servlet 使用 jqgrid json 的示例

【问题讨论】:

    标签: json jsp jqgrid


    【解决方案1】:

    我终于把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);
    

    【讨论】:

    • 仍有任何人无法获得成功,请让我知道我会帮助他/她谢谢 Dhrumil Shah
    • 嗨,Dhrumil Shah,您能告诉我表格结构吗?
    • @Rakesh 我找不到你。您想在网格中显示的 DB Table struct.Columns 需要什么,放在 cell.add(rs.getString(1)); cell.add(rs.getString(2)); cell.add(rs.getString(3));这段代码还放了网格列模型的名称。你会得到结果。我想你明白我的意思。如果您仍有问题,请详细询问。
    【解决方案2】:

    您真的探索过整个互联网吗?感人的。 jqGrid 演示页面上的所有演示都显示了必要的代码,甚至是服务器端代码。您只需将他们的 PHP 转换为 JSP/Servlet 代码。

    【讨论】:

    • 您好,谢谢您的回复。实际上,我在 jsp 中转换 PHP 代码时遇到问题,我知道其他部分。如果你帮助我转换它,它的 Hartley 很感激。德鲁米尔沙阿
    • 那么您应该在新问题中发布您遇到问题的相关代码。或使用更多信息编辑此问题。
    猜你喜欢
    • 1970-01-01
    • 2014-09-29
    • 2018-09-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-15
    • 1970-01-01
    相关资源
    最近更新 更多