节点分页仅显示4页

时间:2018-12-06 17:56:42

标签: node.js express pagination

我使用上一页,下一页,最后一页,第一页按钮和所有页面的数组进行了简单的分页。

routes.js
    // View users
    router.get("/show/:page", function (req, res, next) {
        var perPage = 3
        var page = req.params.page || 1

        User
            .find({})
            .skip((perPage * page) - perPage)
            .limit(perPage)
            .exec(function (err, users) {
                User.countDocuments().exec(function (err, count) {
                    if (err) return next(err)
                    res.render('../modules/users/views/userList', {
                        layout: "cmsLayout",
                        users: users,
                        current: page,
                        pages: Math.ceil(count / perPage),
                        helpers,
                        pagesArr: Array.from(Array(((Math.ceil(count / perPage)))).keys()).map(i => 1 + i)
                    })
                })
            })
    });

车把

<!-- PAGINATION -->
        <ul class="paginator">
            {{#gt current 1}}
            <li class="paginator-item">
                <script></script>
                <a href="{{minus current 1}}" class="paginator-itemLink">Prev</a>
            </li>
            {{else}}
            <li class="paginator-item">
                <a href="" class="paginator-itemLink">Prev</a>
            </li>
            {{/gt}}
            <li class="paginator-item">
                <a href="1" class="paginator-itemLink">1</a>
            </li>
            <li class="paginator-item">
                <a href="" class="paginator-itemLink">...</a>
            </li>
            {{#each pagesArr}}
            <li class="paginator-item">
                <a href="{{this}}" class="paginator-itemLink">{{this}}</a>
            </li>
            {{/each}}
            <li class="paginator-item">
                <a href="" class="paginator-itemLink">...</a>
            </li>
            <li class="paginator-item">
                <a href="{{pages}}" class="paginator-itemLink">{{pages}}</a>
            </li>
            {{#gt pages current}}
            <li class="paginator-item">
                <a href="{{add current 1}}" class="paginator-itemLink">Next</a>
            </li>
            {{else}}
            <li class="paginator-item">
                <a href="" class="paginator-itemLink">Next</a>
            </li>
            {{/gt}}

输出看起来像这样

  

[上一页] [1] ... [1] [2] [3] [4] [5] [6] [7] ... [7] [下一页]

但是我想做的是仅显示4页,而有效号码后仅显示一页,我不知道如何。
像这样

  

[上一个] [1] ... [3] [4] [5] [6] ... [7] [下一个] // 5已激活

感谢您的任何建议或解决方案。

0 个答案:

没有答案