佳木斯湛栽影视文化发展公司

主頁 > 知識庫 > 基于AJAX的分頁類實現(xiàn)代碼

基于AJAX的分頁類實現(xiàn)代碼

熱門標(biāo)簽:電子圍欄 銀行業(yè)務(wù) Linux服務(wù)器 阿里云 服務(wù)器配置 Mysql連接數(shù)設(shè)置 科大訊飛語音識別系統(tǒng) 團購網(wǎng)站
復(fù)制代碼 代碼如下:

/**
* p>pagination.js
* p>通用的基于AJAX的分頁類
* @author jeanwendy
* @version 1.0
*/
var paginationIndex = 0;
var pagination = function(trTemplatId) {
    $().ajaxStart(function() {
        $.blockUI({
            message : 'table>tr>td style="vertical-align :bottom">font size=2pt>nbsp;加載數(shù)據(jù),請稍后.../font>/td>/tr>/table>'
        });
    }).ajaxStop($.unblockUI);

    paginationIndex = paginationIndex + 1;
    this.id = paginationIndex;
    this.trTemplatId = trTemplatId;
    this.pageNo = 1;
    this.pageSize = 10;
    this.beforeQuery = null;
    this.afterQuery = null;
    this.url = null;
    this.params = null;
    this.templat = null;
    this.childrenCount = null;

    this.setPageNo = function(pageNo) {
        if (pageNo != null)
            this.pageNo = pageNo;
    }
    this.setPageSize = function(pageSize) {
        if (pageSize != null)
            this.pageSize = pageSize;
    }
    this.setBeforeQuery = function(fn){
        this.beforeQuery = fn;
    }
    this.setAfterQuery = function(fn){
        this.afterQuery = fn;
    }

    this.load = function(url, params) {
        //初始化(只在第一次查詢時執(zhí)行)
        if(this.templat == null this.childrenCount == null){
            var templatObj = $('#'+this.trTemplatId);
            templatObj.parent().attr('id','tbody_id'+this.id);
            templatObj.removeAttr('id');
            templatObj.wrap("div id='divTemplat'>/div>");
            this.templat = $('#divTemplat').html();
            $('#divTemplat').remove();
            this.childrenCount = $('#tbody_id'+this.id).children().size();
        }
        //開始查詢
        this.url = url;
        if(params == null) params = {};
        $.extend(params,{pageNo:this.pageNo,pageSize:this.pageSize});
        this.params = params;
        var thisObj = this;
        var options = {
            url : url,
            data : params,
            async : false, //采用同步方式請求
            type : 'POST',
            dataType : 'json',
            error : function(xmlhttp, errInfo, e) { //請求出錯處理:如:404等
                if (xmlhttp.status == 200) alert('您已經(jīng)很長時間沒有訪問網(wǎng)站,請退出后重新登陸!');
                else alert('請求后臺服務(wù)時發(fā)生錯誤:' + xmlhttp.status);
            },
            success : function(data){
                //刪除上一次的數(shù)據(jù)
                $('#tbody_id'+thisObj.id).children().filter(':gt('+(thisObj.childrenCount-1)+')').remove();
                thisObj.pageList(data.data);
                thisObj.pageBar(data.total);
                if($.isFunction(thisObj.afterQuery)) thisObj.afterQuery();
            }
        };
        if($.isFunction(this.beforeQuery)) this.beforeQuery();
        $.ajax(options); //發(fā)送請求
    }

    this.pageList = function(data){
        var filedArr = this.templat.match(/\{[A-Za-z0-9_]+\}/ig);
        for(var i = 0;i data.length;i++){
            var thisTemplat = this.templat;
            for(var j = 0;j filedArr.length;j++){
                var key = filedArr[j].substring(1,filedArr[j].length-1);
                if(key == 'NO_'){ //序號標(biāo)識
                    var value = (this.pageNo-1)*this.pageSize + i + 1;
                    thisTemplat = thisTemplat.replace(new RegExp('\{'+key+'\}','gm'),value);
                }else{
                    var value = data[i][key];
                    if(typeof(value) != "undefined" value == null) value = '';
                    thisTemplat = thisTemplat.replace(new RegExp('\{'+key+'\}','gm'),value);
                }
            }
            $(thisTemplat).appendTo($('#tbody_id'+this.id));
        }
    }

    this.pageBar = function(total){
        var templatObj = $(this.templat);
        var delChildren = templatObj.children(':gt(0)');
        delChildren.remove();
        templatObj.children().attr('colspan',$(this.templat).children().size());
        templatObj.children().attr('align','right');
        var pageCount;
        if(total % this.pageSize == 0) pageCount = total/this.pageSize;
        else pageCount = parseInt(total/this.pageSize) + 1;
        if(pageCount == 0) pageCount = 1;
        var toolbar = "第"+this.pageNo+"/"+pageCount+"頁("+total+"條記錄)";
        if(this.pageNo == 1) toolbar = toolbar + "nbsp;首頁nbsp;上頁";
        else toolbar = toolbar + "nbsp;a href='' id='firstPage"+this.id+"'>首頁/a>nbsp;a href='' id='prePage"+this.id+"'>上頁/a>";
        if(this.pageNo == pageCount) toolbar = toolbar + "nbsp;下頁nbsp;末頁";
        else toolbar = toolbar + "nbsp;a href='' id='nextPage"+this.id+"'>下頁/a>nbsp;a href='' id='lastPage"+this.id+"'>末頁/a>";
        toolbar = toolbar + "nbsp;每頁input style='text-align:center;width:25px;height:20px;border:1 solid black' type='text' id='pageSize"+this.id+"' value="+this.pageSize+" />條";
        toolbar = toolbar + "nbsp;input style='text-align:center;width:25px;height:20px;border:1 solid black' type='text' id='pageNo"+this.id+"' value="+this.pageNo+" />";
        toolbar = toolbar + "nbsp;input style='height:20px;border:1 solid black' id='goPage"+this.id+"' type='button' value='GO'>";
        templatObj.children().html(toolbar);
        $(templatObj.wrap("div>/div>").parent().html()).appendTo($('#tbody_id'+this.id));
        var thisObj = this;
        $('#firstPage'+thisObj.id).click(function(){
            thisObj.pageNo = 1;
            thisObj.load(thisObj.url,thisObj.params);
            return false;
        });
        $('#prePage'+thisObj.id).click(function(){
            thisObj.pageNo = parseInt(thisObj.pageNo) - 1;
            thisObj.load(thisObj.url,thisObj.params);
            return false;
        });
        $('#nextPage'+thisObj.id).click(function(){
            thisObj.pageNo = parseInt(thisObj.pageNo) + 1;
            thisObj.load(thisObj.url,thisObj.params);
            return false;
        });
        $('#lastPage'+thisObj.id).click(function(){
            thisObj.pageNo = pageCount;
            thisObj.load(thisObj.url,thisObj.params);
            return false;
        });
        $('#pageSize'+thisObj.id).keydown(function(e){
            if(e.keyCode==13) {
                var v = $('#pageSize'+thisObj.id).val();
                if(!isIntGreatZero(v) || v == '0'){
                    alert('您輸入顯示條數(shù)不合法,請重新輸入!');
                    $("#pageSize"+thisObj.id).focus();
                    return;
                }
                if(v > 200){
                    alert('您輸入顯示條數(shù)過大了,請重新輸入!');
                    $("#pageSize"+thisObj.id).focus();
                    return;
                }
                thisObj.pageNo = 1;
                thisObj.pageSize = v;
                thisObj.load(thisObj.url,thisObj.params);
            }
        });
        $('#pageNo'+thisObj.id).keydown(function(e){
            if(e.keyCode==13) {
                $('#goPage'+thisObj.id).triggerHandler('click');
            }
        });
        $('#goPage'+thisObj.id).click(function(){
         var v = $('#pageNo'+thisObj.id).val();
            if(!isIntGreatZero(v) || v == '0'){
                alert('您輸入頁數(shù)不合法,請重新輸入!');
                $("#pageNo"+thisObj.id).focus();
                return;
            }
         if(v > pageCount){
                alert('您輸入頁數(shù)大于總頁數(shù),請重新輸入!');
                $("#pageNo"+thisObj.id).focus();
                return;
            }
            thisObj.pageNo = v;
            thisObj.load(thisObj.url,thisObj.params);
        });
    }

}
//true if the string is empty
var isEmpty = function(text) {
    var isEmpty = true;
    for (var i = 0; i text.length; i++) {
        if (text.charAt(i) != ' ') {
            isEmpty = false;
            break;
        }
    }
    return isEmpty;
}
//true if the string is int and great than zero or equals zero
var isIntGreatZero = function(str) {
    if (isEmpty(str))
        return false;
    var temp1 = true;
    var temp2 = '0123456789';
    for (var i = 0; i str.length; i++) {
        var c = str.charAt(i);
        if (temp2.indexOf(c) == -1) {
            temp1 = false;
            break;
        } else {
            if (c == '0' i == 0 str.length > 1) {
                temp1 = false;
                break;
            }
        }
    }
    return temp1;
}
您可能感興趣的文章:
  • 用jQuery中的ajax分頁實現(xiàn)代碼
  • AJAX分頁的代碼(后臺asp.net)
  • ajax實現(xiàn)無刷新分頁(php)
  • 無框架 Ajax分頁(原創(chuàng))
  • ASP AJAX 靜態(tài)分頁
  • php ajax無刷新分頁,支持id定位
  • asp+ajax實現(xiàn)靜態(tài)頁面分頁的代碼
  • Ajax PHP分頁演示
  • AJAX和三層架構(gòu)實現(xiàn)分頁功能具體思路及代碼

標(biāo)簽:衡水 衢州 棗莊 蚌埠 大理 萍鄉(xiāng) 江蘇 廣元

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《基于AJAX的分頁類實現(xiàn)代碼》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266
    涿鹿县| 昌邑市| 浪卡子县| 襄汾县| 宽城| 循化| 改则县| 金川县| 邵阳县| 安吉县| 莎车县| 芜湖市| 海原县| 合肥市| 高台县| 乳源| 盐城市| 嘉鱼县| 延庆县| 涿州市| 棋牌| 绥化市| 康保县| 五台县| 酉阳| 边坝县| 临颍县| 襄樊市| 呼玛县| 新营市| 富民县| 莆田市| 山阴县| 措勤县| 玉山县| 宁夏| 澜沧| 潢川县| 宣汉县| 洛川县| 博客|