在學習過程中,可以參照JQuery EasyUI的官方網站學習。地址:http://www.jeasyui.com/demo/main/index.php
在學習JQuery EasyUI中的DataGrid標簽時,可以參照http://www.jeasyui.com/documentation/datagrid.php進行學習。
本文主要講解DataGrid標簽的使用,怎么設置參數,怎么向后台請求數據等。
在使用過程中,我使用的是1.4版本,存在一個問題,當請求數據為0行時,會重新請求一次。
這一個問題,園中兄弟幫我解決了,地址是:http://www.cnblogs.com/Reaver/p/4056770.html,感謝flyreaver
一、在頁面中添加標簽
1 @*這是datagrid標簽展示的地方,其中iconcls表示datagrid中左上角展示的圖標*@ 2 <table id="dg" iconcls="icon-edit"> 3 </table> 4 @*如果需要彈出層,請參照下邊的代碼,並在js中做相應的設置*@ 5 <div id="dlg" class="easyui-dialog" title="彈出框標題" style="width: 400px; height: 200px; padding: 10px" 6 data-options=" 7 iconCls: 'icon-add', 8 buttons: [{ 9 text:'確定', 10 iconCls:'icon-ok', 11 handler:function(){ 12 //點擊'確定'按鈕觸發的事件 13 UpdateData(); 14 } 15 },{ 16 text:'取消', 17 handler:function(){ 18 alert('cancel');; 19 } 20 }] 21 "> 22 <div class="aaa"> 23 <span>主鍵id:</span><input type="text" /> 24 </div> 25 </div>
二、在js中添加方法,如下所示:
1 <script type="text/javascript"> 2 $(function () { 3 loadData(); 4 }); 5 6 function loadData() { 7 $('#dg').datagrid({ 8 url: '/Financial/GetLoanDataInfo',//請求方法的地址 9 title: '信息管理', 10 width: 900, 11 height: 400, 12 fitColumns: true, //列自適應 13 nowrap: false,//禁止文字自動換行 14 idField: 'CID',//主鍵列的列明 15 loadMsg: '正在加載信息...',//當數據沒有加載出來時顯示的文字 16 pagination: true,//是否有分頁 17 singleSelect: true,//是否單行選擇 18 pagePosition: 'bottom',//分頁符在底部,可選參數為top,bottom,both 19 pageSize: 15,//頁大小,一頁多少條數據 20 pageNumber: 1,//默認當前的頁碼 21 pageList: [15, 30, 50],//一頁可顯示數據的條目 22 queryParams: {},//往后台傳遞參數,json格式 23 columns: [[ 24 //field表示綁定的字段名,如不綁定就隨便寫一個; 25 //checkbox: true表示這一列是checkbox 26 //hidden: true表示隱藏這一列 27 //sortable: true表示單擊這一列可以排(升/降)序,這樣請求數據時會向后台多傳輸兩個參數 28 { field: 'ck', checkbox: true, align: 'left', width: 50 }, 29 { field: 'CID', title: '1', width: 80, align: 'center', hidden: true }, 30 { field: 'CMoney', title: '2', width: 80, align: 'center', sortable: true }, 31 { 32 field: 'CAge', title: '3', width: 80, align: 'center', 33 //這里表示對顯示的信息做處理。value表示原本的值,在方法中對value進行邏輯判斷並返回處理后的標簽樣式 34 formatter: function (value, row, index) { 35 //在該函數中,對文本大小判斷,如果大於18,顯示為紅色的字 36 if (value > 18) { 37 return '<span style="color:red;">' + value + '</span>'; 38 } else { 39 return value; 40 } 41 } 42 }, 43 { 44 field: 'CTitle', title: '7', width: 80, align: 'center', 45 formatter: function (value, row, index) { 46 //在該函數中,把這一列變為一個a標簽。設置class和自定義數據。為下邊做准備 47 return "<a href='javascript:void(0);' data-id='" + row.CID + "' class='detailLink'>" + value + "</a>"; 48 } 49 } 50 ]], 51 //綁定數據成功后執行這個方法 52 onLoadSuccess: function () { 53 //先把彈出框關閉 54 $('#dlg').dialog('close'); 55 //添加最后一列的單擊事件 56 $('.detailLink').click(function () { 57 //獲取自定義數據 58 var id = $(this).data('id'); 59 //為彈出的層中的標簽賦值 60 $('.aaa input').attr('value', id); 61 //彈出層 62 $('#dlg').dialog('open'); 63 }); 64 } 65 }); 66 } 67 function UpdateData() { 68 //點擊彈出層的確定按鈕觸發的事件——刷新datagrid 69 $('#dg').datagrid('reload'); 70 }; 71 </script>
三、后端得到datagrid傳過去的參數,並返回數據
1 //在本系統中,datagrid請求的方法需要是Action類型的方法 2 public ActionResult GetLoanDataInfo() 3 { 4 int pageIndex; 5 int pageSize; 6 string sortName = string.Empty; 7 string order = string.Empty; 8 //page是datagrid內部實現的,傳過來的參數名。表示第幾頁 9 if (!int.TryParse(Request.Form["page"], out pageIndex)) 10 { 11 pageIndex = 1; 12 } 13 //rows表示這一頁顯示多少條數據 14 if (!int.TryParse(Request.Form["rows"], out pageSize)) 15 { 16 pageSize = 5; 17 } 18 //sort表示根據那一列排序;order表示升序或者降序,有兩個值:asc、desc 19 if (!string.IsNullOrEmpty(Request.Form["sort"])) 20 { 21 sortName = Request.Form["sort"]; 22 order = Request.Form["order"]; 23 } 24 //這一塊是從后台請求數據,把上邊的參數傳進去(本例是模擬的,沒有傳參數進方法,應該在方法中同時把總數據的條目數out出來) 25 List<TLoan> list = _DataHelper.GetData(); 26 int totalCount = list.Count(); 27 list = list.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList(); 28 //把需要傳給前台的數據,分揀出來,賦給rows,這樣可以把不需要的數據過濾掉,如果不用過濾數據,可以省略下邊一行代碼 29 var rows = from l in list 30 select new { CID = l.CID, CMoney= l.CMoney, CAge= l.CAge, CTitle= l.CTitle };
31 32 //rows = rows, total = totalCount;等號之前的名字是不可變的,因為是easy ui規定的。 33 //JsonRequestBehavior.AllowGet表示該方法可以被get方式請求,否則get方式請求得不到數據 34 return Json(new { rows = rows, total = totalCount }, JsonRequestBehavior.AllowGet); 35 }