


Javascript vue.js table pagination, ajax asynchronous loading of data
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.
Effect:
Code:
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 = Math.max(this.cur - mid,1); var right = Math.max(this.cur + this.pageNum - mid -1,this.pageNum); if (right > this.all ) { right = this.all} while (left <= right){ list.push(left); left ++; } return list; }, showLast: function(){ return this.cur != this.all; }, showFirst: function(){ return this.cur != 1; } } });
Template:
<script type="text/template" id="paginationTpl"> <nav v-if="all > 1"> <ul class="pagination"> <li v-if="showFirst"><a href="javascript:" @click="cur--">«</a></li> <li v-for="index in indexes" :class="{ 'active': cur == index}"> <a @click="btnClick(index)" href="javascript:">{{ index }}</a> </li> <li v-if="showLast"><a @click="cur++" href="javascript:">»</a></li> <li><a>共<i>{{all}}</i>页</a></li> </ul> </nav> </script>
HTML:
<div id='item_list'> ... <pagination :cur="1" :all="pageAll" :page-num="10" @page-to="loadList"></pagination> </div>
When a paging link is clicked, the child component distributes the page-to event through watch cur, and the parent is specified through the @page-to="loadList" tag. The component loadList method handles the event. The parent component receives the page value and then loads the data with ajax. It calculates and updates its own pageAll value based on the server return, because the child component prop dynamically binds the parent component's pageAll object through: all="pageAll". Therefore, the subcomponents will be updated together.
Attached is a simple table component example:
var vm = new Vue({ el: "#item_list", data: { items : [], //分页参数 pageAll:0, //总页数,根据服务端返回total值计算 perPage:10 //每页数量 }, methods: { loadList:function(page){ var that = this; $.ajax({ url : "/getList", type:"post", data:{"page":page,"perPage":this.perPage}, dataType:"json", error:function(){alert('请求列表失败')}, success:function(res){ if (res.status == 1) { that.items = res.data.list; that.perPage = res.data.perPage; that.pageAll = Math.ceil(res.data.total / that.perPage);//计算总页数 } } }); }, //初始化 init:function(){ this.loadList(1); } } }); vm.init();
Thanks for reading, I hope it can help everyone, thank you for your support of this site!
For more Javascript vue.js table paging, ajax asynchronous data loading related articles, please pay attention to the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Article discusses creating, publishing, and maintaining JavaScript libraries, focusing on planning, development, testing, documentation, and promotion strategies.

The article discusses strategies for optimizing JavaScript performance in browsers, focusing on reducing execution time and minimizing impact on page load speed.

Frequently Asked Questions and Solutions for Front-end Thermal Paper Ticket Printing In Front-end Development, Ticket Printing is a common requirement. However, many developers are implementing...

The article discusses effective JavaScript debugging using browser developer tools, focusing on setting breakpoints, using the console, and analyzing performance.

There is no absolute salary for Python and JavaScript developers, depending on skills and industry needs. 1. Python may be paid more in data science and machine learning. 2. JavaScript has great demand in front-end and full-stack development, and its salary is also considerable. 3. Influencing factors include experience, geographical location, company size and specific skills.

The article explains how to use source maps to debug minified JavaScript by mapping it back to the original code. It discusses enabling source maps, setting breakpoints, and using tools like Chrome DevTools and Webpack.

How to merge array elements with the same ID into one object in JavaScript? When processing data, we often encounter the need to have the same ID...

In-depth discussion of the root causes of the difference in console.log output. This article will analyze the differences in the output results of console.log function in a piece of code and explain the reasons behind it. �...
