JQueryEasyUI datagrid框架的基本使用方法

今天說說這個datagrid框架的基本使用,這個框架一直以來都是大傢比較頭疼的框架,尤其是Json數據的拼接,後臺前臺都很重要,使用這個框架,最重要的是仔細:
無需廢話,上代碼瞭:

. 代碼如下:

<link href="jquery-easyui-1.3.2/themes/default/easyui.css" rel="stylesheet" type="text/css" />
<!–easyui最全的樣式包也可單獨引用你想使用的樣式包–>
<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的中文語言包,默認是英文–>
</head>
<body id="layoutbody" class="easyui-layout">
<p data-options="region:'north',title:'North Title',split:true" style="height: 100px;">
</p>
<p data-options="region:'south',title:'South Title',split:true" style="height: 100px;">
</p>
<p data-options="region:'east',iconCls:'icon-reload',title:'East',split:true" style="width: 100px;">
</p>
<p data-options="region:'west',title:'West',split:true" style="width: 100px;">
</p>
<p data-options="region:'center',title:'center title'" href="HTMLPage.htm" style="padding: 5px;
background: #eee; overflow: hidden;"><!–這裡指向瞭一個htm頁–>
</p>
</body>
</html>

HTMLPage.htm代碼:

. 代碼如下:

<script type="text/javascript" charst="utf-8">
//因為layout框架指向href時,隻取html頁面body中間的部分,所以該頁面這樣寫即可
//有datagrid包含屬性較多,所以盡量使用js的方式初始化datagrid框架
$(function () {
$("#dg").datagrid({
url: "GetJson.ashx", //指向一個一般處理程序或者一個控制器,返回數據要求是Json格式,直接賦值Json格式數據也可,我以demo中自帶的Json數據為例,就不寫後臺代碼瞭,但是我會說下後臺返回的註意事項
title: "數據展示表格",
iconCls: "icon-add",
fitColumns: false, //設置為true將自動使列適應表格寬度以防止出現水平滾動,false則自動匹配大小
//toolbar設置表格頂部的工具欄,以數組形式設置
idField: 'id', //標識列,一般設為id,可能會區分大小寫,大傢註意一下
loadMsg: "正在努力為您加載數據", //加載數據時向用戶展示的語句
pagination: true, //顯示最下端的分頁工具欄
rownumbers: true, //顯示行數 1,2,3,4…
pageSize: 10, //讀取分頁條數,即向後臺讀取數據時傳過去的值
pageList: [10, 20, 30], //可以調整每頁顯示的數據,即調整pageSize每次向後臺請求數據時的數據
//由於datagrid的屬性過多,我就不每個都介紹瞭,如有需要,可以看它的API
sortName: "name", //初始化表格時依據的排序 字段 必須和數據庫中的字段名稱相同
sortOrder: "asc", //正序
columns: [[
{ field: 'code', title: 'Code', width: 100 },
{ field: 'name', title: 'Name', width: 100 ,sortable:true},//sortable:true點擊該列的時候可以改變升降序
{ field: 'addr', title: 'addr', width: 100,
//這裡可以添加這樣一個方法,使其顯示數據得到改變
// formatter: function (value, row, index) {
// if (value == "0") {
// return "普通角色";
// } else {
// return "特殊角色";
// }
// }
}
]]//這裡之所以有兩個方括號,是因為可以做成水晶報表形式,具體可看demo
});
});
</script>
<p id="tt" class="easyui-tabs" style="width: 500px; height: 250px;" fit="true"
border="false">
<p title="Tab1" style="padding: 20px;" border="false">
<table id="dg">
</table>
</p>
</p>

這是前臺請求數據時發送的數據;

Json格式數據一定要是雙引號的,單引號無法顯示數據哦;

數據格式如下:

. 代碼如下:

{
"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"}
]
}

這裡呢,後臺傳遞數據很重要:

 

註意:表格Post或者get回來的請求中
page:3 代表page為key,然後選擇的當前頁碼為3
rows:10 代表一頁的大小為10
後臺返回的數據的格式為:{total:'',rows:[{},{}]}
隻要包含瞭總數tatol字段,rows是具體的行數
例如:
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中有一個類也可以序列化Json格式數據;

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *