這篇文章主要為大家詳細(xì)介紹了基于Vue.js的表格分頁(yè)組件使用方法,了解了Vue.js的特點(diǎn),感興趣的朋友可以參考一下
一、Vue.js簡(jiǎn)介
1、Vue的主要特點(diǎn): (1) 簡(jiǎn)潔 (2) 輕量 (3)快速 (4) 數(shù)據(jù)驅(qū)動(dòng) (5) 模塊友好 (6) 組件化
(1) 簡(jiǎn)潔
下面看一段Angular的實(shí)現(xiàn)雙向綁定的代碼
// html
<body ng-app="myApp">
<div ng-controller="myCtrl">
<p>{{ note }}</p>
<input type="text" ng-model="note">
</div>
</body>
// js
var myModule = angular.module('myApp', []);
myModule.controller('myCtrl', ['$scopp', function($scope) {
$scope.note = '';
]);
然后再看一下Vue的代碼:
// html
<body>
<div id="app">
<p>{{ note }}</p>
<input type="text" v-model="note">
</div>
</body>
// js
var vm = new Vue({
el: '#app',
data: {
note: ''
}
})
相比較而言我個(gè)人認(rèn)為Vue的代碼編寫(xiě)風(fēng)格更加簡(jiǎn)潔,并且通俗易懂。
(2)不失優(yōu)雅
Vue雖然是一個(gè)比較輕量級(jí)的框架,簡(jiǎn)單輕量的同時(shí)還非常的人性化,其提供的API也是非常的容易理解,同時(shí)也提供了一些很便捷的指令和屬性。
例如:
1)、綁定click事件
<a v-on:click="doSomething"></a>
可以簡(jiǎn)寫(xiě)為:
<a @click="doSomething"></a>
2)、 綁定動(dòng)態(tài)屬性
<a v-bind:href="url"></a>
可以簡(jiǎn)寫(xiě)為:
<a :href="url"></a>
3)、便捷的修飾符
<!-- 阻止單擊事件冒泡 -->
<a @click.stop="doSomething"></a>
<!-- 只在按下回車(chē)鍵的時(shí)候觸發(fā)事件 -->
<input @keyup.enter="submit">
4)、實(shí)用的參數(shù)特性
<!-- debounce 設(shè)置一個(gè)最小的延時(shí) -->
<input v-model="note" debounce="500">
<!-- 在 "change" 而不是 "input" 事件中更新數(shù)據(jù) -->
<input v-model="msg" lazy>
怎么樣,是不是感覺(jué)優(yōu)雅極了。
(3)小巧
說(shuō)起小巧,那應(yīng)該首先要關(guān)注下Vue的源碼大小,Vue的成產(chǎn)版本(即min版)源碼僅為72.9kb,官網(wǎng)稱(chēng)gzip壓縮后只有25.11kb,相比Angular的144kb縮小了一半。
小巧的一種好處就是可以讓用戶更自由的選擇相應(yīng)的解決方案,在配合其他庫(kù)方面它給了用戶更大的空間。
如Vue的核心默認(rèn)是不包含路由和 Ajax 功能,但是如果項(xiàng)目中需要路由和AJAX,可以直接使用Vue提供的官方庫(kù)Vue-router及第三方插件vue-resource,同時(shí)你也可以使用其他你想要使用的庫(kù)或插件,如jQuery的AJAX等。
是不是感覺(jué)非常的靈活。
(4)不乏大匠
Vue雖然小巧,但是“麻雀雖小五臟俱全”,在構(gòu)建大型應(yīng)用的時(shí)候也是得心應(yīng)手。
1)、模塊化
結(jié)合一些第三方模塊構(gòu)建工具,如CommonJS、RequireJS或者SeaJs,可以輕松實(shí)現(xiàn)代碼的模塊化。
但是在這里小編不推薦使用上述構(gòu)建工具,直接使用ES6的模塊化功能,再結(jié)合Webpack進(jìn)行相應(yīng)打包是目前最熱門(mén)的方案。
不了解ES6模塊功能的可以詳見(jiàn):http://es6.ruanyifeng.com/#docs/module
在今后的文章中,我也會(huì)對(duì)其進(jìn)行介紹,包括Webpack的配置。
2)、組件化
Vue的組件化功能可謂是它的一大亮點(diǎn),通過(guò)將頁(yè)面上某一組件的html、CSS、js代碼放入一個(gè).vue的文件中進(jìn)行管理可以大大提高代碼的維護(hù)性。
例如:
// App.vue
<template>
<div class="box" v-text="note"></div>
</template>
<script>
export default {
data () {
return {
note: '這是一個(gè)組件的html模板!'
}
}
}
</script>
<style scoped>
.box {
color: #000;
}
</style>
我們還可以在組件里寫(xiě)一些預(yù)處理語(yǔ)言:
// App.vue
<template lang='jade'>
div(class="box" v-text="text")
</template>
<script>
export default {
data () {
return {
note: '這是一個(gè)組件的html模板!'
}
}
}
</script>
<style lang="stylus">
.box
color: #000
</style>
當(dāng)然這樣寫(xiě)我們是需要通過(guò)webpack來(lái)進(jìn)行打包的,推薦使用Webpack + vue-loader的方式,同時(shí)使用ES6語(yǔ)法,需要安裝babel來(lái)進(jìn)行轉(zhuǎn)換。因?yàn)槲恼聻闇\談Vue.js,所以這里不做深入介紹。
3)、路由
和Angular一樣,Vue也具有它的路由功能。通過(guò)路由功能,我們可以實(shí)現(xiàn)各個(gè)組件的按需加載,輕松構(gòu)建單頁(yè)應(yīng)用。下面是一個(gè)簡(jiǎn)單的路由配置文件:
// router.js
'use strict'
export default function(router) {
router.map({
'/': {
component: function (resolve) {
require(['./components/Foo.vue'], resolve)
}
},
'/foo': {
component: function (resolve) {
require(['./components/Foo.vue'], resolve)
}
},
'/bar': {
component: function (resolve) {
require(['./components/Bar.vue'], resolve)
}
}
})
}
二、BootPage組件簡(jiǎn)介
其實(shí)也不是啥高大上的組件了,相反確實(shí)一個(gè)簡(jiǎn)單的表格分頁(yè)組件而已,主要是自己最近項(xiàng)目中需要一個(gè)表格分頁(yè)組件,而Vue官方組件庫(kù)里分頁(yè)組件都功能太強(qiáng)大或者沒(méi)有適合我的,所以就自己寫(xiě)了一個(gè)湊合著用,或許有人和我一樣需要這樣一個(gè)簡(jiǎn)單的分頁(yè)組件來(lái)實(shí)現(xiàn)簡(jiǎn)單的分頁(yè)功能,我便在這里分享一下,大家自覺(jué)填坑咯。
如需高大上的組件,可以移步Vue官方組件庫(kù):https://github.com/vuejs/awesome-vue#libraries--plugins
(1)使用方法
在.vue的組件文件中我們這樣寫(xiě)template,即html代碼:
<table class="table table-hover table-bordered">
<thead>
<tr>
<th width="10%">id</th>
<th width="30%">name</th>
<th width="40%">content</th>
<th width="20%">remark</th>
</tr>
</thead>
<tbody>
<tr v-for="data in tableList">
<td v-text="data.num"></td>
<td v-text="data.author"></td>
<td v-text="data.contents"></td>
<td v-text="data.remark"></td>
</tr>
</tbody>
<tfoot>
<tr>
<td colspan="4">
<div class="col-sm-12 pull-right">
<boot-page :async="false" :data="lists" :lens="lenArr" :page-len="pageLen"></boot-page>
</div>
</td>
</tr>
</tfoot>
</table>
<boot-page>標(biāo)簽中async指是否從服務(wù)器端獲取數(shù)據(jù),false為否;data為靜態(tài)的為分頁(yè)的表格數(shù)據(jù)數(shù)組;lens為每頁(yè)顯示行數(shù)的數(shù)組;page-len為可顯示的頁(yè)碼數(shù);
使用靜態(tài)數(shù)據(jù)的javascript代碼即script標(biāo)簽內(nèi)的內(nèi)容如下:
<script>
import bootPage from './components/BootPage.vue'
export default {
data () {
return {
lenArr: [10, 50, 100], // 每頁(yè)顯示長(zhǎng)度設(shè)置
pageLen: 5, // 可顯示的分頁(yè)數(shù)
lists: [
{num: 1, author: 'luozh', contents: '123', remark: 'bootPage'},
{num: 1, author: 'luozh', contents: '123', remark: 'bootPage'},
{num: 1, author: 'luozh', contents: '123', remark: 'bootPage'},
{num: 1, author: 'luozh', contents: '123', remark: 'bootPage'},
{num: 1, author: 'luozh', contents: '123', remark: 'bootPage'},
{num: 1, author: 'luozh', contents: '123', remark: 'bootPage'}
], // 表格原始數(shù)據(jù),使用服務(wù)器數(shù)據(jù)時(shí)無(wú)需使用
tableList: [] // 分頁(yè)組件傳回的分頁(yè)后數(shù)據(jù)
}
},
components: {
bootPage
},
events: {
// 分頁(yè)組件傳回的表格數(shù)據(jù)
'data' (data) {
this.tableList = data
}
}
}
</script>
一般我們很少使用靜態(tài)的表格數(shù)據(jù),大多數(shù)應(yīng)用的數(shù)據(jù)都是從服務(wù)器端獲取的,所以這里提供了獲取服務(wù)器分頁(yè)數(shù)據(jù)的方法:
使用服務(wù)器數(shù)據(jù)的組件HTML如下:
<boot-page :async="true" :lens="lenArr" :url="url" :page-len="pageLen" :param="param"></boot-page>
其中url為服務(wù)器的請(qǐng)求地址;param為需要向服務(wù)器發(fā)送的參數(shù)對(duì)象;
使用服務(wù)器數(shù)據(jù)javascript的代碼如下:
<script>
import bootPage from './components/BootPage.vue'
export default {
data () {
return {
lenArr: [10, 50, 100], // 每頁(yè)顯示長(zhǎng)度設(shè)置
pageLen: 5, // 可顯示的分頁(yè)數(shù)
url: '/bootpage/', // 請(qǐng)求路徑
param: {}, // 向服務(wù)器傳遞參數(shù)
tableList: [] // 分頁(yè)組件傳回的分頁(yè)后數(shù)據(jù)
}
},
methods: {
refresh () {
this.$broadcast('refresh') // 這里提供了一個(gè)表格刷新功能
}
},
components: {
bootPage
},
events: {
// 分頁(yè)組件傳回的表格數(shù)據(jù)(這里即為服務(wù)器傳回的數(shù)據(jù))
'data' (data) {
this.tableList = data
}
}
}
</script>
注:服務(wù)器除了傳給組件表格的數(shù)組內(nèi)容,還需一個(gè)總頁(yè)數(shù)的鍵名,名為page_num
(2)組件源碼
至于分頁(yè)的實(shí)現(xiàn)源碼這里的就不展示了,所有源碼我都上傳到了我的github,地址為:https://github.com/luozhihao/BootPage
這里事先提個(gè)醒:因?yàn)檫@個(gè)組件是我用幾個(gè)小時(shí)趕出來(lái)的,所以對(duì)于Vue組件的編寫(xiě)格式和規(guī)范肯定是考慮不周的,沒(méi)有完全獨(dú)立出來(lái),所以自覺(jué)填坑咯,這里只作分享。
當(dāng)然你也可以隨意的修改組件的代碼來(lái)適合自己項(xiàng)目的使用,畢竟實(shí)現(xiàn)大而全的分頁(yè)組件還是比較復(fù)雜的。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助。