如何使用knex js显示分页?

时间:2019-02-16 07:52:25

标签: node.js express knex.js

我在我的项目中使用express js和node js,并且对显示的记录实施了knex分页,但是面对记录下方的显示分页(分页),有人可以建议我如何显示分页吗? 下面我已经发布了代码。

getUsers: (req, res) => {

                var reqData    = req.query;
                var pagination = {};
                var per_page   = reqData.per_page || 2;
                var page       = reqData.current_page || 1;
                if (page < 1) page = 1;
                var offset = (page - 1) * per_page;

                Promise.all([
                        knex.count('* as count').from(customers).first(),
                        knex.select("*").from(customers).offset(offset).limit(per_page)
                ]).then(([total, rows]) => {

                                        var count = total.count;
                                        var rows = rows;
                                        pagination.total = count;
                                        pagination.per_page = per_page;
                                        pagination.offset = offset;
                                        pagination.to = offset + rows.length;
                                        pagination.last_page = Math.ceil(count / per_page);
                                        pagination.current_page = page;
                                        pagination.from = offset;
                                        pagination.data = rows;
                                        res.render('admin/users/index', { data: pagination });
                                })
                    .catch(error => {

                    });
},

我的树枝文件

    <table class="table table-striped table-bordered table-hover dataTables-example dataTable" id="DataTables_Table_0" aria-describedby="DataTables_Table_0_info" role="grid">
    <thead>
      <tr role="row">
         <th class="sorting_asc" tabindex="0" aria-controls="DataTables_Table_0" rowspan="1" colspan="1" aria-sort="ascending" aria-label="Rendering engine: activate to sort column descending" style="width: 166px;">Email</th>
      </tr>
    </thead>
    <tbody>
        {% for res in data.data %}
            <tr>
               <td>{{ res.email }}</td>
            </tr>
        {% endfor %}

        ?? paginaion 

    </tbody>

knex pagination

0 个答案:

没有答案