笔记:自制jquery分页小插件

function fjPage() {
    this.pageBoxName = null;
    this.$pageBox = null;
    this.settings = {
        pageNo: 1, // 当前页
        pageTotal: 0, // 一共多少页
        allCount: 0, // 一共多少条记录
        currentChange: function (pagenumber) { }, // currentPage 改变时会触发
        //nextClick: function (pagenumber) { }, // 用户点击下一页按钮改变当前页后触发
        //prevClick: function (pagenumber) { } //  用户点击上一页按钮改变当前页后触发 
    };
}
fjPage.prototype.init = function (className, opt) {
    this.pageBoxName = className;
    this.$pageBox = $(className);
    if (opt) {
        $.extend(this.settings, opt);
    }

    if (this.settings.pageTotal > 1) {
        this.html();
        this.event();
        this.$pageBox.show()
    } else {
        this.$pageBox.hide()
    }

    
    
}

fjPage.prototype.html = function () {
   
    // debugger
    var pageTotal = this.settings.pageTotal // Math.ceil(this.settings.pageTotal);

    var str1 =
        '  共 ' + pageTotal + ' 页    ' + this.settings.allCount + ' 条记录,' +
        '当前为第  ' + this.settings.pageNo + ' 页   ' +
        '跳转到 页 ' +
        ''

    this.$pageBox.find('.fj-pageTip').html(str1);

    var str = '首页上页'
   
    if (this.settings.pageTotal >7 && this.settings.pageNo - 3 > 1) str += '...';

    var startNum = 1;
    var endNum = 7;

    if (pageTotal < 7) endNum = pageTotal;
    else {
        console.log('d')
        if (this.settings.pageNo + 3 > pageTotal) {
            endNum = pageTotal;
            startNum = endNum - 6;
        } else if (this.settings.pageNo - 3 > 0) {
            startNum = this.settings.pageNo - 3;
            endNum = startNum + 6;
        }
    }
    startNum = startNum < 1 ? 1 : startNum;
    for (var i = startNum; i <= endNum; i++) {
        str += '' + i + '';
    }
    if (this.settings.pageTotal > 7 && this.settings.pageNo + 4 < pageTotal) str += '...';
    str += '下页尾页'
    this.$pageBox.find('.fj-pageList').html(str);

    this.setPageStatus();
}
fjPage.prototype.event = function () {
    var params = { that: this }
    // 确定事件
    $(document).off('click', this.pageBoxName + ' .fj-page-enter', this.eventEnter)
    $(document).on('click', this.pageBoxName + ' .fj-page-enter', params, this.eventEnter)

    // 首页
    $(document).off('click', this.pageBoxName + ' .fj-skip-home', this.eventHome)
    $(document).on('click', this.pageBoxName + ' .fj-skip-home', params, this.eventHome)
    // 上页
    $(document).off('click', this.pageBoxName + ' .fj-skip-prev', this.eventPrev)
    $(document).on('click', this.pageBoxName + ' .fj-skip-prev', params, this.eventPrev)
    // ...
    $(document).off('click', this.pageBoxName + ' .fj-page-more1', this.eventMore1)
    $(document).on('click', this.pageBoxName + ' .fj-page-more1', params, this.eventMore1)
    // ...
    $(document).off('click', this.pageBoxName + ' .fj-page-more2', this.eventMore2)
    $(document).on('click', this.pageBoxName + ' .fj-page-more2', params, this.eventMore2)
    // page-i
    $(document).off('click', this.pageBoxName + ' .fj-skip-page-i', this.eventPageI)
    $(document).on('click', this.pageBoxName + ' .fj-skip-page-i', params, this.eventPageI)
    // 下一页
    $(document).off('click', this.pageBoxName + ' .fj-skip-next', this.eventNext)
    $(document).on('click', this.pageBoxName + ' .fj-skip-next', params, this.eventNext)
    // 尾页
    $(document).off('click', this.pageBoxName + ' .fj-skip-last', this.eventLast)
    $(document).on('click', this.pageBoxName + ' .fj-skip-last', params, this.eventLast)
}

fjPage.prototype.setPageStatus = function () {
    var that = this;
    if (that.settings.pageNo <= 1) {
        $(that.pageBoxName + ' .fj-skip-prev').css('cursor', 'no-drop');
        $(that.pageBoxName + ' .fj-skip-home').css('cursor', 'no-drop');
    }
    if (that.settings.pageNo >= that.settings.pageTotal) {
        $(that.pageBoxName + ' .fj-skip-next').css('cursor', 'no-drop');
        $(that.pageBoxName + ' .fj-skip-last').css('cursor', 'no-drop');
    }
}

fjPage.prototype.eventEnter = function (event) {
    var that = event.data.that;
    var index = Number(that.$pageBox.find('[name=CurrentPage]').val());
    if (that.settings.pageNo == index) { return }
    if (index && index > 0 && Number(index) <= that.settings.pageTotal) {
        that.settings.pageNo = index
        that.settings.currentChange(that.settings.pageNo);
    }
    
}
fjPage.prototype.eventHome = function (event) {
    var that = event.data.that;
    if (that.settings.pageNo <= 1) {
        return;
    }
    that.settings.pageNo = 1
    that.settings.currentChange(that.settings.pageNo);
    
}
fjPage.prototype.eventPrev = function (event) {
    var that = event.data.that;
    if (that.settings.pageNo <= 1) {
        return;
    }
    that.settings.pageNo = that.settings.pageNo - 1 > 0 ? that.settings.pageNo - 1 : 1
    that.settings.currentChange(that.settings.pageNo);
   
}
fjPage.prototype.eventMore1 = function (event) {
    var that = event.data.that;
    that.settings.pageNo = that.settings.pageNo - 1;
    that.settings.currentChange(that.settings.pageNo);
   
}
fjPage.prototype.eventMore2 = function (event) {
    var that = event.data.that;
    that.settings.pageNo = that.settings.pageNo + 4
    that.settings.currentChange(that.settings.pageNo);
    
}
fjPage.prototype.eventPageI = function (event) {
    var that = event.data.that;
    var index = Number($(this).attr('data-i'));
    if (index != that.settings.pageNo) {
        that.settings.pageNo = Number(index)
        that.settings.currentChange(that.settings.pageNo);
    }
     
   
}
fjPage.prototype.eventNext = function (event) {
    var that = event.data.that;
    if (that.settings.pageNo >= that.settings.pageTotal) {
        return;
    }
    that.settings.pageNo = that.settings.pageNo + 1 < that.settings.pageTotal ? that.settings.pageNo + 1 : that.settings.pageTotal;
    that.settings.currentChange(that.settings.pageNo);
   
}
fjPage.prototype.eventLast = function (event) {
    var that = event.data.that;
    if (that.settings.pageNo >= that.settings.pageTotal) {
        return;
    }
    that.settings.pageNo = that.settings.pageTotal
    that.settings.currentChange(that.settings.pageNo);
    
}






你可能感兴趣的:(其它,jquery,js,排序,按条件排序)