Examples of vue. js table paging and vue. js table Paging
Paging is generally used together with a table. A paging link is used as a part of a table. It is reasonable to encapsulate a paging link into an independent component and then embed it into a table component as a child component.
Effect:
Code:
1. Register a component
Js
Vue. component ('pagination', {template: '# paginationTpl', replace: true, props: ['cur', 'all', 'pagenum'], methods: {// page number Click Event 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 = []; // calculate the left and right page numbers var mid = parseInt (this. pageNum/2); // The median value 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 (lef T); 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 =" {'activity ': cur = index} "> <a @ click =" btnClick (index) "href =" javascript: ">{{ index }}</a> </li> <li v-if =" showLast "> <a @ click =" cur ++ "href =" javascript: ">» </a> </li> <a> total <I >{{ all }}</I> pages </a> </li> </ul> </nav> </script>
HTML:
<div id='item_list'> ... <pagination :cur="1" :all="pageAll" :page-num="10" @page-to="loadList"></pagination></div>
When you click a paging link, use the watch cur sub-component to distribute page-to events and use the @ page-to = "loadList" label to specify the parent component loadList method to process events, the parent component receives the page value and then loads the data using ajax. Based on the result returned by the server, it calculates and updates its own pageAll value, because the child component prop passes through: all = "pageAll" is dynamically bound to the pageAll object of the parent component, so the child components are updated together.
An example of a simple table component is attached:
Var vm = new Vue ({el: "# item_list", data: {items: [], // pageAll: 0, // total number of pages, calculate perPage: 10 // Number of pages per page} based on the total value returned by the server, methods: {loadList: function (page) {var that = this; $. ajax ({url: "/getList", type: "post", data: {"page": page, "perPage": this. perPage}, dataType: "json", error: function () {alert ('request list failed')}, success: function (res) {if (res. status = 1) {that. items = res. data. list; that. perPage = res. data. perPage; that. pageAll = Math. round (res. data. total/that. perPage); // total number of calculated pages }}}) ;}, // initialize init: function () {this. loadList (1) ;}}); vm. init ();
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.