JQuery EasyUI的datagrid的使用方式總結


JQuery EasyUI的datagrid的使用方式總結
第一步:添加樣式和js腳本在前台添加展示數據表格的table元素
例如:
<div>
<table id="tt" style="width: 700px;" title="標題,可以使用代碼進行初始化,也可以使用這種屬性的方式" iconcls="icon-edit">
</table>
</div>
注:表格的屬性可以在table中設置(Unobtrusive),也可以直接使用js腳本進行控制。建議使用js腳本控制


屬性的定義:
請參見Jquery easyui API

第二步:在doucment.ready中初始化表格的屬性以及數據獲取的方式。
例如:
$('#tt').datagrid({
url: '/UserInfo/GetAllUserInfos',
title: '演示表格使用',
width: 700,
height: 400,
fitColumns: true,
idField: 'ID',
loadMsg: '正在加載用戶的信息...',
pagination: true,
singleSelect: false,
pageSize:10,
pageNumber:1,
pageList: [10, 20, 30],
queryParams: {},
columns: [[
{ field: 'ck', checkbox: true, align: 'left', width: 50 },
{ field: 'ID', title: '主鍵', width: 80 },
{ field: 'UserName', title: '用戶名', width: 120 },
{ field: 'SubTime', title: '提交時間', width: 80, align: 'right',
formatter:function(value,row,index){
return (eval(value.replace(/\/Date\((\d+)\)\//gi, "new Date($1)"))).pattern("yyyy-M-d h:m:s.S");
}
},
{field:'showprice',title:'商品價格',width:80,align:'right',
styler:function(value,row,index){
if (value < 20){
return 'background-color:#ffee00;color:red;';
}
},
formatter:function(value,row,index){
return "<a href='#' onclick='editGoodsPrice("+row.goodsid+");return false;'>"+value+"</a>";
}
}
]],
toolbar: [{
id: 'btnDownShelf',
text: '上架',
iconCls: 'icon-add',
handler: function () {
var rows = $('#goodGrid').datagrid('getSelections');
if (!rows || rows.length == 0) {
//alert("請選擇要修改的商品!");
$.messager.alert("選擇商品提醒", "請選擇要修改的商品!", "error");
return;
}
$.messager.confirm("上架提醒", "您是否要真的要將此商品上架?", function (r) {
if (r) {
updateGoodsNewHot(rows, "onshelf");
}
});
}
}],
onHeaderContextMenu: function (e, field) {

}
});


第三步:后台設置加載的數據:

注意:表格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);
}

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM