asp.net 中使用EasyUI Datagrid 載入動態資料分頁查詢
剛用了datagrid外掛,記錄備忘。
datagrid官網文件地址:http://www.jeasyui.net/plugins/183.html
效果截圖:
功能實現的核心程式碼如下:
前臺程式碼:
前臺搜尋按鈕:
<li><span class="xs-search"><i class="iconfont icon-sousuo"></i><span><input type="text" placeholder="搜尋內容" id="SearchText"/></span>
<input type="button" value="搜尋" onclick="Search()"/>
</span> <a href="#">高階</a>
</li>
datagrid繫結到 class=“index-grid”的div:
<div class="index-right">
<div class="index-grid" style="height:100%"></div></div>
datagrid的實現,查詢,分頁功能的實現:
function Search() {
var _SearchText = $("#SearchText").val();
//用了 queryParams傳遞引數,這兒將替換queryParams的引數
$(".index-grid").datagrid('load', {
Action: 'Search',
SearchText: _SearchText
});
}
$(function () {
$(".index-grid").datagrid({
collapsible: true,
pagination: true,//分頁
pageList: [10, 20, 50, 100],
method: 'get',//請求方式
url: 'index.aspx',//資料請求地址
queryParams: { Action: 'List', SearchText: '' },//傳遞的引數
singleSelect: true,
columns: [
[{
field: 'PartsCode',//每個對應後臺json的鍵
title: "配件編碼",
align: 'center',
width: 90
}, {
field: 'PartsName',
title: "配件名稱",
align: 'center',
width: 90
}, {
field: 'brand',
title: "品牌",
align: 'center',
width: 90
}, {
field: 'UsedType',
title: "適用車型",
align: 'center',
width: 90
}, {
field: 'OutQuantity',
title: "出庫數量",
align: 'center',
width: 90
}, {
field: 'Unit',
title: "單位",
align: 'center',
width: 90
}, {
field: 'StockPrice',
title: "預設銷售價",
align: 'center',
width: 90
}, {
field: 'StockTaxPrice',
title: "含稅成本價",
align: 'center',
width: 90
}, {
field: 'StockTaxCost',
title: "含稅成本金額",
align: 'center',
width: 90
}, {
field: 'RepositoryNamefrom',
title: "倉庫",
align: 'center',
width: 90
}, {
field: 'RepositoryNamefrom2',
title: "倉位",
align: 'center',
width: 90
}, {
field: 'AddTime',
title: "日期",
align: 'center',
width: 120
}]
]
});
});
</script>
後臺核心程式碼:
public partial class list
{
public string ClassId = DTRequest.GetQueryString("ClassId");
public int _RecordCount = 0;
public int _PageCount = 0;
public int pageSize = 10;
public Hashtable datajson = new Hashtable();
BLL.PartOut R = new BLL.PartOut();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
try
{
int page = int.Parse(Request.QueryString["page"] ?? "1");
string _Action = Request.QueryString["Action"] ?? "";
string _SearchText = Request.QueryString["SearchText"] ?? "";
pageSize = int.Parse(Request.QueryString["rows"] ?? "10");
if (_Action == "Search")
{
StartLoad(1, "PartsName like '%" + _SearchText + "%'");
Response.Write(Regex.Unescape(LitJson.JsonMapper.ToJson(datajson)));
Response.End();
}
else if (_Action == "List")
{
StartLoad(page, "1=1");
Response.Write(LitJson.JsonMapper.ToJson(datajson));
Response.End();
}
else
{
StartLoad(page, "1=1");
}
if (page > 1)
{
Response.Write(LitJson.JsonMapper.ToJson(datajson));
Response.End();
}
}
catch
{
JscriptMsg("頻道引數不正確!", "back", "Error");
}
}
}
private void StartLoad(int _pageindex, string where)
{
DataTable dts = R.GetPage(where, "StockId", "order by StockId asc", _pageindex, pageSize, out _RecordCount, out _PageCount, null);
List<Hashtable> List = new List<Hashtable>();
datajson.Add("total", _RecordCount);
if (dts != null && dts.Rows.Count != 0)
{
for (int i = 0; i < dts.Rows.Count; i++)
{
Hashtable ht = new Hashtable();
ht.Add("PartsCode", dts.Rows[i]["PartsCode"].ToString());
ht.Add("PartsName", dts.Rows[i]["PartsName"].ToString());
ht.Add("brand", dts.Rows[i]["brand"].ToString());
ht.Add("UsedType", dts.Rows[i]["UsedType"].ToString());
ht.Add("OutQuantity", dts.Rows[i]["OutQuantity"].ToString());
ht.Add("Unit", dts.Rows[i]["Unit"].ToString());
ht.Add("StockPrice", dts.Rows[i]["StockPrice"].ToString());
ht.Add("StockTaxPrice", dts.Rows[i]["StockTaxPrice"].ToString());
ht.Add("StockTaxCost", dts.Rows[i]["StockTaxCost"].ToString());
ht.Add("RepositoryNamefrom", dts.Rows[i]["RepositoryNamefrom"].ToString());
ht.Add("RepositoryNamefrom2", dts.Rows[i]["RepositoryNamefrom2"].ToString());
ht.Add("AddTime", dts.Rows[i]["AddTime"].ToString());
List.Add(ht);
}
}
datajson.Add("rows", List);
}
}