2010-10-25 25 views
6

Soy muy nuevo en jQuery y json.i saber JSP y Java en mi trabajo tengo proyecto para implementar una cuadrícula de datos mismas series como en http://www.trirand.com/blog/jqgrid/jqgrid.html# i este sitio ejemplo necesito una cuadrícula de datos utilizando Objeto Json. exploro todo en internet, pero no puedo encontrar ninguna ayuda de eso.cómo crear jqGrid utilizando JSP

por favor me proporcione un ejemplo con usos jqGrid JSON utilizando JSP -servlet

Respuesta

7

finalmente convertir el código PHP para código JSP de jqGrid:

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 

En este código, he creado dos archivos JSP.

En primer lugar un código está por debajo

<%@ 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); 

%> 

y el archivo de las meams JSP secound getsubdata.jsp es el siguiente:

<%@ 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); 

Y el secound JSP meams archivo getsubdata.jsp está por debajo

<%@ 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

cualquiera no puede conseguir el éxito favor hágamelo saber lo ayudaré/ sus Gracias Dhrumil Shah –

+0

Hola Dhrumil Shah puede usted por favor dígame el strucre mesa? –

+1

@Rakesh No puedo ayudarte. cuál es la necesidad de DB Table struct.Columns que desea mostrar en la cuadrícula en cell.add (rs.getString (1)); cell.add (rs.getString (2)); cell.add (rs.getString (3)); este código y también pone los nombres del modelo de columna de cuadrícula. obtendrás el resultado. creo que entiendes mi punto. Si aún tiene problemas, pregunte en detalles. –

0

Usted ha explorado realmente toda la Internet? Impresionante. Todas las demostraciones en la página de demostración de jqGrid muestran el código necesario, incluso el código del lado del servidor. Simplemente deberá convertir su PHP en código JSP/Servlet.

+0

Ya man Gracias por su respuesta. de hecho tengo un problema para convertir el código PHP en jsp else, parte que conozco. si me ayudas a convertir esto, Hartley lo apreciará. Dhrumil Shah –

+0

Luego debe publicar el código relevante con el que tiene problemas en una nueva pregunta. O edita esta pregunta con más información. Aún – Gregg

Cuestiones relacionadas