题解 | #分页#

分页

http://www.nowcoder.com/practice/71da71df9d1c4af9b8dc056213473ba7

function Pagination(container, total, current) {
this.total = total;
this.current = current;
this.html = html;
this.val = val;
this.el = document.querySelector('.pagination'); //TODO: 创建分页组件根节点
if (!this.el) return;
this.val()
this.el.innerHTML = this.html();
container.appendChild(this.el);
// this.el.className = '';
//TODO: 判断是否需要隐藏当前元素
if (total <= 1) {
this.el.classList.add('hide')
}
function html() {

    var liList = ''
    if (this.total <= 1){ 
        return '';
    }
    var start = 1;
    var end = total > 5 ? 5 :total;
    var head = `<li>首页</li>`
    var foot = `<li>末页</li>`
    if (current-2 > 1 && current+2 < total) { 
      // 有首页有末页
      start = current-2
      end = current+2
    } else if (current-2 > 1 && current+2 >= total) { 
      // 有头无尾
      start = total-4
      end = total
      foot = ''
    } else if (current-2 <= 1 && current+2 < total) { 
      // 无头有尾
      start = 1
      end = 5
      head = ''
    } else { 
      // 无头无尾
      start = 1
      end = total
      head = ''
      foot = ''
    }
    liList += head
    for (let index=start; index <= end;index++) {
        index===current ? liList += `<li class = 'current'>${index}</li>` : liList += `<li>${index}</li>`
    }
    liList += foot
    console.log(liList, 'liList')
    return liList;
}



function val(current) {
    if (arguments.length === 0) return this.current;
    if (current < 1 || current > this.total || current === this.current) return;
    this.current = current;
    this.el.innerHTML = this.html();
};

}

全部评论
// 有头无尾 if (total - 4 > 0) { start = total - 4; } else { start = 1; head = '' } end = total foot = '' 有头无尾得这样
点赞 回复 分享
发布于 03-06 07:03 广东

相关推荐

11-11 14:21
西京学院 C++
无敌混子大王:首先一点,不管学校层次怎么样,教育经历放在第一页靠上位置,第一页看不到教育经历,hr基本直接扔掉了
点赞 评论 收藏
分享
评论
6
收藏
分享
牛客网
牛客企业服务