题解 | #分页#

分页

https://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.createElement("ul"); //TODO: 创建分页组件根节点
    if (!this.el) return;

    this.el.innerHTML = this.html();
    container.appendChild(this.el);
    this.el.className = this.total <=1 ? 'pagination hide' : 'pagination'; //TODO: 判断是否需要隐藏当前元素

    function html() {
        if (this.total <= 1) return '';
        let text = "";
        if (this.total <= 5) {
            for(let i = 1; i <= total; i++) {                
                text += `<li${i === this.current ? ' class="current"' : ''}>${i}</li>`;
                console.log(text)
            }
        } else {
            let i = this.current, j = i;
            text = `<li class="current">${this.current}</li>`
            while (j - i < 4) {
                if (i > 1) {
                    text = `<li>${--i}</li>` + text;
                }
                if (j < total) {
                    text = text + `<li>${++j}</li>`;
                }
            }
            if (i > 1) text = "<li>首页</li>" + text;
            if (j < this.total) text = text + "<li>末页</li>";
        }
        //TODO: 生成组件的内部html字符串
        return text;
    }

    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();
    };
}

全部评论

相关推荐

不愿透露姓名的神秘牛友
07-02 17:58
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
06-29 17:30
找实习找着找着就要进入7月了,马上秋招也要开始了,找实习还有意义吗?
绝迹的星:有面就面, 没面上就当日薪4位数大佬免费培训, 面上了再考虑要不要实习
点赞 评论 收藏
分享
龙珠传说:nb,公务员解约不需要支付违约金吧
点赞 评论 收藏
分享
认真搞学习:28小登的建议,投算法岗不要写什么物理竞赛,互联网+,多写点项目,用什么算法做了什么。还有本科算法是不可能的开发你这个也没有项目啊
点赞 评论 收藏
分享
06-27 18:45
中山大学 Ruby
25届应届毕业生,来广州2个礼拜了,找不到工作,绝望了,太难过了…
应届想染班味:9爷找不到工作只能说明,太摆了或者太挑了。
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务