Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

109
Vistas
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 la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda