Semantic UI Pagination

semantic ui pagination html

<div class="ui borderless pagination menu">
  <a class="item">
    <i class="left arrow icon"></i> Previous  </a>
  <a class="item">1</a>
  <a class="item">2</a>
  <a class="item">3</a>
  <a class="item">4</a>
  <a class="item">5</a>
  <a class="item">6</a>
  <a class="item">
    Next <i class="icon right arrow"></i>
  </a></div>

143429_qQNE_1387007.jpg

http://semantic-ui.com/collections/menu.html

ajax pagination javascript

function Pagination(obj) {
    this.id = obj.id;  //div id
    this.url = obj.url; 
    this.pageSize = obj.pageSize; 
    this.pageNum = 1; //current page number
    this.total = 0; //total count
    this.totalPage = 0; 
    this.barSize = obj.barSize; //分页工具条上展现的页码数
    this.numPoint=1;  
    this.data = obj.data;
    this.success = obj.success;
    this.error = obj.error;
    this.div = null;
    this.init();
}
Pagination.prototype.init = function () {
    if (this.data == undefined) {
        this.data = {}
    }
    this.div = $('#' + this.id);
    this.fetchData(1);
};

Pagination.prototype.fetchData = function (pageNum) {
    this.data.pageNum = pageNum;
    this.data.pageSize = this.pageSize;
    var that = this;
    $.ajax({
        url: that.url,
        data: that.data,
        type: 'post',
        dataType: 'json',
        success: function (data) {
            if(data.total==undefined) {
                that.success(data.rows);
                return;
            }
            that.total = data.total;
            var tmp = that.total % that.pageSize;
            var num = Math.floor(that.total / that.pageSize);
            that.totalPage = tmp == 0 ? num : num + 1;
            that.showUI();
            that.success(data.rows);
        },
        error: function (msg) {
            that.error(msg);
        }
    })
};
Pagination.prototype.showUI = function () {
    var that = this;
    this.div.empty();
    var currentBarSize=this.totalPage-(this.numPoint-1)*this.barSize;
    currentBarSize = currentBarSize > this.barSize ? this.barSize : currentBarSize;
    this.div.append('<a class="item"><i class="left arrow icon"></i> 上一页</a>');
    for (var i = 0; i < currentBarSize; i++) {
        this.div.append('<a class="item">' + (this.pageNum+i) + '</a>');
    }
    this.div.append('<a class="item"> 下一页 <i class="icon right arrow"></i></a>');

    var array = this.div.find('a');
    for (var j = 0; j < array.length; j++) {
        var current = $(array[j]);
        if (j == 0) {
            current.click({param: that}, that.previewPage);
        } else if (j == array.length - 1) {
            current.click({param: that}, that.nextPage)
        } else {
            current.click({param: that}, function (event) {
                var p = event.data.param;
                var n = $(this).text().trim();
                p.fetchData(parseInt(n));
            })
        }
    }
};
Pagination.prototype.nextPage = function (event) {
    var that = event.data.param;
    if (that.numPoint > 1) {
        that.div.find('a').first().attr('class', 'item');
    }
    if (that.numPoint >= Math.ceil(that.totalPage/that.barSize)) {
        that.div.find('a').last().attr('class', 'disabled item');
    } else {
        that.pageNum=that.numPoint*that.barSize+1;
        that.numPoint++;
        that.showUI();
    }
};
Pagination.prototype.previewPage = function (event) {
    var that = event.data.param;
    if (that.numPoint <Math.ceil(that.totalPage/that.barSize)) {
        that.div.find('a').last().attr('class', 'item');
    }
    if (that.numPoint == 1) {
        that.div.find('a').first().attr('class', 'disabled item');
    } else {
        that.numPoint--;
        that.pageNum=(that.numPoint-1)*that.barSize+1;
        that.showUI();
    }
};

示例

new Pagination({
    id: "pagination",
    url: "<c:url value="/image/showImage"/>",
    pageSize: 5, //每页显示的记录数
    barSize: 5,//分页工具条上展现的页码数
    data: {panoId: '201501055A_1353497', model: 'sphere', level: 'F'},
    success: function (rows) { //回调函数
        console.log(rows);
    },
    error: function (msg) {//回调函数
        console.log(msg);
    }
})

服务端返回的数据格式:

{"total":33,"rows":[{},{}....]}


转载于:https://my.oschina.net/u/1387007/blog/376104

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值