Jquery分页功能

Posted 茶树

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Jquery分页功能相关的知识,希望对你有一定的参考价值。

Jquery代码

/// <reference path="jquery-1.9.1-vsdoc.js" />
//锚点
var anchor="#apage";
$(function(){
    pagerFun();
    $("#btnSearch").click(function(){
        var name=$.trim($("#txtHospitalName").val());
        var provinceId=$("#selProvince").val();
        var rateLevel= $("#selRateLevel").val();
        var pageUrl=location.href.replace(location.search,"").replace(anchor,"");
        var newURL=pageUrl+"?name="+encodeURIComponent(name)+"&pid="+provinceId+"&level="+rateLevel+anchor;
        location.href=newURL;
        //location.replace(newURL)
    });
//    var Request = new Object();
//    Request = $.getRequest();
//    if(Request["name"]){
//        var name=Request["name"];
//        $("#txtHospitalName").val(decodeURIComponent(name));
//    }
});
//分页方法
function pagerFun(){
    var pageIndex=$("#hidPageIndex").val();
    var pageCount=$("#hidPageCount").val();
    var pageUrl=location.href.replace(location.search,"").replace(anchor,"");
    var urlSearch=location.search;
    if(urlSearch.indexOf("?") != -1) {
        urlSearch=urlSearch.substr(1);
        var paraArr=urlSearch.split("&");
        if(paraArr[0].indexOf("p=")!=-1){
            urlSearch=urlSearch.replace(paraArr[0],"");
        }
        if(urlSearch!=""&urlSearch.indexOf("&")!=0){
            urlSearch="&"+urlSearch;
        }
    }
    var strPager="";
    var showPager=5;
    var startIndex=1;
    var endIndex=pageCount;
    if(pageCount==2){
        if(pageIndex==1){
            strPager="<a class=\"curr\" href=\""+pageUrl+"?p=1"+urlSearch+anchor+"\">1</a>";
            strPager=strPager+"<a href=\""+pageUrl+"?p=2"+urlSearch+anchor+"\">2</a>";
        }
        else{
            strPager="<a href=\""+pageUrl+"?p=1"+urlSearch+anchor+"\">1</a>";
            strPager=strPager+"<a class=\"curr\" href=\""+pageUrl+"?p=2"+urlSearch+anchor+"\">2</a>";
        }
        
    }
    if(pageCount>2){
        var prevPage=pageIndex-1;
        if(prevPage<=0){
            strPager="<span>上一页</span>";
        }
        else{
            strPager="<a href=\""+pageUrl+"?p="+prevPage+urlSearch+anchor+"\">上一页</a>";
        }
        if(pageIndex>=5&&pageCount>5){
            startIndex=parseInt(pageIndex)-2;
            strPager=strPager+"<a href=\""+pageUrl+"?p=1"+urlSearch+anchor+"\">1</a>";
            strPager=strPager+"<span class=\"defspan\">...</span>";
            endIndex=parseInt(pageIndex)+2;
            if(endIndex>=pageCount){
                endIndex=pageCount;
                startIndex=parseInt(pageCount)-4;
            }
        }
        else{
            if(pageCount>showPager){
                if(pageIndex<showPager){
                    endIndex=showPager;
                }
                else{
                    endIndex=parseInt(pageIndex) +parseInt(showPager)-2;
                }
            }
        }
        for(var i=startIndex;i<=endIndex;i++)
        {
            if(i==pageIndex){
                strPager=strPager+"<a class=\"curr\" href=\""+pageUrl+"?p="+i+urlSearch+anchor+"\">"+i+"</a>";
                continue;
            }
            
            strPager=strPager+"<a href=\""+pageUrl+"?p="+i+urlSearch+anchor+"\">"+i+"</a>";
        }
        if(endIndex<pageCount){
            if(endIndex!=parseInt(pageCount)-1){
                strPager=strPager+"<span class=\"defspan\">...</span>";
            }
            strPager=strPager+"<a href=\""+pageUrl+"?p="+pageCount+urlSearch+anchor+"\">"+pageCount+"</a>";
        }
        var nextPage=parseInt(pageIndex)+1;
        if(nextPage>pageCount){
            strPager=strPager+"<span>下一页</span>";
        }
        else{
            strPager=strPager+"<a href=\""+pageUrl+"?p="+nextPage+urlSearch+anchor+"\">下一页</a>";
        }
    }
    $("#divPager").html(strPager);
}

 


 

前端代码

 <input type="hidden" id="hidPageIndex" value="1" runat="server" />
            <input type="hidden" id="hidPageCount" value="1" runat="server" />
            <div class="pager" style="height: 30px;">
                <div id="divPager" style="float: right; margin-right: 40px;">
                </div>
            </div> 

 


 

后台代码

  /// <summary>
        /// 数据加载
        /// </summary>
        /// <param name="pageIndex">页码</param>
        /// <param name="provinceId">省份id</param>
        /// <param name="name">医院名字</param>
        /// <param name="level">星级</param>
        private void DataLoad(int pageIndex, int provinceId, string name, string level)
        {

            phDataContext ph = new phDataContext();
            //IQueryable<PetOrg> petOrgList = ph.PetOrg.Where(p => p.State == 1 && (p.ProvinceId == provinceId || provinceId == 0));
            IQueryable<W_Petorg_PMP_PetOrgMoreInformation> petOrgList = ph.W_Petorg_PMP_PetOrgMoreInformation.Where(s => s.OrgState == 1);
            if (!name.IsNullOrEmptyOrWhiteSpace())
            {
                name = Uri.UnescapeDataString(name);
                txtHospitalName.Value = name;

                petOrgList = petOrgList.Where(p => p.OrgName.Contains(name));
            }
           if (!provinceId.ToString().IsNullOrEmptyOrWhiteSpace()&&provinceId!=0)
           {
               petOrgList = petOrgList.Where(a => a.ProvinceId == provinceId);
           }
            int count = petOrgList.Count();
            int pageCount = OperatHelper.GetUpInt(count, pageSize);
            if (pageIndex > pageCount)
            {
                pageIndex = pageCount;
            }
            if (pageIndex <= 0)
            {
                pageIndex = 1;
            }
            if (count == 0)
            {
                //noHospital.InnerText = "无记录!";
            }
            else
            {
                rep_bind.DataSource = petOrgList.OrderByDescending(p => p.CreateDate).Skip((pageIndex - 1) * pageSize).Take(pageSize);
                rep_bind.DataBind();
            }
            hidPageCount.Value = pageCount.ToString();
            hidPageIndex.Value = pageIndex.ToString();  
        }
        

 


 

以上是关于Jquery分页功能的主要内容,如果未能解决你的问题,请参考以下文章

jquery 分页功能

基于jQuery的分页功能实现

项目实操:jQuery伪分页

jquery实现分页功能

jQuery+AJAX实现纯js分页功能

jQuery WeUI实现分页功能