123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391 |
- <template>
- <view class="uni-pagination">
- <!-- #ifndef APP-NVUE -->
- <view class="uni-pagination__total is-phone-hide">共 {{ total }} 条</view>
- <!-- #endif -->
- <view
- class="uni-pagination__btn"
- :class="currentIndex === 1 ? 'uni-pagination--disabled' : 'uni-pagination--enabled'"
- :hover-class="currentIndex === 1 ? '' : 'uni-pagination--hover'"
- :hover-start-time="20"
- :hover-stay-time="70"
- @click="clickLeft"
- >
- <template v-if="showIcon === true || showIcon === 'true'">
- <uni-icons color="#666" size="16" type="arrowleft" />
- </template>
- <template v-else>
- <text class="uni-pagination__child-btn">{{ prevText }}</text>
- </template>
- </view>
- <view class="uni-pagination__num uni-pagination__num-flex-none">
- <view class="uni-pagination__num-current">
- <text class="uni-pagination__num-current-text is-pc-hide" style="color:#409EFF">{{ currentIndex }}</text>
- <text class="uni-pagination__num-current-text is-pc-hide">/{{ maxPage || 0 }}</text>
- <!-- #ifndef APP-NVUE -->
- <view
- v-for="(item, index) in paper"
- :key="index"
- :class="{ 'page--active': item === currentIndex }"
- class="uni-pagination__num-tag tag--active is-phone-hide"
- @click.top="selectPage(item, index)"
- >
- <text>{{ item }}</text>
- </view>
- <!-- #endif -->
-
- </view>
- </view>
- <view
- class="uni-pagination__btn"
- :class="currentIndex >= maxPage ? 'uni-pagination--disabled' : 'uni-pagination--enabled'"
- :hover-class="currentIndex === maxPage ? '' : 'uni-pagination--hover'"
- :hover-start-time="20"
- :hover-stay-time="70"
- @click="clickRight"
- >
- <template v-if="showIcon === true || showIcon === 'true'">
- <uni-icons color="#666" size="16" type="arrowright" />
- </template>
- <template v-else>
- <text class="uni-pagination__child-btn">{{ nextText }}</text>
- </template>
- </view>
- </view>
- </template>
- <script>
- /**
- * Pagination 分页器
- * @description 分页器组件,用于展示页码、请求数据等
- * @tutorial https://ext.dcloud.net.cn/plugin?id=32
- * @property {String} prevText 左侧按钮文字
- * @property {String} nextText 右侧按钮文字
- * @property {Number} current 当前页
- * @property {Number} total 数据总量
- * @property {Number} pageSize 每页数据量
- * @property {Number} showIcon = [true|false] 是否以 icon 形式展示按钮
- * @event {Function} change 点击页码按钮时触发 ,e={type,current} current为当前页,type值为:next/prev,表示点击的是上一页还是下一个
- */
- export default {
- name: 'UniPagination',
- props: {
- value: {
- type: [Number, String],
- default: 1
- },
- prevText: {
- type: String,
- default: '上一页'
- },
- nextText: {
- type: String,
- default: '下一页'
- },
- current: {
- type: [Number, String],
- default: 1
- },
- total: {
- // 数据总量
- type: [Number, String],
- default: 0
- },
- pageSize: {
- // 每页数据量
- type: [Number, String],
- default: 10
- },
- showIcon: {
- // 是否以 icon 形式展示按钮
- type: [Boolean, String],
- default: false
- },
- pagerCount: {
- type: Number,
- default: 7
- }
- },
- data() {
- return {
- currentIndex: 1,
- paperData: []
- }
- },
- computed: {
- maxPage() {
- let maxPage = 1
- let total = Number(this.total)
- let pageSize = Number(this.pageSize)
- if (total && pageSize) {
- maxPage = Math.ceil(total / pageSize)
- }
- return maxPage
- },
- paper() {
- const num = this.currentIndex
- // TODO 最大页数
- const pagerCount = this.pagerCount
- // const total = 181
- const total = this.total
- const pageSize = this.pageSize
- let totalArr = []
- let showPagerArr = []
- let pagerNum = Math.ceil(total / pageSize)
- for (let i = 0; i < pagerNum; i++) {
- totalArr.push(i + 1)
- }
- showPagerArr.push(1)
- const totalNum = totalArr[totalArr.length - (pagerCount + 1) / 2]
- totalArr.forEach((item, index) => {
- if ((pagerCount + 1) / 2 >= num) {
- if (item < pagerCount + 1 && item > 1) {
- showPagerArr.push(item)
- }
- } else if (num + 2 <= totalNum) {
- if (item > num - (pagerCount + 1) / 2 && item < num + (pagerCount + 1) / 2) {
- showPagerArr.push(item)
- }
- } else {
- if ((item > num - (pagerCount + 1) / 2 || pagerNum - pagerCount < item) && item < totalArr[totalArr.length - 1]) {
- showPagerArr.push(item)
- }
- }
- })
- if (pagerNum > pagerCount) {
- if ((pagerCount + 1) / 2 >= num) {
- showPagerArr[showPagerArr.length - 1] = '...'
- } else if (num + 2 <= totalNum) {
- showPagerArr[1] = '...'
- showPagerArr[showPagerArr.length - 1] = '...'
- } else {
- showPagerArr[1] = '...'
- }
- showPagerArr.push(totalArr[totalArr.length - 1])
- } else {
- if ((pagerCount + 1) / 2 >= num) {
- } else if (num + 2 <= totalNum) {
- } else {
- showPagerArr.shift()
- showPagerArr.push(totalArr[totalArr.length - 1])
- }
- }
- return showPagerArr
- }
- },
- watch: {
- current(val) {
- this.currentIndex = val
- },
- value(val) {
- if (val < 1) {
- this.currentIndex = 1
- } else {
- this.currentIndex = val
- }
- }
- },
- created() {
- this.currentIndex = +this.value
- },
- methods: {
- // 选择标签
- selectPage(e, index) {
- if (parseInt(e)) {
- this.currentIndex = e
- this.change('current')
- } else {
- let pagerNum = Math.ceil(this.total / this.pageSize)
- // let pagerNum = Math.ceil(181 / this.pageSize)
- // 上一页
- if (index <= 1) {
- if (this.currentIndex - 5 > 1) {
- this.currentIndex -= 5
- } else {
- this.currentIndex = 1
- }
- return
- }
- // 下一页
- if (index >= 6) {
- if (this.currentIndex + 5 > pagerNum) {
- this.currentIndex = pagerNum
- } else {
- this.currentIndex += 5
- }
- return
- }
- }
- },
- clickLeft() {
- if (Number(this.currentIndex) === 1) {
- return
- }
- this.currentIndex -= 1
- this.change('prev')
- },
- clickRight() {
- if (Number(this.currentIndex) >= this.maxPage) {
- return
- }
- this.currentIndex += 1
- this.change('next')
- },
- change(e) {
- this.$emit('input', this.currentIndex)
- this.$emit('change', {
- type: e,
- current: this.currentIndex
- })
- }
- }
- }
- </script>
- <style lang="scss" scoped>
- .uni-pagination {
- /* #ifndef APP-NVUE */
- display: flex;
- /* #endif */
- position: relative;
- overflow: hidden;
- flex-direction: row;
- justify-content: center;
- align-items: center;
- }
- .uni-pagination__total {
- font-size: 14px;
- color: #999;
- margin-right: 15px;
- }
- .uni-pagination__btn {
- /* #ifndef APP-NVUE */
- display: flex;
- cursor: pointer;
- /* #endif */
- padding: 0 8px;
- line-height: 30px;
- font-size: $uni-font-size-base;
- position: relative;
- background-color: #f4f4f5;
- flex-direction: row;
- justify-content: center;
- align-items: center;
- text-align: center;
- // border-width: 1px;
- // border-style: solid;
- // border-color: $uni-border-color;
- }
- .uni-pagination__child-btn {
- /* #ifndef APP-NVUE */
- display: flex;
- /* #endif */
- font-size: $uni-font-size-base;
- position: relative;
- flex-direction: row;
- justify-content: center;
- align-items: center;
- text-align: center;
- }
- .uni-pagination__num {
- /* #ifndef APP-NVUE */
- display: flex;
- /* #endif */
- flex: 1;
- flex-direction: row;
- justify-content: center;
- align-items: center;
- height: 30px;
- line-height: 30px;
- font-size: $uni-font-size-base;
- color: $uni-text-color;
- margin: 0 5px;
- }
- .uni-pagination__num-tag {
- /* #ifdef H5 */
- cursor: pointer;
- min-width: 30px;
- /* #endif */
- margin: 0 5px;
- height: 30px;
- text-align: center;
- line-height: 30px;
- // border: 1px red solid;
- color: #666;
- // border-width: 1px;
- // border-style: solid;
- // border-color: $uni-border-color;
- }
- .uni-pagination__num-current {
- /* #ifndef APP-NVUE */
- display: flex;
- /* #endif */
- flex-direction: row;
- }
- .uni-pagination__num-current-text {
- font-size: 15px;
- }
- .uni-pagination--enabled {
- color: #333333;
- opacity: 1;
- }
- .uni-pagination--disabled {
- opacity: 0.5;
- /* #ifdef H5 */
- cursor: default;
- /* #endif */
- }
- .uni-pagination--hover {
- color: rgba(0, 0, 0, 0.6);
- background-color: $uni-bg-color-hover;
- }
- .tag--active:hover {
- color: $uni-color-primary;
- }
- .page--active {
- color: #fff;
- background-color: $uni-color-primary;
- }
- .page--active:hover {
- color: #fff;
- }
- /* #ifndef APP-NVUE */
- .is-pc-hide {
- display: block;
- }
- .is-phone-hide {
- display: none;
- }
- @media screen and (min-width: 450px) {
- .is-pc-hide {
- display: none;
- }
- .is-phone-hide {
- display: block;
- }
- .uni-pagination__num-flex-none {
- flex: none;
- }
- }
- /* #endif */
- </style>
|