Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

117
Visualizações
Nodejs search and paginate returned results

I need help or advice on how to manage this. I'm working on a node / mysql project. I need to paginate my data which I've managed to implement successfully. Now I only have one problem that has taken me over two days trying to solve to no avail. When a user searches in my site and let's assume the search results is over 100 records, I need to paginate these results on the frontend. Now the issue is,how do I maintain the search query when the user clicks on page 2 or 3. The situation at the moment is when the user searches the first page record is returned successfully and well paginated but when he clicks on page 2 the search query ain't there anymore. so am trying to figure out how to maintain this query search in my app so that when he searches for 1000 records they're able to be paginated and he maeuvres through each page containing the search results successfully.

this is my backend pagination code at the moment with the pagination and my frontend template is ejs

app.get('/jobs', (req, res) => {       
    
    db.query(sql, (err, result) => {
        if(err) throw err;
        const numOfResults = result.length;
        const numberOfPages = Math.ceil(numOfResults / resultsPerPage);
        let page = req.query.page ? Number(req.query.page) : 1;
        if(page > numberOfPages){
            res.redirect('/?page='+encodeURIComponent(numberOfPages));
        }else if(page < 1){
            res.redirect('/?page='+encodeURIComponent('1'));
        }
        //Determine the SQL LIMIT starting number
        const startingLimit = (page - 1) * resultsPerPage;
        //Get the relevant number of POSTS for this starting page
        sql = `SELECT * FROM photos LIMIT ${startingLimit},${resultsPerPage}`;
        db.query(sql, (err, result)=>{
            if(err) throw err;
            let iterator = (page - 5) < 1 ? 1 : page - 5;
            let endingLink = (iterator + 9) <= numberOfPages ? (iterator + 9) : page + (numberOfPages - page);
            if(endingLink < (page + 4)){
                iterator -= (page + 4) - numberOfPages;
            }
            res.render('index', {data: result, page, iterator, endingLink, numberOfPages});
        });
    });
});
about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda