Adocasts Plus Timed Exclusive

Please check back in 19 hours. Get immediate access to this lesson and more by joining Adocasts Plus for $8.00/mo or signing into your existing Adocasts+ account.

robot mascot smiling

How To Paginate Filtered Query Results

In this lesson, we'll learn about AdonisJS Model Query Builder pagination and walk through an example of how we can easily paginate the results of our filtered movies.

Published
May 06
Duration
9m 15s

Developer, dog lover, and burrito eater. Currently teaching AdonisJS, a fully featured NodeJS framework, and running Adocasts where I post new lessons weekly. Professionally, I work with JavaScript, .Net C#, and SQL Server.

Adocasts

Burlington, KY

Adocasts Plus Timed Exclusive

Please check back in 19 hours. Get immediate access to this lesson and more by joining Adocasts Plus for $8.00/mo or signing into your existing Adocasts+ account.

Join The Discussion! (1 Comments)

Please sign in or sign up for free to join in on the dicussion.

  1. Commented 3 days ago

    Hey all! Somehow, I completely missed that there's a queryString method available on the paginator that allows us to define additional query string key/values. We'll patch this in down the road, but wanted to make a note here that it is available!

    async index({ request, view, auth }: HttpContext) {
      const page = request.input('page', 1)
      const filters = await movieFilterValidator.validate(request.qs())
      const movies = await MovieService.getFiltered(filters, auth.user).paginate(page, 15)
      const movieStatuses = await MovieStatus.query().orderBy('name').select('id', 'name')
      const movieSortOptions = MovieService.sortOptions
    --  const qs = querystring.stringify(filters)
    
      movies.baseUrl(router.makeUrl('movies.index'))
    ++ movies.queryString(filters)
    
      const rangeMin = movies.currentPage - 3
      const rangeMax = movies.currentPage + 3
      let pagination = movies.getUrlsForRange(1, movies.lastPage).filter((item) => {
        return item.page >= rangeMin && item.page <= rangeMax
      })
    
    --  if (qs) {
    --    pagination = pagination.map((item) => {
    --      item.url += `&${qs}`
    --      return item
    --    })
    --  }
    
      return view.render('pages/movies/index', {
        movies,
        movieStatuses,
        movieSortOptions,
        filters,
        pagination,
        qs,
      })
    }
    Copied!
    0

    Please sign in or sign up for free to reply