VueJs Paginator is a simple but powerful plugin since it gives you access on how to render the data, instead of using a predefined table.
It takes a resource_url
and generates simple pagination buttons. Every time a page is changed or fetched, it emits an event to update the resource.
animals
variable contents:
{{ animals }}
npm install vuejs-paginator --save
<script src="https://cdn.jsdelivr.net/vuejs-paginator/2.0.0/vuejs-paginator.min.js"></script>
<!--or-->
<script src="https://cdnjs.cloudflare.com/ajax/libs/vuejs-paginator/2.0.0/vuejs-paginator.js"></script>
Import VuePaginator
and define it in your components.
new Vue({
el: '#app',
data () {
return {
// The resource variable
animals: [],
// Here you define the url of your paginated API
resource_url: 'http://hootlex.github.io/vuejs-paginator/samples/animals1.json'
}
},
components: {
VPaginator: VuePaginator
},
methods: {
updateResource(data){
this.animals = data
}
}
...
});
Use it in your HTML
<v-paginator :resource_url="resource_url" @update="updateResource"></v-paginator>
<ul>
<li v-for="animal in animals">
{{ animal.name }}
</li>
</ul>
Name | Type | Default | Description |
---|---|---|---|
offset | Number |
0 |
Pixels to offset from top of screen when calculating position of scroll. |
remote_data | String |
'data' | The key on the response object which contains the value for data. |
remote_current_page | String |
'current_page' | The key on the response object which contains the value for current_page. |
remote_last_page | String |
last_page | The key on the response object which contains the value for last_page. |
remote_next_page_url | String |
'next_page_url' | The key on the response object which contains the value for next_page_url. |
remote_prev_page_url | String |
'prev_page_url' | The key on the response object which contains the value for prev_page_url. |
previous_button_text | String |
'Previous' | The text value of the previous button. |
next_button_text | String |
'Next' | The text value of the next button. |
headers | String |
null | Additional headers that will be included in the HTTP call. |
resource_url
.fetchData()
method.update
event and update the resource variable.
To access Paginator's fetchData()
you have to assign a reference ID to the component using ref
. For example:
<v-paginator :resource_url="resource_url" ref="vpaginator" @update="updateResource"></v-paginator>
vm.$refs.vpaginator.fetchData(resource_url)
.
If no resource_url
is passed, the current page will be fetched again.
{
"nested": {
"current_page": 1,
"last_page": 3,
"next_page_url": "/api/animals?page=2",
"prev_page_url": null,
"data": [
{
"id": 1,
"name": "Crocodile"
},
{
"id": 2,
"name": "Bobcat"
},
{
"id": 3,
"name": "Elephant"
},
{
"id": 4,
"name": "bandicoot"
},
{
"id": 5,
"name": "barracuda"
}
]
}
}
options
object and pass it as a property to the paginator
.
// If you are using the library from cdn you don't have to import these.
import Vue from 'vue'
import VueResource from 'vue-resource'
import VuePaginator from 'vuejs-paginator'
Vue.use(VueResource)
//end if
new Vue({
el: '#app',
components: {
VPaginator: VuePaginator
},
data: {
animals: [],
resource_url: '/api/animals',
options: {
remote_data: 'nested.data',
remote_current_page: 'nested.current_page',
remote_last_page: 'nested.last_page',
remote_next_page_url: 'nested.next_page_url',
remote_prev_page_url: 'nested.prev_page_url',
next_button_text: 'Go Next',
previous_button_text: 'Go Back'
}
},
methods: {
updateResource(data){
this.animals = data
}
}
});
options
prop:
<v-paginator :options="options" :resource_url="resource_url" @update="updateResource"></v-paginator>