JQueryEasyUI datagrid框架的基本使用
今天說說這個(gè)datagrid框架的基本使用,這個(gè)框架一直以來都是大家比較頭疼的框架,尤其是Json數(shù)據(jù)的拼接,后臺(tái)前臺(tái)都很重要,使用這個(gè)框架,最重要的是仔細(xì):
無需廢話,上代碼了:
<link href="jquery-easyui-1.3.2/themes/default/easyui.css" rel="stylesheet" type="text/css" />
<!--easyui最全的樣式包也可單獨(dú)引用你想使用的樣式包-->
<link href="jquery-easyui-1.3.2/themes/icon.css" rel="stylesheet" type="text/css" />
<!--easyui自帶圖片樣式包,也可自行添加-->
<script src="jquery-easyui-1.3.2/jquery-1.8.0.min.js" type="text/javascript"></script>
<!--我使用的是easyui 1.3.2,基于jquery-1.8.0-->
<script src="jquery-easyui-1.3.2/jquery.easyui.min.js" type="text/javascript"></script>
<!--easyui的js包-->
<script src="jquery-easyui-1.3.2/locale/easyui-lang-zh_CN.js" type="text/javascript"></script>
<!--easyui的中文語言包,默認(rèn)是英文-->
</head>
<body id="layoutbody" class="easyui-layout">
<div data-options="region:'north',title:'North Title',split:true" style="height: 100px;">
</div>
<div data-options="region:'south',title:'South Title',split:true" style="height: 100px;">
</div>
<div data-options="region:'east',iconCls:'icon-reload',title:'East',split:true" style="width: 100px;">
</div>
<div data-options="region:'west',title:'West',split:true" style="width: 100px;">
</div>
<div data-options="region:'center',title:'center title'" href="HTMLPage.htm" style="padding: 5px;
background: #eee; overflow: hidden;"><!--這里指向了一個(gè)htm頁-->
</div>
</body>
</html>
HTMLPage.htm代碼:
<script type="text/javascript" charst="utf-8">
//因?yàn)閘ayout框架指向href時(shí),只取html頁面body中間的部分,所以該頁面這樣寫即可
//有datagrid包含屬性較多,所以盡量使用js的方式初始化datagrid框架
$(function () {
$("#dg").datagrid({
url: "GetJson.ashx", //指向一個(gè)一般處理程序或者一個(gè)控制器,返回?cái)?shù)據(jù)要求是Json格式,直接賦值Json格式數(shù)據(jù)也可,我以demo中自帶的Json數(shù)據(jù)為例,就不寫后臺(tái)代碼了,但是我會(huì)說下后臺(tái)返回的注意事項(xiàng)
title: "數(shù)據(jù)展示表格",
iconCls: "icon-add",
fitColumns: false, //設(shè)置為true將自動(dòng)使列適應(yīng)表格寬度以防止出現(xiàn)水平滾動(dòng),false則自動(dòng)匹配大小
//toolbar設(shè)置表格頂部的工具欄,以數(shù)組形式設(shè)置
idField: 'id', //標(biāo)識(shí)列,一般設(shè)為id,可能會(huì)區(qū)分大小寫,大家注意一下
loadMsg: "正在努力為您加載數(shù)據(jù)", //加載數(shù)據(jù)時(shí)向用戶展示的語句
pagination: true, //顯示最下端的分頁工具欄
rownumbers: true, //顯示行數(shù) 1,2,3,4...
pageSize: 10, //讀取分頁條數(shù),即向后臺(tái)讀取數(shù)據(jù)時(shí)傳過去的值
pageList: [10, 20, 30], //可以調(diào)整每頁顯示的數(shù)據(jù),即調(diào)整pageSize每次向后臺(tái)請(qǐng)求數(shù)據(jù)時(shí)的數(shù)據(jù)
//由于datagrid的屬性過多,我就不每個(gè)都介紹了,如有需要,可以看它的API
sortName: "name", //初始化表格時(shí)依據(jù)的排序 字段 必須和數(shù)據(jù)庫(kù)中的字段名稱相同
sortOrder: "asc", //正序
columns: [[
{ field: 'code', title: 'Code', width: 100 },
{ field: 'name', title: 'Name', width: 100 ,sortable:true},//sortable:true點(diǎn)擊該列的時(shí)候可以改變升降序
{ field: 'addr', title: 'addr', width: 100,
//這里可以添加這樣一個(gè)方法,使其顯示數(shù)據(jù)得到改變
// formatter: function (value, row, index) {
// if (value == "0") {
// return "普通角色";
// } else {
// return "特殊角色";
// }
// }
}
]]//這里之所以有兩個(gè)方括號(hào),是因?yàn)榭梢宰龀伤?bào)表形式,具體可看demo
});
});
</script>
<div id="tt" class="easyui-tabs" style="width: 500px; height: 250px;" fit="true"
border="false">
<div title="Tab1" style="padding: 20px;" border="false">
<table id="dg">
</table>
</div>
</div>

