本文主要是介绍[ASP.NET] 於ASP.NET使用 jQuery EasyUi DataGrid 存取資料,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
最近逛了逛 jQuery 資料又發現了一個新的 UI 可以使用,就是 jQuery EasyUI,沒注意到是甚麼時候出現的,之前印象只有jquery ui跟jquery tools,看了一下他的文件內容跟Demo資料,其實也提供蠻多好用的plugin,其中覺得他的datagrid感覺還不錯,順手寫了一個範例記錄一下。
jQuery EasyUi 網址: http://www.jeasyui.com
jQuery EasyUi 中文文件: http://www.phptogether.com/juidoc/
範例
Step 1
首先要去 jQuery EasyUi 網站下載檔案,最新版本為 jQuery EasyUI 1.2.5

Step 2
建立新網站,加入jquery及easyui套件

Step 3
建立一頁新aspx網頁及ashx泛型處理常式用來處理ajax需求,將js檔案載入aspx頁面,依序是css, jquery.js, easyui.js

- jquery.easyui.min.js為包含所有plugin,當然也可以單獨載入要使用的plugin檔案,
- 如果需要使用繁體語系,可以在所有.js檔最後面載入 locale 資料夾內的 easyui-lang-zh_TW.js 檔案。
- 因官網下載的datagrid有如果在page中將datagrid放在form標籤中,選擇跳頁會post的問題,所以這邊需要重新載入原作者提供給我的修正檔pagination plugin。
Step 4
依照官網範例其實就可以很簡單的建立起基本雛型,這邊也一樣實際做一下,針對DataGrid easyui是使用<table>標籤,所以在body內加入一個table並且設定一下表頭欄位,表頭欄位也可以在 script 內的 columns properties設定 ,dgl 的 div 是為了使用 Dialog 來做新增資料的介面。


Step 5
接下來就開始撰寫 Java Script 碼,基本用法如下:
2 | url:'CRUDHandler.ashx', |
4 | {field:'id',title:'ID',width:100}, |
5 | {field:'name',title:'Name',width:100}, |
6 | {field:'age',title:'Age',width:100,align:'right'}, |
7 | {field:'address',title:'Address',width:100} |
這使用尚有很多種屬性可以設定,這些屬性在官方文檔內都可以找的到,在這邊我輸入的Script如下:
02 | var qParams = { id: $("#txtid").val(), name: $("#txtname").val() }; |
13 | url: 'CRUDHandler.ashx', |
15 | frozenColumns: [[{ field: 'ck', checkbox: true}]], |
16 | pageList: [10, 15, 20], |
23 | handler: function () { |
27 | onClickRow: function (rowIndex) { |
28 | if (oldRowIndex == rowIndex) { |
29 | opt.datagrid('clearSelections', oldRowIndex); |
31 | var selectRow = opt.datagrid('getSelected'); |
32 | oldRowIndex = opt.datagrid('getRowIndex', selectRow); |
34 | }).datagrid("getPager").pagination({ |
39 | handler: function () { |
45 | iconCls: 'icon-remove', |
46 | handler: function () { |
50 | onBeforeRefresh: function () { |
54 | $("#btnQry").click(function(){ |
以上這段為主體的Script,這個DataGrid可以處理 查詢、新增、更新、刪除,傳輸資料的格式主要為JSON,接下來必須將對應的方法都補上,開啟新增畫面方法:
2 | function insertRow(opt) { |
3 | var offset = opt.offset(); |
4 | $('#dlg').dialog('open').dialog('setTitle', '新增資料').dialog('move', { |
儲存資料方法:
03 | if ($(".easyui-validatebox").val() != "") { |
04 | $.post('CRUDHandler.ashx', 'mode=INS&' + $('#frmAjax input').serialize(), function (result) { |
06 | $('#dlg').dialog('close'); |
07 | $.messager.alert('Success', '新增成功!', 'info', function () { |
08 | $('#grid').datagrid('reload'); |
11 | $.messager.alert('Error', result.msg, 'warning'); |
16 | $.messager.alert('警告', '未輸入ID!', 'warning'); |
編輯資料列方法(這邊使用直接跳頁至Detail頁面處理:
3 | var row = $("#grid").datagrid('getSelected'); |
5 | window.location.href = "Detail.aspx?mode=UPD&pk=" + row.id; |
7 | $.messager.alert('訊息', '尚未選擇需編輯的資料列!', 'info'); |
刪除資料列方法:
02 | function removeRow() { |
03 | var row = $("#grid").datagrid('getSelected'); |
05 | $.messager.confirm('Confirm', '確定要刪除此筆資料?', function (r) { |
07 | var index = $("#grid").datagrid('getRowIndex', row); |
09 | $.post('CRUDHandler.ashx', { 'mode': 'DEL', id: row.id }, function (result) { |
11 | $.messager.alert('Success', '刪除成功!', 'info', function () { |
12 | $('#grid').datagrid('reload'); |
15 | $.messager.alert('Error', result.msg, 'warning'); |
23 | $.messager.alert('訊息', '尚未選擇需刪除的資料列!', 'info'); |
查詢方法:
03 | if ($("#txtid").val() != "") |
04 | qid = $("#txtid").val(); |
07 | if ($("#txtname").val() != "") |
08 | qname = $("#txtname").val(); |
12 | qParams = { id: qid, name: qname }; |
13 | $('#grid').datagrid('options').queryParams = qParams; |
14 | $('#grid').datagrid('options').pageNumber = 1; |
15 | var p = $('#grid').datagrid('getPager'); |
17 | $(p).pagination({ pageNumber: 1 }); |
19 | $("#grid").datagrid('reload'); |
以上aspx頁面的Script就差不多完成了,接下來就要處理資料面的問題,我使用了 CRUDHandler.ashx 這隻泛型處理常式來處理Server端的動作,在ashx開始我會先判斷要處理哪種作業,並且執行哪個method。
01 | public void ProcessRequest (HttpContext context) { |
02 | if (context.Request["mode"] != null) |
04 | string mode = context.Request["mode"].ToString(); |
在查詢的部分我使用SQL資料分頁的方式取得資料並回傳Client端,DataGrid 預設會POST兩個參數回Server端。
在Server端必須接收這兩個參數後再去資料庫取得分頁資料,如果需要增加額外的參數就如同我上面的Script碼寫法增加,一個查詢的程式碼大概如下:
02 | public void QueryData(HttpContext context) |
05 | string page = context.Request["page"]; |
06 | string rows = context.Request["rows"]; |
07 | List<users> li = new List<users>(); |
08 | DataSet ds = GetData(int.Parse(rows), int.Parse(page), context); |
09 | foreach (DataRow dr in ds.Tables[0].Rows) |
13 | id = dr["id"].ToString(), |
14 | name = dr["name"].ToString(), |
15 | age = dr["age"].ToString(), |
16 | address = dr["address"].ToString() |
19 | ReturnDate rd = new ReturnDate(); |
20 | rd.total = ds.Tables[1].Rows[0]["TotalCount"].ToString(); |
22 | DataContractJsonSerializer json = new DataContractJsonSerializer(rd.GetType()); |
23 | json.WriteObject(context.Response.OutputStream, rd); |
26 | public class ReturnDate |
28 | public string total { get; set; } |
29 | public List<users> rows { get; set; } |
34 | public string id { get; set; } |
35 | public string name { get; set; } |
36 | public string age { get; set; } |
37 | public string address { get; set; } |
38 | }</users></users></users> |
這是我的寫法,將撈出來的資料轉成JSON後在傳到Client,而其他的處理方法就可以依照你自己需求而去撰寫,我這邊就不多撰寫了,這樣就完成了一個DataGrid,實際完成的畫面如下。


範例程式碼
这篇关于[ASP.NET] 於ASP.NET使用 jQuery EasyUi DataGrid 存取資料的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!