Pagination with sequelize + Node.js

Asked

Viewed 1,053 times

1

Hello, I am developing an application Node.js where I have to list a register of classes. But as the number of records are many need to make a pagination, I achieved this by making a function with the offset and limit attributes of the sequelize as below

Routes:

router.get('/turmas/page/:skip', (req, res) => {
    Turma.select(req, res)
})

Function that searches the data in the database

  select: (req, res) => {

        let page = 0;
        if (req.params) {

            page = parseInt(req.params.skip) * 3
        }

        console.log(page)

        Turma.findAll({
            limit: 3,
            offset: page
        }).then((turmas => {
            res.render('turma/index', {
                title: "Tumas",
                titlePanel: "Turmas",
                turmas: turmas
            })
        }))

    },

But I wanted to know if there is another way to do this and how can I insert this in my front-end html since I have already created Nav with the pages as below

<%- include ./../layout/header %>
<a href="/turmas/register">
   <button class="btn btn-success ">Nova turma</button>
</a>


 <div class="container">
     <table class="table">
    <thead>
        <tr>
            <th>ID</th>
            <th>Nome</th>
            <th>Serie</th>
            <th>Opções</th>
        </tr>
    </thead>



    <tbody>
  <% turmas.forEach((turma)=> { %>
        <tr>
            <td><%= turma.TR_ID %></td>
            <td><%= turma.TR_NAME%></td>
            <td><%= turma.TR_YEAR %> Ano</td>
            <td>    

            <a href="turmas/update/<%= turma.TR_ID %>">
            <button class="btn btn-warning"> Editar </button>
            </a>

             <a href="turmas/delete/<%= turma.TR_ID %>">
                <button class="btn btn-danger"> Excluir </button>
             </a>
            </td>
        </tr>

         <% })%>


    </tbody>


</table>

        <!-- Nav com as paginas -->
        <nav aria-label="...">
            <ul class="pagination">
                <li class="page-item disabled"><a class="page-link" href="#" tabindex="-1" aria-disabled="true">Previous</a></li>
                <li class="page-item"><a class="page-link" href="#">1</a></li>
                <li class="page-item active" aria-current="page">
                <a class="page-link" href="#">2 <span class="sr-only">(current)</span></a>
                </li>
                <li class="page-item"><a class="page-link" href="#">3</a></li>
                <li class="page-item">
                <a class="page-link" href="#">Next</a>
                </li>
            </ul>
        </nav>

Would anyone know the best way to do this paging with Node.js + sequelize ? could be with another example of code.

  • It seems to be well done. What’s the problem anyway?

  • https://answall.com/users/35970/bruno-costa. Back-end is working what you’d like to know is how to print the front-end to Nav for each page link. That is how to print the buttons to select the page in question and proceed.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.