Json格式數(shù)據(jù)一定要是雙引號(hào)的,單引號(hào)無法顯示數(shù)據(jù)哦;
數(shù)據(jù)格式如下:
{
"total":239,
"rows":[
{"code":"001","name":"Name 1","addr":"Address 11","col4":"col4 data"},
{"code":"002","name":"Name 2","addr":"Address 13","col4":"col4 data"},
{"code":"003","name":"Name 3","addr":"Address 87","col4":"col4 data"},
{"code":"004","name":"Name 4","addr":"Address 63","col4":"col4 data"},
{"code":"005","name":"Name 5","addr":"Address 45","col4":"col4 data"},
{"code":"006","name":"Name 6","addr":"Address 16","col4":"col4 data"},
{"code":"007","name":"Name 7","addr":"Address 27","col4":"col4 data"},
{"code":"008","name":"Name 8","addr":"Address 81","col4":"col4 data"},
{"code":"009","name":"Name 9","addr":"Address 69","col4":"col4 data"},
{"code":"010","name":"Name 10","addr":"Address 78","col4":"col4 data"}
]
}
這里呢,后臺(tái)傳遞數(shù)據(jù)很重要:
注意:表格Post或者get回來的請(qǐng)求中
page:3 代表page為key,然后選擇的當(dāng)前頁碼為3
rows:10 代表一頁的大小為10
后臺(tái)返回的數(shù)據(jù)的格式為:{total:'',rows:[{},{}]}
只要包含了總數(shù)tatol字段,rows是具體的行數(shù)
例如:
Asp.Net MVC 例子:
public JsonResult GetAllUserInfos()
{
int pageSize = 5;
int pageIndex = 1;
int.TryParse(this.Request["page"], out pageIndex);
int.TryParse(this.Request["rows"], out pageSize);
pageSize = pageSize <= 0 ? 5 : pageSize;
pageIndex = pageIndex < 1 ? 1 : pageIndex;
var temp = db.UserInfo
.OrderBy(u=>u.Sort)
.Skip<UserInfo>((pageIndex-1)*pageSize)
.Take<UserInfo>(pageSize)
.ToList<UserInfo>();
Hashtable ht = new Hashtable();
ht["total"] = db.UserInfo.Count();
ht["rows"] = temp;
return Json(ht);
}
Asp.Net WebForm 例子:
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
var strWebName = context.Request["WebName"] ?? string.Empty;
var GoodsNo = context.Request["GoodsNo"] ?? string.Empty;
int categoryId = 0;
int pageIndex = 1;
int pageSize = 10;
int.TryParse(context.Request["rows"], out pageSize);
int.TryParse(context.Request["page"], out pageIndex);
decimal priceLeft = 0;
decimal priceRight = 1000000;
int goodsStatus = 0;
decimal.TryParse(context.Request["PriceLeft"], out priceLeft);
decimal.TryParse(context.Request["PriceRight"], out priceRight);
int.TryParse(context.Request["CategoryId"], out categoryId);
int.TryParse(context.Request["GoodsStatus"], out goodsStatus);
var goodsQueryParamter = new GoodsQueryParamter();
goodsQueryParamter.GoodsStatus = (Model.GoodsModel.GoodsStatusEnum)goodsStatus;
var ds = goodsService.GetGoodsList(goodsQueryParamter);
string json = string.Empty;
if (ds != null && ds.Tables.Count > 0)
{
System.Text.StringBuilder rowJson = new System.Text.StringBuilder();
int colLen = ds.Tables[0].Columns.Count;
DataColumnCollection col = ds.Tables[0].Columns;
foreach (DataRow row in ds.Tables[0].Rows)
{
System.Text.StringBuilder colJson = new System.Text.StringBuilder();
rowJson.Append("{");
for (int i = 0; i < colLen; i++)
{
colJson.Append("\"" + col[i].ColumnName + "\":\"" + row[i].ToString() + "\",");
}
rowJson.Append(colJson.ToString().TrimEnd(','));
rowJson.Append("},");
}
json = "{\"total\":" + ds.Tables[0].Rows[0]["sumGoods"] + ",\"rows\":[" + rowJson.ToString().TrimEnd(',') + "]}";
}
context.Response.Write(json);
}
ASP.Net中有一個(gè)類也可以序列化Json格式數(shù)據(jù);
相關(guān)文章
利用jquery和BootStrap實(shí)現(xiàn)動(dòng)態(tài)滾動(dòng)條效果
這篇文章主要介紹了利用jquery和BootStrap實(shí)現(xiàn)動(dòng)態(tài)滾動(dòng)條效果,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-12-12
jQuery實(shí)現(xiàn)動(dòng)態(tài)刪除LI的方法
這篇文章主要介紹了jQuery實(shí)現(xiàn)動(dòng)態(tài)刪除LI的方法,結(jié)合實(shí)例形式分析了jQuery針對(duì)頁面元素動(dòng)態(tài)操作相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2017-05-05
jQuery使用一個(gè)按鈕控制圖片的伸縮實(shí)現(xiàn)思路
上一篇講解了jquery同時(shí)控制四張圖片的伸縮,想必大家對(duì)伸縮有一定了解了,本文將為大家介紹下使用按鈕控制圖片的伸縮,感興趣的朋友可以參考下哈2013-04-04
jQuery簡(jiǎn)單入門示例之用戶校驗(yàn)demo示例
這篇文章主要介紹了jQuery簡(jiǎn)單入門示例之用戶校驗(yàn)demo示例,分析了基于jQuery的簡(jiǎn)單用戶校驗(yàn)功能相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2016-07-07

