vue.js 表格分頁ajax 非同步載入資料,vue.jsajax

來源:互聯網
上載者:User

vue.js 表格分頁ajax 非同步載入資料,vue.jsajax

Vue.js是一個輕巧、高效能、可組件化的MVVM庫,同時擁有非常容易上手的API。

分頁一般和表格一起用,分頁連結作為表格的一部分,將分頁連結封裝成一個獨立的組件,然後作為子組件嵌入到表格組件中,這樣比較合理。

1.註冊一個組件

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}}});

模板:

<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='parentEle'>...<pagination :cur="1" :all="pageAll" :page-num="10" @page-to="loadList"></pagination></div>

當點擊分頁連結的時候,會觸發

page-to

事件,而我們在html標籤中指定了使用父組件

loadList

方法處理事件,我們只要在組件中把當前頁碼傳給父組件即可,父組件負責ajax載入資料,並更新自身的 pageAll 值。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.