天天看点

jquery easyui datagrid分页显示数据

本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());

}

继续阅读