Tuesday, September 9, 2014

jqGrid:四、 remote data(JSON)

  1. 1.  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">  
    2.  <html>  
    3.      <head>  
    4.          <title>grid.html</title>  
    5.          <meta http-equiv="keywords" content="keyword1,keyword2,keyword3" />  
    6.          <meta http-equiv="description" content="this is my page" />  
    7.          <meta http-equiv="content-type" content="text/html; charset=UTF-8" />  
    8.    
    9.          <link rel="stylesheet" type="text/css" media="screen"  
    10.             href="css/themes/redmond/jquery-ui-1.8.2.custom.css" />  
    11.         <link rel="stylesheet" type="text/css" media="screen"  
    12.             href="css/themes/ui.jqgrid.css" />  
    13.         <link rel="stylesheet" type="text/css" media="screen"  
    14.             href="css/themes/ui.multiselect.css" />  
    15.         <link rel="stylesheet" type="text/css" media="screen"  
    16.             href="css/themes/jquery.searchFilter.css" />  
    17.         <style>  
    18. html,body {  
    19.     --margin: 0; /* Remove body margin/padding */  
    20.     padding: 0;  
    21.     overflow: hidden; /* Remove scroll bars on browser window */  
    22.     font-size: 75%;  
    23. }  
    24. </style>  
    25.   
    26.         <script src="js/jquery-1.4.2.min.js" type="text/javascript"></script>  
    27.         <script src="js/jquery-ui-1.8.2.custom.min.js" type="text/javascript"></script>  
    28.         <script src="js/src/ui.multiselect.js"  
    29.             type="text/javascript"></script>  
    30.         <script src="js/src/grid.loader.js" type="text/javascript"></script>  
    31.         <script type="text/javascript">  
    32.             $.jgrid.no_legacy_api = true;  
    33.             $.jgrid.useJSON = true;  
    34.         </script>  
    35.         <script type="text/javascript">  
    36.             $(function(){   
    37.               $("#grid_id").jqGrid({  
    38.                 url:'/demo2/servlet/JqGridJsonServlet',  
    39.                 mtype: 'GET',  
    40.                 datatype: 'json',  
    41.                 jsonReader : {  
    42.                    repeatitems: false  
    43.                 },  
    44.                 height: "auto",  
    45.                 loadui: "disable",  
    46.                 colNames:['Inv No','Date', 'Amount','Tax','Total','Notes'],  
    47.                 colModel :[   
    48.                   {name:'invId', index:'invId', width:70},   
    49.                   {name:'invDate', index:'invDate', width:120, editable:true},   
    50.                   {name:'amount', index:'amount', width:90, align:'right', editable:true},   
    51.                   {name:'tax', index:'tax', width:90, align:'right', editable:true},   
    52.                   {name:'total', index:'total', width:90, align:'right', editable:true},   
    53.                   {name:'note', index:'note', width:180, sortable:false, editable:true}   
    54.                 ],  
    55.                 pager: '#pager',  
    56.                 rowNum:10,  
    57.                 rowList:[10,20,30],  
    58.                 sortname: 'invid',  
    59.                 sortorder: 'asc',  
    60.                 viewrecords: true,  
    61.                 caption: 'My first grid'  
    62.               });  
    63.             });   
    64.         </script>  
    65.     </head>  
    66.     <body>  
    67.         <table id="grid_id"></table>  
    68.         <div id="pager"></div>  
    69.     </body>  
    70. </html>  

    servlet 
    Java  Description: 收藏代码
    1.  package com.qoma.servlet;  
    2.    
    3.  import java.io.IOException;  
    4.  import java.io.PrintWriter;  
    5.  import java.util.List;  
    6.    
    7.  import javax.servlet.ServletException;  
    8.  import javax.servlet.http.HttpServlet;  
    9.  import javax.servlet.http.HttpServletRequest;  
    10. import javax.servlet.http.HttpServletResponse;  
    11.   
    12. import org.apache.commons.lang.StringUtils;  
    13.   
    14. import com.et.ar.exception.ActiveRecordException;  
    15. import com.qoma.db.vo.InvHeader;  
    16. import com.qoma.service.InvHeaderService;  
    17. import com.qoma.util.Json;  
    18.   
    19. public class JqGridJsonServlet extends HttpServlet {  
    20.   
    21.     /** 
    22.      *  
    23.      */  
    24.     private static final long serialVersionUID = 1676458940650461673L;  
    25.   
    26.     private InvHeaderService service = new InvHeaderService();  
    27.   
    28.     /** 
    29.      * Constructor of the object. 
    30.      */  
    31.     public JqGridJsonServlet() {  
    32.         super();  
    33.     }  
    34.   
    35.     /** 
    36.      * Destruction of the servlet. <br> 
    37.      */  
    38.     public void destroy() {  
    39.         super.destroy(); // Just puts "destroy" string in log  
    40.         // Put your code here  
    41.     }  
    42.   
    43.     /** 
    44.      * The doGet method of the servlet. <br> 
    45.      *  
    46.      * This method is called when a form has its tag value method equals to get. 
    47.      *  
    48.      * @param request 
    49.      *            the request send by the client to the server 
    50.      * @param response 
    51.      *            the response send by the server to the client 
    52.      * @throws ServletException 
    53.      *             if an error occurred 
    54.      * @throws IOException 
    55.      *             if an error occurred 
    56.      */  
    57.     public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {  
    58.         this.doPost(request, response);  
    59.     }  
    60.   
    61.     /** 
    62.      * The doPost method of the servlet. <br> 
    63.      *  
    64.      * This method is called when a form has its tag value method equals to 
    65.      * post. 
    66.      *  
    67.      * @param request 
    68.      *            the request send by the client to the server 
    69.      * @param response 
    70.      *            the response send by the server to the client 
    71.      * @throws ServletException 
    72.      *             if an error occurred 
    73.      * @throws IOException 
    74.      *             if an error occurred 
    75.      */  
    76.     public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {  
    77.   
    78.         response.setContentType("text/html");  
    79.         PrintWriter out = response.getWriter();  
    80.   
    81.         String oper = request.getParameter("oper");  
    82.         String s = "";  
    83.         if (null == oper || "".equals(oper)) {  
    84.             Integer page = Integer.parseInt(request.getParameter("page"));  
    85.             Integer limit = Integer.parseInt(request.getParameter("rows"));  
    86.             String sidx = request.getParameter("sidx");  
    87.             String sord = request.getParameter("sord");  
    88.             if (null == sidx || "".equals(sidx))  
    89.                 sidx = "1";  
    90.   
    91.             Long count = 0L;  
    92.             try {  
    93.                 count = service.getCount();  
    94.             } catch (ActiveRecordException e) {  
    95.                 e.printStackTrace();  
    96.             }  
    97.             Integer totalPages = 0;  
    98.             if (count > 0 && limit > 0) {  
    99.                 totalPages = new Long(count / limit).intValue();  
    100.                         if (count % limit != 0) {  
    101.                             totalPages += 1;  
    102.                         }  
    103.                     } else {  
    104.                         totalPages = 0;  
    105.                     }  
    106.           
    107.                     // if for some reasons the requested page is greater than the total  
    108.                     // set the requested page to total page  
    109.                     if (page > totalPages)  
    110.                         page = totalPages;  
    111.           
    112.                     // calculate the starting position of the rows  
    113.                     Integer start = limit * page - limit;  
    114.           
    115.                     if (start < 0)  
    116.                         start = 0;  
    117.           
    118.                     try {  
    119.                         List<InvHeader> list = service.getLimitList(start, limit, sidx, sord);  
    120.                         s = service.getAllJson(page, totalPages, count, list);  
    121.                     } catch (ActiveRecordException e) {  
    122.                         e.printStackTrace();  
    123.                         s = Json.FAILURE;  
    124.                     }  
    125.                 } else {  
    126.                     String idValue = request.getParameter("id");  
    127.                     Integer invId = (StringUtils.isEmpty(idValue) || "_empty".equals(idValue)) ? 0 : Integer.parseInt(idValue);// add操作,id值默认为_empty  
    128.                     InvHeader vo = new InvHeader();  
    129.                     vo.invId = invId;  
    130.                     if ("del".equals(oper)) {  
    131.                         try {  
    132.                             service.deleteInvHeader(vo);  
    133.                             s = Json.SUCCESS;  
    134.                         } catch (ActiveRecordException e) {  
    135.                             e.printStackTrace();  
    136.                             s = Json.getFailure(e.getMessage());  
    137.                         }  
    138.                     } else {  
    139.                         String invDateValue = request.getParameter("invDate");  
    140.                         String clientIdValue = request.getParameter("client_Id");  
    141.                         String amountValue = request.getParameter("amount");  
    142.                         String taxValue = request.getParameter("tax");  
    143.                         String totalValue = request.getParameter("total");  
    144.                         String noteValue = request.getParameter("note");  
    145.                         vo.invDate = invDateValue;  
    146.                         vo.client_Id = StringUtils.isEmpty(clientIdValue) ? 0 : Integer.parseInt(clientIdValue);  
    147.                         vo.amount = StringUtils.isEmpty(amountValue) ? 0 : Float.parseFloat(amountValue);  
    148.                         vo.tax = StringUtils.isEmpty(taxValue) ? 0 : Float.parseFloat(taxValue);  
    149.                         vo.total = StringUtils.isEmpty(totalValue) ? 0 : Float.parseFloat(totalValue);  
    150.                         vo.note = noteValue;  
    151.                         if ("add".equals(oper)) {  
    152.                             try {  
    153.                                 if (service.addInvHeader(vo)) {  
    154.                                     s = Json.SUCCESS;  
    155.                                 } else {  
    156.                                     s = Json.FAILURE;  
    157.                                 }  
    158.                             } catch (ActiveRecordException e) {  
    159.                                 e.printStackTrace();  
    160.                                 s = Json.getFailure(e.getMessage());  
    161.                             }  
    162.                         } else if ("edit".equals(oper)) {  
    163.                             try {  
    164.                                 if (service.updateInvHeader(vo)) {  
    165.                                     s = Json.SUCCESS;  
    166.                                 } else {  
    167.                                     s = Json.FAILURE;  
    168.                                 }  
    169.                             } catch (ActiveRecordException e) {  
    170.                                 e.printStackTrace();  
    171.                                 s = Json.getFailure(e.getMessage());  
    172.                             }  
    173.                         }  
    174.                     }  
    175.                 }  
    176.           
    177.                 out.println(s);  
    178.                 out.flush();  
    179.                 out.close();  
    180.             }  
    181.           
    182.             /** 
    183.              * Initialization of the servlet. <br> 
    184.              *  
    185.              * @throws ServletException 
    186.              *             if an error occurs 
    187.              */  
    188.             public void init() throws ServletException {  
    189.                 // Put your code here  
    190.             }  
    191.           
    192.         }  
    ·         lib.zip (1.1 MB)
    ·         载次数: 38
    ·         demo2.zip (348.8 KB)
    ·         载次数: 52

No comments:

Post a Comment