Home  >  Article  >  Web Front-end  >  vue.js table pagination ajax asynchronous loading of data

vue.js table pagination ajax asynchronous loading of data

高洛峰
高洛峰Original
2017-01-12 13:41:151392browse

Vue.js is a lightweight, high-performance, componentable MVVM library, and has a very easy-to-use API.

Paging is generally used together with tables. The paging link is used as part of the table. It is more reasonable to encapsulate the paging link into an independent component and then embed it into the table component as a sub-component.

1. Register a component

js

Vue.component('pagination',{
template:'#paginationTpl',
replace:true,
props:['cur','all','pageNum'],
methods:{
//页码点击事件
btnClick: function(index){
if(index != this.cur){
this.cur = index
}
}
},
watch:{
"cur" : function(val,oldVal) {
this.$dispatch('page-to', val)
}
},
computed:{
indexes : function(){
var list = []
//计算左右页码
var mid = parseInt(this.pageNum / 2);//中间值
var left = this.cur - mid;
var right = Math.max(this.cur + this.pageNum - mid -1,this.pageNum);
if (left < 1) {left = 1}
if (right > this.all ) { right = this.all}
while (left <= right){
list.push(left)
left ++
}
return list;
},
showLast: function(){
if(this.cur == this.all){
return false
}
return true
},
showFirst: function(){
if(this.cur == 1){
return false
}
return true
}
}
});

Template:

HTML:

...

When you click the paging link, The

page-to

event will be triggered, and we specified in the html tag to use the parent component

loadList

method to handle the event. We only need to Just pass the current page number to the parent component in the component. The parent component is responsible for loading data via ajax and updating its own pageAll value.

For more articles related to vue.js table pagination ajax asynchronous loading of data, please pay attention to the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn