標籤:des 分頁控制項 style blog http io ar color sp
由於項目原因,用了jquery easyui 感覺介面不錯,皮膚樣式少點,可是官網最近打不開了,資料比較少,給的demo沒有想要的效果,今天在用datagrid 做分頁顯示的時候,折騰了半天,網上的資料也比較少,後自己動手,終於解決,廢話不說,開始:
datagrid分頁有一個附加的分頁控制項,只需後台擷取分頁控制項自動認可的兩個參數rows每頁顯示的記錄數和page;//當前第幾頁
然後讀取相應頁數的記錄,和總記錄數total一塊返回即可 介面如下:
1、下邊是datagrid的顯示對話方塊,我直接用table把列頭顯示出來,感覺比用js寫要易於閱讀
<table id="list_data" cellspacing="0" cellpadding="0"> <thead> <tr> <th field="fldAppDept" width="100">部門</th> <th field="fldAppNode" width="100">網站</th> <th field="fldAppName" width="100">名稱</th> <th field="fldAppMgr" width="100">管理員</th> <th field="fldAppNote" width="100">注釋</th> <th field="fldAppType" width="100">類型</th> <th field="fldTelphone" width="100">電話</th> <th field="fldAppImg" width="100">職務</th> <th field="fldAppMonitor" width="100">啟用監測</th> <th field="fldAppLevel" width="100">要重層級</th> </tr> </thead> </table>
2、js代碼,用於構建datagrid
注意 要想顯示分頁控制項,pagination屬性必須為true
$(function() { //datagrid初始化 $(‘#list_data‘).datagrid({ title:‘應用系統列表‘, iconCls:‘icon-edit‘,//表徵圖 width: 700, height: ‘auto‘, nowrap: false, striped: true, border: true, collapsible:false,//是否可摺疊的 fit: true,//自動大小 url:‘listApp.action‘, //sortName: ‘code‘, //sortOrder: ‘desc‘, remoteSort:false, idField:‘fldId‘, singleSelect:false,//是否單選 pagination:true,//分頁控制項 rownumbers:true,//行號 frozenColumns:[[ {field:‘ck‘,checkbox:true} ]], toolbar: [{ text: ‘添加‘, iconCls: ‘icon-add‘, handler: function() { openDialog("add_dialog","add"); } }, ‘-‘, { text: ‘修改‘, iconCls: ‘icon-edit‘, handler: function() { openDialog("add_dialog","edit"); } }, ‘-‘,{ text: ‘刪除‘, iconCls: ‘icon-remove‘, handler: function(){ delAppInfo(); } }], }); //設定分頁控制項 var p = $(‘#list_data‘).datagrid(‘getPager‘); $(p).pagination({ pageSize: 10,//每頁顯示的記錄條數,預設為10 pageList: [5,10,15],//可以設定每頁記錄條數的列表 beforePageText: ‘第‘,//頁數文字框前顯示的漢字 afterPageText: ‘頁 共 {pages} 頁‘, displayMsg: ‘當前顯示 {from} - {to} 條記錄 共 {total} 條記錄‘, /*onBeforeRefresh:function(){ $(this).pagination(‘loading‘); alert(‘before refresh‘); $(this).pagination(‘loaded‘); }*/ }); });
3、後台我是通過struts2處理的資料 返回json串
private JSONObject result;//返回的json private String rows;//每頁顯示的記錄數 private String page;//當前第幾頁 private AppServiceInter appService; public JSONObject getResult() { return result; } public void setResult(JSONObject result) { this.result = result; } public void setAppService(AppServiceInter appService) { this.appService = appService; } public String getRows() { return rows; } public void setRows(String rows) { this.rows = rows; } public String getPage() { return page; } public void setPage(String page) { this.page = page; } /** * 查詢應用系統 * @return */ public String listApp() { System.out.println("---------------"); //當前頁 int intPage = Integer.parseInt((page == null || page == "0") ? "1":page); //每頁顯示條數 int number = Integer.parseInt((rows == null || rows == "0") ? "10":rows); //每頁的開始記錄 第一頁為1 第二頁為number +1 int start = (intPage-1)*number; List<TblApp> list = appService.findByPageApp(start,number);//每頁的資料,放入list Map<String, Object> jsonMap = new HashMap<String, Object>();//定義map jsonMap.put("total", appService.getCountApp());//total鍵 存放總記錄數,必須的 jsonMap.put("rows", list);//rows鍵 存放每頁記錄 list result = JSONObject.fromObject(jsonMap);//格式化result 一定要是JSONObject //result = JSONArray.fromObject(jsonMap); return SUCCESS; }
4、附上struts.xml設定檔
<package name="app" extends="json-default"> <action name="listApp" class="appAction" method="listApp"> <result type="json"> <param name="root">result</param> </result> </action> </package>
特寫出這些,方便自己或他人以後參考 ,如果有什麼問題大家可以留言......
本文轉自:http://www.cnblogs.com/huozhicheng/archive/2011/09/27/2193605.html
jquery easyui datagrid 分頁詳解