//組件引入import pager from '../../component/pager/pager.vue' //組件調用聲明components:{ pager} //參數data () { return { pageSize: 30, //總頁數 -- 後臺傳值 pageNo: 2 //當 ...
翻頁組件 -- 子組件
<template>
<div class="pager-wrapper" ref="pager">
<div class="pager-box">
<a class="pager-prev" :class="{'pager-disabled':prevDisable}" href="javascript:void(0)" @click="jumpPrev()">上一頁</a>
<template v-for="(i,index) in pageSize">
<span v-if="i==pageNo" class="pager-curr" :key="index">
<em class="pager-em"></em>
<em>{{i}}</em>
</span>
<a v-else-if="pageNo<5&&(i)<6" href="javascript:void(0)" @click="jump(i)" :key="index">
{{i}}
</a>
<a v-else-if="pageSize<7||i==1||i==pageSize||(pageNo-2<=i&&i<=pageNo+2)" href="javascript:void(0)" @click="jump(i)" :key="index">
{{i}}
</a>
<template v-else>
<span v-if="pageNo<5&&i==6" class="pager-spr" :key="index">…</span>
<span v-if="pageNo==4&&i==7" class="pager-spr" :key="index">…</span>
<span v-if="pageNo>4&&i==pageNo-3" class="pager-spr" :key="index">…</span>
<span v-if="pageNo>4&&i==pageNo+3" class="pager-spr" :key="index">…</span>
</template>
</template>
<a class="pager-next" :class="{'pager-disabled':nextDisable}" href="javascript:void(0)" @click="jumpNext()">下一頁</a>
</div>
<div class="pager-input">
<div>跳轉到:</div>
<input type="text" v-model="jumpPage">
<a class="pager-btn-go" href="javascript:void(0)" @click="Go()">GO</a>
</div>
</div>
</template>
<script>
export default {
model: { // 通過v-model傳過來的參數
prop: 'pageNo',
event: 'jumpPage'
},
props: {
pageSize: {
type: Number,
default: 1
},
pageNo: { // 通過v-model傳過來的參數
type: Number,
default: 1
}
},
data () {
return {
jumpPage: '' // 避免操作props參數
}
},
computed: {
prevDisable: function () { // “上一頁”按鈕是否可點
if (this.pageNo > 1) {
return false
} else {
return true
}
},
nextDisable: function () { // “下一頁”按鈕是否可點
if (this.pageNo < this.pageSize && this.pageSize > 1) {
return false
} else {
return true
}
}
},
methods: {
jumpPrev: function () { // 點擊上一頁
if (this.pageNo === 1) {
return false
} else {
this.$emit('jumpPage', this.pageNo - 1)
}
},
jumpNext: function () { // 點擊下一頁
if (this.pageNo === this.pageSize) {
return false
} else {
this.$emit('jumpPage', this.pageNo + 1) // 修改當前頁碼
}
},
jump: function (id) { // 直接跳轉
if (id > this.pageSize) {
id = this.pageSize
}
this.jumpPage = ''
this.$emit('jumpPage', id) // 修改當前頁碼
},
Go: function () {
if (this.jumpPage === '') { // 判空處理
return false
} else if (/^\d*$/.test(parseInt(this.jumpPage))) { // 填寫數字才能跳轉
this.jump(parseInt(this.jumpPage))
this.jumpPage = ''
} else {
this.jumpPage = ''
return false
}
}
}
}
</script>
<style scoped lang="stylus" type="text/stylus">
@import "~styles/varibles.styl"
.pager-wrapper
float right
display flex
flex-direction row
height 38px
.pager-box
margin-top -15px
a,span
display inline-block
width 38px
height 38px
margin 0 2px
border 1px solid #E5E5E5
color #bdbdbd
text-align center
font 14px/38px ""
em
color $bgColor
.pager-prev,.pager-next
width 78px
.pager-input
display flex
flex-direction row
height 38px
margin-left 20px
font 14px/40px ""
color #bdbdbd
input,.pager-btn-go
display inline-block
width 40px
height 40px
border 1px solid #E5E5E5
box-sizing content-box
outline none
text-align center
</style>
父組件
<pager :pageSize="pageSize" v-model="pageNo" @jumpPage="jump"></pager>
//組件引入
import pager from '../../component/pager/pager.vue'
//組件調用聲明
components:{ pager}
//參數
data () {
return {
pageSize: 30, //總頁數 -- 後臺傳值
pageNo: 2 //當前頁
}
}
//接收跳轉事件
methods:{
jump (id) {
console.log(id)
}
}