easyui的datagrid的基本使用

最近用easyui的datagrid,此datagrid具有增删改查,满足基本的使用:  

1.jsp代码(对页面的初始化操作): 
Js代码   收藏代码
  1. <%@ page language="java" pageEncoding="UTF-8"%>  
  2. <html>  
  3. <head>  
  4.     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
  5.     <meta http-equiv="pragma" content="no-cache">  
  6.     <meta http-equiv="cache-control" content="no-cache">  
  7.       
  8.     <title>ComboBox - jQuery EasyUI Demo</title>  
  9.       
  10.     <%  
  11.         String path = request.getContextPath();  
  12.     %>  
  13.       
  14.     <link rel="stylesheet" type="text/css" href="<%=path %>/jquery_ui/themes/default/easyui.css">  
  15.     <link rel="stylesheet" type="text/css" href="<%=path %>/jquery_ui/themes/icon.css">  
  16.     <link rel="stylesheet" type="text/css" href="<%=path %>/jquery_ui/ui.css">  
  17.     <script type="text/javascript" src="<%=path %>/jquery_ui/jquery-1.6.min.js"></script>  
  18.     <script type="text/javascript" src="<%=path %>/jquery_ui/jquery.easyui.min.js"></script>  
  19.     <script>  
  20.     $(function(){  
  21.         var lastIndex;  
  22.         $('#test').datagrid({  
  23.             title:'My DataGrid',  
  24.             iconCls:'icon-save',  
  25.             width:800,  
  26.             height:"auto",  
  27.             nowrap: false,  
  28.             striped: true,  
  29.             collapsible:true,  
  30.             url:'TestAction!getGridInfo.action',  
  31.             idField:'id',  
  32.             singleSelect:true,  
  33.             fitColumns: true,  
  34.             frozenColumns:[[  
  35.                 {field:'ck',checkbox:true},  
  36.                 {title:'编号',field:'id',width:80,sortable:true}  
  37.             ]],  
  38.             columns:[[  
  39.                 {title:'基本信息',colspan:2},  
  40.                 {  
  41.                     field:'tel',  
  42.                     title:'移动电话',  
  43.                     width:100,  
  44.                     align:'center',   
  45.                     rowspan:2,  
  46.                     editor: {  
  47.                         type: 'numberbox',  
  48.                         options:{  
  49.                             required: true,  
  50.                             validType: "length[11,11]"  
  51.                         }  
  52.                           
  53.                     }  
  54.                 }  
  55.             ],[  
  56.                 {  
  57.                     field:'name',  
  58.                     title:'姓名',  
  59.                     width:120,  
  60.                     editor:'text'  
  61.                 },  
  62.                 {field:'address',title:'地址',width:220,rowspan:2,editor:'text'}  
  63.             ]],  
  64.             pagination:true,  
  65.             toolbar:[{  
  66.                 id:'btnadd',  
  67.                 text:'Add',  
  68.                 iconCls:'icon-add',  
  69.                 handler:function(){  
  70.                     $('#btnsave').linkbutton('enable');  
  71.                     alert('add')  
  72.                 }  
  73.             },{  
  74.                 id:'btncut',  
  75.                 text:'Cut',  
  76.                 iconCls:'icon-cut',  
  77.                 handler:function(){  
  78.                     $('#btnsave').linkbutton('enable');  
  79.                     alert('cut')  
  80.                 }  
  81.             },'-',{  
  82.                 id:'btnsave',  
  83.                 text:'Save',  
  84.                 disabled:true,  
  85.                 iconCls:'icon-save',  
  86.                 handler:function(){  
  87.                     $('#btnsave').linkbutton('disable');  
  88.                     alert('save')  
  89.                 }  
  90.             }],  
  91.             onClickRow: function(rowindex){  
  92.                 $('#test').datagrid('endEdit', lastIndex);  
  93.                 $('#test').datagrid('beginEdit', rowindex);  
  94.                 var ed = $('#test').datagrid('getEditors', rowindex);  
  95.                   
  96.                 for (var i = 0; i < ed.length; i++){  
  97.                     var e = ed[i];  
  98.                     $(e.target).bind("keyup",function(event){  
  99.                         if (event.which == 13){  
  100.                             $('#test').datagrid('endEdit', rowindex);  
  101.                         }  
  102.                     });  
  103.                 }  
  104.                 lastIndex = rowindex;  
  105.             },  
  106.             onAfterEdit: function(rowIndex, rowData){  
  107.                 $.ajax({  
  108.                    url: "<%=request.getContextPath()%>/TestAction!updateGrid.action",  
  109.                    data: "name="+encodeURIComponent(rowData.name)+"&address="+encodeURIComponent(rowData.address)+"&tel="+encodeURIComponent(rowData.tel)+"&id="+rowData.id,  
  110.                    cache:false,  
  111.                    async : true  
  112.                 })  
  113.             }  
  114.         });  
  115.           
  116.         var p = $('#test').datagrid('getPager');  
  117.           
  118.         $(p).pagination({  
  119.             pageSize:10,  
  120.             pageList:[10,20,50],  
  121.             beforePageText: '第',  
  122.             afterPageText: '页    共 {pages} 页',  
  123.             displayMsg: '当前显示 {from} - {to} 条记录   共 {total} 条',  
  124.             onBeforeRefresh:function(){  
  125.                 $("#test").datagrid("reload");  
  126.             }  
  127.         });  
  128.     });  
  129.   
  130.     function search(value){  
  131.         var p = $('#test').datagrid('getPager');  
  132.         $(p).pagination({  
  133.             pageNumber: 1  
  134.         });  
  135.         var queryParams = $('#test').datagrid('options').queryParams;   
  136.         queryParams.name=value;  
  137.         $('#test').datagrid("reload");  
  138.     }  
  139.     </script>  
  140.       
  141.     <style type="text/css">  
  142.         .demo-info{  
  143.             background:#FFFEE6;  
  144.             color:#8F5700;  
  145.             padding:12px;  
  146.         }  
  147.         .demo-tip{  
  148.             width:24px;  
  149.             height:16px;  
  150.             float:left;  
  151.         }  
  152.     </style>  
  153. </head>  
  154. <body>  
  155.     <h2>Complex DataGrid</h2>  
  156.     <div class="demo-info">  
  157.         <div class="demo-tip icon-tip"></div>  
  158.         <div>Click the button to do actions with datagrid.</div>  
  159.     </div>  
  160.       
  161.     <input class='easyui-searchbox' prompt='请输入姓名进行搜索' style='width:798px' searcher='search'/>  
  162.       
  163.     <table id="test"></table>  
  164. </body>  
  165. </html>  


2.处理逻辑的TestAction类: 
Java代码   收藏代码
  1. public class TestAction extends ActionSupport{  
  2.       
  3.     private int rows;  
  4.     private int page;  
  5.     private long total;  
  6.       
  7.     private QueryDao dao = new QueryDao();  
  8.   
  9.     public long getTotal() {  
  10.         return total;  
  11.     }  
  12.   
  13.     public void setTotal(long total) {  
  14.         this.total = total;  
  15.     }  
  16.   
  17.     public int getRows() {  
  18.         return rows;  
  19.     }  
  20.   
  21.     public void setRows(int rows) {  
  22.         this.rows = rows;  
  23.     }  
  24.   
  25.     public int getPage() {  
  26.         return page;  
  27.     }  
  28.   
  29.     public void setPage(int page) {  
  30.         this.page = page;  
  31.     }  
  32.       
  33.     public void getGridInfo(){  
  34.         HttpServletRequest request = ServletActionContext.getRequest();  
  35.         List<Grid> list = new ArrayList<Grid>();  
  36.         String name = request.getParameter("name");  
  37.           
  38.         if(!("".equals(name) || name == null)){  
  39.             //根据名称进行模糊查询,并初始化查询出来的数据的页数和行数  
  40.             name = name.trim();  
  41.             list = dao.getGridByName(name, page, rows);  
  42.             total = dao.getSize();  
  43.         }else{  
  44.             total = dao.getTotal();  
  45.             list=dao.getGridData(page ,rows);  
  46.         }  
  47.         Map<String,Object> jsonMap = new HashMap<String,Object>();  
  48.         jsonMap.put("total", total);  
  49.         jsonMap.put("rows", list);  
  50.         JSONObject json=new JSONObject();  
  51.         json=JSONObject.fromObject(jsonMap);  
  52.         try {  
  53.             ServletActionContext.getResponse().setContentType("text/html;charset=utf-8");  
  54.             PrintWriter pw=ServletActionContext.getResponse().getWriter();  
  55.             pw.println(json.toString());   
  56.             pw.flush();  
  57.             pw.close();  
  58.         } catch (IOException e) {  
  59.             e.printStackTrace();  
  60.         }  
  61.     }  
  62.   
  63. public void updateGrid(){  
  64.         String id=(String)ServletActionContext.getRequest().getParameter("id");  
  65.         String name=(String)ServletActionContext.getRequest().getParameter("name");  
  66.         String address=(String)ServletActionContext.getRequest().getParameter("address");  
  67.         String tel=(String)ServletActionContext.getRequest().getParameter("tel");  
  68.         String hql="update Grid g set g.name=?,g.address=?,g.tel=? where g.id=?";  
  69.         try  
  70.         {  
  71.             DataAccess.createPersister().saveOrUpdateBySqlString(hql,new Object[]{name,address,tel,id});  
  72.             dao.updateGridCache(id);  
  73.         }  
  74.         catch (Exception ex)  
  75.         {  
  76.             ex.printStackTrace();  
  77.         }  
  78.     }  
  79.   
  80. }  


3.查询数据的QueryDao(如果大数据量,建议使用缓存): 
Java代码   收藏代码
  1. public class QueryDao{  
  2.       
  3.     private long size;  
  4.       
  5.     private int initPage;  
  6.           
  7.     public int getInitPage() {  
  8.         return initPage;  
  9.     }  
  10.       
  11.     public long getSize() {  
  12.         return size;  
  13.     }  
  14.     /** 
  15.      * 查询出所有的grid数据,便于进行大数据量的模糊查询 
  16.      * @return 
  17.      */  
  18.     public List<Grid> getAllGrid(){  
  19.           
  20.         String hql = "from Grid";  
  21.         try  
  22.         {  
  23.             Query query = DataAccess.createQuery();  
  24.               
  25.             @SuppressWarnings("unchecked")  
  26.             List<Grid> gridResult = query.queryByHqlString(hql);  
  27.             return gridResult;  
  28.         }  
  29.         catch (Exception ex)  
  30.         {  
  31.             ex.printStackTrace();  
  32.         }  
  33.         return new ArrayList<Grid>();  
  34.     }  
  35.       
  36.     public List<Grid> getGridData(int page , int row){  
  37.         //Cache cache = CacheManager.getInstance().getCache("grid");  
  38.         List<Grid> list = (List<Grid>)getAllGrid();  
  39.         return spliList(list, page, row);  
  40.     }  
  41.       
  42.     public Long getTotal(){  
  43.         String countHql = "select count(*) from Grid";  
  44.         try  
  45.         {  
  46.             Query query = DataAccess.createQuery();  
  47.               
  48.             List countList = query.queryByHqlString(countHql);  
  49.             long total = (Long)countList.get(0);              
  50.             return total;  
  51.         }  
  52.         catch (Exception ex)  
  53.         {  
  54.             ex.printStackTrace();  
  55.         }  
  56.         return 0L;  
  57.     }  
  58.       
  59.     public Grid getGridById(String id){  
  60.         String hql = "from Grid where id=?";  
  61.         try  
  62.         {  
  63.             Query query = DataAccess.createQuery();  
  64.               
  65.             List countList = query.queryByHqlString(hql,new Object[]{id});  
  66.             return (Grid)countList.get(0);  
  67.         }  
  68.         catch (Exception ex)  
  69.         {  
  70.             ex.printStackTrace();  
  71.         }  
  72.         return new Grid();  
  73.     }  
  74.       
  75.     /** 
  76.      * 查询包含此数据名的数据 
  77.      * @param name 
  78.      * @param total 
  79.      * @return 
  80.      */  
  81.     public List<Grid> getGridByName(String name,int page, int rows){  
  82.         List<Grid> gridList = new ArrayList<Grid>();  
  83.         Cache cache = CacheManager.getInstance().getCache("grid");  
  84.         List<Grid> list = (List<Grid>)cache.getValue();  
  85.         for(int i = 0, len = list.size(); i < len; i++){  
  86.             Grid grid = (Grid) list.get(i);  
  87.             if(grid.getName().contains(name)){  
  88.                 gridList.add(grid);  
  89.             }  
  90.         }  
  91.         size = gridList.size();  
  92.         return spliList(gridList, page, rows);  
  93.     }  
  94.       
  95.     /** 
  96.      * 对list进行分页 
  97.      * @param list 
  98.      * @param page 
  99.      * @param rows 
  100.      * @return 
  101.      */  
  102.     public List spliList(List list, int page, int rows){  
  103.         List grid = new ArrayList();  
  104.         int a = (page - 1) * rows;  
  105.         int b = a + rows;  
  106.         for(int i = a; i < b; i++){  
  107.             try {  
  108.                 grid.add(list.get(i));  
  109.             } catch (IndexOutOfBoundsException e) {  
  110.                 return grid;  
  111.             }  
  112.         }  
  113.         return grid;  
  114.     }  
  115.       
  116.     public void updateGridCache(String id){  
  117.         Cache cache = CacheManager.getInstance().getCache("grid");  
  118.         List<Grid> list = (List<Grid>)cache.getValue();  
  119.         for (int i = 0, len = list.size(); i < len; i++) {  
  120.             Grid grid  = list.get(i);  
  121.             if(id.equals(grid.getId())){  
  122.                 Grid updateGrid = getGridById(id);  
  123.                 list.remove(i);  
  124.                 list.add(i, updateGrid);  
  125.                 cache.setValue(list);  
  126.                 return;  
  127.             }  
  128.         }  
  129.     }  
  130. }  


以上就是如何基本使用easyui的datagrid...
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值