Skip to content

Instantly share code, notes, and snippets.

@Daudongit
Last active July 8, 2019 17:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Daudongit/7702071ceca8a66bf5d573c70b7eff53 to your computer and use it in GitHub Desktop.
Save Daudongit/7702071ceca8a66bf5d573c70b7eff53 to your computer and use it in GitHub Desktop.
Simple pagination hook for AdonisJs and bootstrap
View.global('simplePagination', function (page,lastPage) {
const url = this.resolve('url')
const prevPage = ()=>{
if(page == 1){
return `<li class="page-item disabled"><span class="page-link">«</span></li>`
}
else{
return `<li><a href="${url}?page=${parseInt(page) - 1}">«</a></li>`
}
}
const nextPage = ()=>{
if(parseInt(lastPage) > parseInt(page)){
return `<li><a href="${url}?page=${parseInt(page) + 1}">»</a></li>`
}else{
return `<li class="page-item disabled"><span class="page-link">»</span></li>`
}
}
return this.safe(`
<ul class="pagination pagination-md">
${prevPage()}
<li><a href="#">page ${page} of ${lastPage}</a></li>
${nextPage()}
</ul>
`)
})
//Sample use case
//After setting up database and Item(model)
//In controller app\Controllers\Http\ItemContoller.js
// 'use strict'
// const Item = use('App/Models/Item')
// class ItemController {
// async index ({ view,request }) {
// const items = await Item.query().paginate(
// request.input('page',1),5
// )
// return view.render('items.index',{
// items:items.toJSON()
// })
// }
// }
// module.exports = ItemController
//In view resources\views\items\index.edge
//<div class="text-center">{{simplePagination(items.page,items.lastPage)}}</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment