1

I am trying to implement server side pagination, that's why I used Pagination-v2 plugin which is fetching data from MongoDB server and appending into data table but now the problem is, after fetching the data.T he searching option is not working.I tried to use plugins Populate method but still it is not working. Please help me to solve this searching issue.

NodeJS code.

app.get('/gettable',(req,res)=>{
    console.log(req.query);
     user.paginate({},{
        page:Math.ceil(req.query.start / req.query.length) + 1,
        limit:parseInt(req.query.length),

        populate:{
            match:{
                name:'Graiden Waller'
            }
        }  
    },function(err,result){
        console.log(result); 
        var mytable = {
            draw:req.query.draw, 
            recordsTotal:0,
            recordsFiltered:0,
            data:[],  

        }
        if(err) {
            console.log(err);
            res.json(mytable);
        } else {
            if(result.totalDocs > 0) {
                mytable.recordsTotal = result.totalDocs;
                mytable.recordsFiltered = result.totalDocs;

                for(var key in result.docs) {
                    mytable.data.push([
                        result.docs[key]['name'],
                        result.docs[key]['lastname'],
                        result.docs[key]['email'],
                        result.docs[key]['pass'],
                        result.docs[key]['birthdate'],
                        result.docs[key]['zipcode'],
                        result.docs[key]['phonenumber'],
                    ]);
                }
              }
               res.json(mytable); 
          }
});  });

as per my logic ,it should show me data related to name:"Graiden Waller" but its giving count function error.Please suggest me any other way to do searching in data table or please suggest me any other plugin to do this.Thank you Current plugins github repository is Mongoose-pagination-v2

Arjun
  • 1,294
  • 13
  • 24

1 Answers1

2

Can you try this?

...
user.paginate({
            name:'Graiden Waller'
        },{
    page:Math.ceil(req.query.start / req.query.length) + 1,
    limit:parseInt(req.query.length)
},
...
  • Hi,thank you for your answer. Your solution is working but by default its showing 0 data in data table. After writing whole name, its giving me a particular row of that name.I want to see whole data first and after the search it should give me a particular row. – Arjun Jan 29 '19 at 11:30
  • If you want to filter the data on client side, this paginator can not help you. – Norbert Kurkó Jan 29 '19 at 12:52
  • On search event you have to send a new GET request with a 'search' parameter: `user.paginate({ name: req.query.search },{..` – Norbert Kurkó Jan 29 '19 at 13:07
  • I used this name:{ $regex: text, $options: '' }, and its working now.but it can filter only in name column ,when I am trying to write same like this name:{ $regex: text, $options: '' }, lastname:{$regex:text,$options:"} then nothing is working.Do you know how to write multiple query like this? – Arjun Jan 29 '19 at 13:14
  • I think you can. Please check it here: [textSearch](https://docs.mongodb.com/manual/text-search/) or [an answer](https://stackoverflow.com/a/19802670/5766411) – Norbert Kurkó Jan 29 '19 at 13:35
  • Its done with $or:[{name : { $regex: text, $options: '' }}, {lastname : {$regex: text, $options: ''}}] } this.Thank you for the help :) – Arjun Jan 29 '19 at 13:38