jqGrid:四、 remote data(JSON)

页面
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
    <head>
        <title>grid.html</title>
        <meta http-equiv="keywords" content="keyword1,keyword2,keyword3" />
        <meta http-equiv="description" content="this is my page" />
        <meta http-equiv="content-type" content="text/html; charset=UTF-8" />

        <link rel="stylesheet" type="text/css" media="screen"
            href="css/themes/redmond/jquery-ui-1.8.2.custom.css" />
        <link rel="stylesheet" type="text/css" media="screen"
            href="css/themes/ui.jqgrid.css" />
        <link rel="stylesheet" type="text/css" media="screen"
            href="css/themes/ui.multiselect.css" />
        <link rel="stylesheet" type="text/css" media="screen"
            href="css/themes/jquery.searchFilter.css" />
        <style>
html,body {
    --margin: 0; /* Remove body margin/padding */
    padding: 0;
    overflow: hidden; /* Remove scroll bars on browser window */
    font-size: 75%;
}
</style>

        <script src="js/jquery-1.4.2.min.js" type="text/javascript"></script>
        <script src="js/jquery-ui-1.8.2.custom.min.js" type="text/javascript"></script>
        <script src="js/src/ui.multiselect.js"
            type="text/javascript"></script>
        <script src="js/src/grid.loader.js" type="text/javascript"></script>
        <script type="text/javascript">
            $.jgrid.no_legacy_api = true;
            $.jgrid.useJSON = true;
        </script>
        <script type="text/javascript">
            $(function(){ 
              $("#grid_id").jqGrid({
                url:'/demo2/servlet/JqGridJsonServlet',
                mtype: 'GET',
                datatype: 'json',
                jsonReader : {
                   repeatitems: false
                },
                height: "auto",
                loadui: "disable",
                colNames:['Inv No','Date', 'Amount','Tax','Total','Notes'],
                colModel :[ 
                  {name:'invId', index:'invId', width:70}, 
                  {name:'invDate', index:'invDate', width:120, editable:true}, 
                  {name:'amount', index:'amount', width:90, align:'right', editable:true}, 
                  {name:'tax', index:'tax', width:90, align:'right', editable:true}, 
                  {name:'total', index:'total', width:90, align:'right', editable:true}, 
                  {name:'note', index:'note', width:180, sortable:false, editable:true} 
                ],
                pager: '#pager',
                rowNum:10,
                rowList:[10,20,30],
                sortname: 'invid',
                sortorder: 'asc',
                viewrecords: true,
                caption: 'My first grid'
              });
            }); 
        </script>
    </head>
    <body>
        <table id="grid_id"></table>
        <div id="pager"></div>
    </body>
</html>

servlet
package com.qoma.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;

import com.et.ar.exception.ActiveRecordException;
import com.qoma.db.vo.InvHeader;
import com.qoma.service.InvHeaderService;
import com.qoma.util.Json;

public class JqGridJsonServlet extends HttpServlet {

    /**
     * 
     */
    private static final long serialVersionUID = 1676458940650461673L;

    private InvHeaderService service = new InvHeaderService();

    /**
     * Constructor of the object.
     */
    public JqGridJsonServlet() {
        super();
    }

    /**
     * Destruction of the servlet. <br>
     */
    public void destroy() {
        super.destroy(); // Just puts "destroy" string in log
        // Put your code here
    }

    /**
     * The doGet method of the servlet. <br>
     * 
     * This method is called when a form has its tag value method equals to get.
     * 
     * @param request
     *            the request send by the client to the server
     * @param response
     *            the response send by the server to the client
     * @throws ServletException
     *             if an error occurred
     * @throws IOException
     *             if an error occurred
     */
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }

    /**
     * The doPost method of the servlet. <br>
     * 
     * This method is called when a form has its tag value method equals to
     * post.
     * 
     * @param request
     *            the request send by the client to the server
     * @param response
     *            the response send by the server to the client
     * @throws ServletException
     *             if an error occurred
     * @throws IOException
     *             if an error occurred
     */
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        response.setContentType("text/html");
        PrintWriter out = response.getWriter();

        String oper = request.getParameter("oper");
        String s = "";
        if (null == oper || "".equals(oper)) {
            Integer page = Integer.parseInt(request.getParameter("page"));
            Integer limit = Integer.parseInt(request.getParameter("rows"));
            String sidx = request.getParameter("sidx");
            String sord = request.getParameter("sord");
            if (null == sidx || "".equals(sidx))
                sidx = "1";

            Long count = 0L;
            try {
                count = service.getCount();
            } catch (ActiveRecordException e) {
                e.printStackTrace();
            }
            Integer totalPages = 0;
            if (count > 0 && limit > 0) {
                totalPages = new Long(count / limit).intValue();
                if (count % limit != 0) {
                    totalPages += 1;
                }
            } else {
                totalPages = 0;
            }

            // if for some reasons the requested page is greater than the total
            // set the requested page to total page
            if (page > totalPages)
                page = totalPages;

            // calculate the starting position of the rows
            Integer start = limit * page - limit;

            if (start < 0)
                start = 0;

            try {
                List<InvHeader> list = service.getLimitList(start, limit, sidx, sord);
                s = service.getAllJson(page, totalPages, count, list);
            } catch (ActiveRecordException e) {
                e.printStackTrace();
                s = Json.FAILURE;
            }
        } else {
            String idValue = request.getParameter("id");
            Integer invId = (StringUtils.isEmpty(idValue) || "_empty".equals(idValue)) ? 0 : Integer.parseInt(idValue);// add操作时,id值默认为_empty
            InvHeader vo = new InvHeader();
            vo.invId = invId;
            if ("del".equals(oper)) {
                try {
                    service.deleteInvHeader(vo);
                    s = Json.SUCCESS;
                } catch (ActiveRecordException e) {
                    e.printStackTrace();
                    s = Json.getFailure(e.getMessage());
                }
            } else {
                String invDateValue = request.getParameter("invDate");
                String clientIdValue = request.getParameter("client_Id");
                String amountValue = request.getParameter("amount");
                String taxValue = request.getParameter("tax");
                String totalValue = request.getParameter("total");
                String noteValue = request.getParameter("note");
                vo.invDate = invDateValue;
                vo.client_Id = StringUtils.isEmpty(clientIdValue) ? 0 : Integer.parseInt(clientIdValue);
                vo.amount = StringUtils.isEmpty(amountValue) ? 0 : Float.parseFloat(amountValue);
                vo.tax = StringUtils.isEmpty(taxValue) ? 0 : Float.parseFloat(taxValue);
                vo.total = StringUtils.isEmpty(totalValue) ? 0 : Float.parseFloat(totalValue);
                vo.note = noteValue;
                if ("add".equals(oper)) {
                    try {
                        if (service.addInvHeader(vo)) {
                            s = Json.SUCCESS;
                        } else {
                            s = Json.FAILURE;
                        }
                    } catch (ActiveRecordException e) {
                        e.printStackTrace();
                        s = Json.getFailure(e.getMessage());
                    }
                } else if ("edit".equals(oper)) {
                    try {
                        if (service.updateInvHeader(vo)) {
                            s = Json.SUCCESS;
                        } else {
                            s = Json.FAILURE;
                        }
                    } catch (ActiveRecordException e) {
                        e.printStackTrace();
                        s = Json.getFailure(e.getMessage());
                    }
                }
            }
        }

        out.println(s);
        out.flush();
        out.close();
    }

    /**
     * Initialization of the servlet. <br>
     * 
     * @throws ServletException
     *             if an error occurs
     */
    public void init() throws ServletException {
        // Put your code here
    }

}

你可能感兴趣的:(apache,html,json,servlet,XHTML)