本datagrid分頁顯示資料方法在每一次分頁操作中隻從資料庫中擷取pageSize個資料(背景分頁)(-2144182054)
1》前台jquery代碼
//初始化代碼
(function () {
var dg =(‘#tableModule’);
var opts = dg.datagrid(‘options’);
var pager = dg.datagrid(‘getPager’);
pager.pagination({
onSelectPage: function (pageNum, pageSize) {
opts.pageNumber = pageNum;
opts.pageSize = pageSize;
pager.pagination(‘refresh’, {
pageNumber: pageNum,
pageSize: pageSize
});
Search(); //從資料庫中擷取資料,并加載
},
pageList: [10, 30, 50, 100], //可以設定每頁記錄條數的清單
beforePageText: ‘第’, //頁數文本框前顯示的漢字
afterPageText: ‘頁 共 {pages} 頁’,
displayMsg: ‘目前顯示 {from} - {to} 條記錄 共 {total} 條記錄’
});
})
//從資料庫中擷取資料,并加載
function Search() {
var page_Number = (‘#tableModule’).datagrid(‘options’).pageNumber; //pageNumber為datagrid的目前頁碼
var page_Size =(‘#tableModule’).datagrid(‘options’).pageSize; //pageSize為datagrid的每頁記錄條數
var name = $(‘#inputName’).val(); //得到查詢關鍵字
$.post('Ajax/GetModuleListHandler.ashx', { Name: name, PageNumber: page_Number, PageSize: page_Size }, function (data) {
var data = $.parseJSON(data);
if (data != undefined && data != null) {
if (data.rows != undefined && data.rows != null && data.rows.length > 0 && data.rows[0].TotalCount != undefined && data.rows[0].TotalCount!=null) {
data.total = Number(data.rows[0].TotalCount);
$('#tableModule').datagrid('loadData', data);
}
}
})
}
2》一般處理程式中的代碼
public class GetModuleListHandler : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = “text/plain”;
string searchModuleName = "", json = "[]",pageNumberStr,pageSizeStr; //searchModuleName為查詢條件
int pageNumber = 0, pageSize = 0;
searchModuleName = context.Request["Name"];
pageNumberStr = context.Request["PageNumber"];
pageSizeStr = context.Request["PageSize"];
if (int.TryParse(pageNumberStr, out pageNumber) && int.TryParse(pageSizeStr, out pageSize))
{
string strWhere = " ";
if (!string.IsNullOrWhiteSpace(searchModuleName)) //如果搜尋子產品名不空,則将該子產品及其子子產品顯示出來
{
strWhere = " LEFT(ModuleCode,(select top 1 LEN(ModuleCode) from SYS_Module where";
strWhere += string.Format(" Name='{0}'))=(select top 1 ModuleCode from SYS_Module where Name='{0}')", searchModuleName);
}
DataSet ds=new DataSet();
string cacheKey=new StringBuilder().Append("SYS_ModulePage").Append(pageNumber).ToString();
if (context.Cache[cacheKey] == null)
{
//從業務工廠得到子產品處理對象
IBLL_SYS_Module iBLL_Module = BLLFactory.Factory.Get_BLL_SYS_Module();
ds = iBLL_Module.GetListByPage(strWhere, pageSize, pageNumber);
if (ds != null && ds.Tables.Count > 0)
{
context.Cache[cacheKey] = ds;
}
}
else
ds = context.Cache[cacheKey] as DataSet;
json = JsonType.DataTableToJson(ds.Tables[0]);
}
if (string.IsNullOrWhiteSpace(json)) json = "{\"total\":0,\"rows\":[]}";
context.Response.Write(json);
}
public bool IsReusable
{
get
{
return false;
}
}
}
3》業務邏輯
///
/// 分頁擷取資料清單
///
public DataSet GetListByPage(string strWhere, int pageSize, int pageIndex)
{
int startIndex = 0, endIndex = 0;
if (pageSize > 0 && pageIndex > 0)
{
startIndex = (pageIndex - 1) * pageSize + 1;
endIndex = pageSize * pageIndex;
}
if (startIndex > 0 && endIndex > startIndex)
return GetListByPage2(strWhere, “ModuleCode”, startIndex, endIndex);
else
return null;
}
///
/// 分頁擷取資料清單
///
public DataSet GetListByPage2(string strWhere, string orderby, int startIndex, int endIndex)
{
StringBuilder strSql = new StringBuilder();
strSql.Append(“SELECT * FROM ( “);
strSql.Append(” SELECT ROW_NUMBER() OVER (“);
if (!string.IsNullOrEmpty(orderby.Trim()))
{
strSql.Append(“order by T.” + orderby);
}
else
{
strSql.Append(“order by T.ModuleID desc”);
}
strSql.Append(“)AS Row, T.*,(select count(1) from SYS_Module “);
if (!string.IsNullOrEmpty(strWhere.Trim()))
{
strSql.Append(” WHERE ” + strWhere);
strSql.Append(“) as TotalCount from SYS_Module T “);
strSql.Append(” WHERE ” + strWhere);
}
else
strSql.Append(“) as TotalCount from SYS_Module T “);
strSql.Append(” ) TT”);
strSql.AppendFormat(” WHERE TT.Row between {0} and {1}”, startIndex, endIndex);
return DbHelperSQL.Query(strSql.ToString());
}