jquery easyui datagrid 分頁 詳解

由於項目原因,用了jquery easyui 感覺界面不錯,皮膚樣式少點,可是官網最近打不開了,資料比較少,給的demo沒有想要的效果,今天在用datagrid 做分頁顯示的時候,折騰了半天,網上的資料也比較少,後自己動手,終於解決,廢話不說,開始:

datagrid分頁 有一個附加的分頁控件,只需後臺獲取分頁控件自動提交的兩個參數rows每頁顯示的記錄數和page;//當前第幾頁

然後讀取相應頁數的記錄,和總記錄數total一塊返回即可 界面如下:



1、下邊是datagrid的顯示對話框,我直接用table把列頭顯示出來,感覺比用js寫要易於閱讀

  1. <tableid="list_data"cellspacing="0"cellpadding="0"> 
  2.     <thead> 
  3.         <tr> 
  4.             <thfield="fldAppDept"width="100">部門</th> 
  5.             <thfield="fldAppNode"width="100">網站</th> 
  6.             <thfield="fldAppName"width="100">名稱</th> 
  7.             <thfield="fldAppMgr"width="100">管理員</th> 
  8.             <thfield="fldAppNote"width="100">註釋</th> 
  9.             <thfield="fldAppType"width="100">類型</th> 
  10.             <thfield="fldTelphone"width="100">電話</th> 
  11.             <thfield="fldAppImg"width="100">職務</th> 
  12.             <thfield="fldAppMonitor"width="100">啓用監測</th> 
  13.             <thfield="fldAppLevel"width="100">要重級別</th> 
  14.         </tr> 
  15.     </thead> 
  16. </table> 

2、js代碼,用於構建datagrid

注意 要想顯示分頁控件,pagination屬性必須爲true

[javascript] view plaincopy
  1. //datagrid初始化 
  2.     $('#list_data').datagrid({ 
  3.         title:'應用系統列表'
  4.         iconCls:'icon-edit',//圖標 
  5.         width: 700, 
  6.         height: 'auto'
  7.         nowrap: false
  8.         striped: true
  9.         border: true
  10.         collapsible:false,//是否可摺疊的 
  11.         fit: true,//自動大小 
  12.         url:'listApp.action'
  13.         //sortName: 'code', 
  14.         //sortOrder: 'desc', 
  15.         remoteSort:false,  
  16.         idField:'fldId'
  17.         singleSelect:false,//是否單選 
  18.         pagination:true,//分頁控件 
  19.         rownumbers:true,//行號 
  20.         frozenColumns:[[ 
  21.             {field:'ck',checkbox:true
  22.         ]], 
  23.         toolbar: [{ 
  24.             text: '添加'
  25.             iconCls: 'icon-add'
  26.             handler: function() { 
  27.                 openDialog("add_dialog","add"); 
  28.             } 
  29.         }, '-', { 
  30.             text: '修改'
  31.             iconCls: 'icon-edit'
  32.             handler: function() { 
  33.                 openDialog("add_dialog","edit"); 
  34.             } 
  35.         }, '-',{ 
  36.             text: '刪除'
  37.             iconCls: 'icon-remove'
  38.             handler: function(){ 
  39.                 delAppInfo(); 
  40.             } 
  41.         }], 
  42.     }); 
  43.     //設置分頁控件 
  44.     var p = $('#list_data').datagrid('getPager'); 
  45.     $(p).pagination({ 
  46.         pageSize: 10,//每頁顯示的記錄條數,默認爲10 
  47.         pageList: [5,10,15],//可以設置每頁記錄條數的列表 
  48.         beforePageText: '第',//頁數文本框前顯示的漢字 
  49.         afterPageText: '頁    共 {pages} 頁'
  50.         displayMsg: '當前顯示 {from} - {to} 條記錄   共 {total} 條記錄'
  51.         /*onBeforeRefresh:function(){
  52.             $(this).pagination('loading');
  53.             alert('before refresh');
  54.             $(this).pagination('loaded');
  55.         }*/ 
  56.     }); 
3、後臺我是通過struts2處理的數據 返回json串
  1. private JSONObject result;//返回的json 
  2.      
  3.     private String rows;//每頁顯示的記錄數 
  4.      
  5.     private String page;//當前第幾頁 
  6.  
  7.         private AppServiceInter appService; 
  8.  
  9.     public JSONObject getResult() { 
  10.         return result; 
  11.     } 
  12.     public void setResult(JSONObject result) { 
  13.         this.result = result; 
  14.     } 
  15.     public void setAppService(AppServiceInter appService) { 
  16.         this.appService = appService; 
  17.     } 
  18.  
  19.         public String getRows() { 
  20.         return rows; 
  21.     } 
  22.     public void setRows(String rows) { 
  23.         this.rows = rows; 
  24.     } 
  25.     public String getPage() { 
  26.         return page; 
  27.     } 
  28.     public void setPage(String page) { 
  29.         this.page = page; 
  30.     } 
  31.         /**
  32.      * 查詢應用系統
  33.      * @return
  34.      */ 
  35.     public String listApp() { 
  36.         System.out.println("---------------"); 
  37.         //當前頁 
  38.         int intPage = Integer.parseInt((page ==null || page =="0") ?"1":page); 
  39.         //每頁顯示條數 
  40.         int number = Integer.parseInt((rows ==null || rows =="0") ?"10":rows); 
  41.         //每頁的開始記錄  第一頁爲1  第二頁爲number +1 
  42.         int start = (intPage-1)*number; 
  43.          
  44.         List<TblApp> list = appService.findByPageApp(start,number);//每頁的數據,放入list 
  45.             Map<String, Object> jsonMap = new HashMap<String, Object>();//定義map 
  46.             jsonMap.put("total", appService.getCountApp());//total鍵 存放總記錄數,必須的 
  47.             jsonMap.put("rows", list);//rows鍵 存放每頁記錄 list 
  48.             result = JSONObject.fromObject(jsonMap);//格式化result   一定要是JSONObject 
  49.          
  50.         //result = JSONArray.fromObject(jsonMap); 
  51.         return SUCCESS; 
  52.     } 

4、附上struts.xml配置文件

  1. <packagename="app"extends="json-default"> 
  2.         <actionname="listApp"class="appAction"method="listApp"> 
  3.             <resulttype="json"> 
  4.                 <paramname="root">result</param> 
  5.             </result> 
  6.         </action> 
  7. </package> 
發佈了27 篇原創文章 · 獲贊 2 · 訪問量 14萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章