0

I'm using ionic 5.4.5. RestApi was created with node and express js. When I want to search, the app just looking for datas on the first page. I want the app to search for all datas.How can I do this?

Our api paginated.

app.get("/units", (req, res) => {
   let page = parseInt(req.query.page)
   let limit = parseInt(req.query.limit)

   if(!page && !limit){
      page = 1;
      limit = 5;
   }

   let startIndex = (page -1) * limit
   let endIndex = page * limit

   let results = {} 
   if(endIndex < data.length){
   results.next = {
      page : page + 1,
      limit : limit
   }
   }

   if(startIndex > 0){
   results.previous = {
      page : page - 1,
      limit : limit
   }
}

   results.results = data.slice(startIndex,endIndex)
   res.json(results);
});

app.get("/units/:id", (req, res) => {
   const itemId = req.params.id;
   const item = data.find(_item => _item.id == itemId);

   if (item) {
      res.json(item);
   } else {
      res.json({ message: `item ${itemId} doesn't exist`})
   }
});

home.page.ts

 getItems(ev) {
   let val = ev.target.value;

   if (val == '') {
   this.loadUnit();
   return;
   }
    if (val && val.trim() != '') {
      this.unit = this.unit.filter((un,name) => {
        return (un.name.toLocaleLowerCase().indexOf(val.toLocaleLowerCase()) > -1);
      })
    } 
 }

I also use a service to get data from the API.

ucnumara
  • 155
  • 1
  • 1
  • 11

1 Answers1

0

Either return all data from the API and perform filtering and pagination on the app. Or perform filtering and pagination (in that order) in the API.

The last option is probably better if there are a lot of data rows, because:

  • Sending all rows to the app will use a lot of data and take a lot of time, especially over a WiFi or mobile data connection.
  • Processing on the server is usually faster than processing on the client, especially if it is a mobile device.

You might also want to return the total number of rows from the API.

rveerd
  • 3,620
  • 1
  • 14
  • 30
  • Our api paged. I also made the path all the data was displayed. When I make a request from the paging link, it is only the first page's search. When I pull all the data, it won't search. Wouldn't this be done with the code I wrote? – ucnumara Nov 05 '19 at 20:34
  • You need to make that API support searching for all data. You can't do it on the client side except if you get all data as @rveerd said. You can check this: https://levelup.gitconnected.com/node-js-filtering-sorting-and-pagination-50ce6c90d0ad – yazantahhan Nov 06 '19 at 08:01
  • @ucnumara Your API only returns one page. Your app can only filter these results. You need to send the filter (`val`) to you API. You can use the [query string](https://expressjs.com/en/4x/api.html#req.query) for this. First filter all data and then perform pagination in your API. Return the result to your app. – rveerd Nov 06 '19 at 08:55