Last active
July 8, 2019 17:38
-
-
Save Daudongit/7702071ceca8a66bf5d573c70b7eff53 to your computer and use it in GitHub Desktop.
Simple pagination hook for AdonisJs and bootstrap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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