button.js
5 years ago
collapse.js
5 years ago
component-wrapper.js
5 years ago
list-table-heading.js
5 years ago
list-table-item.js
5 years ago
list-table.js
5 years ago
notice-component.js
5 years ago
notice.js
5 years ago
pagination.js
5 years ago
popup.js
5 years ago
repeater-item.js
5 years ago
repeater.js
5 years ago
tabs-panel.js
5 years ago
tabs.js
5 years ago
title.js
5 years ago
pagination.js
184 lines
| 1 | import { oneOf } from '../../utils/assist'; |
| 2 | import { checkConditions } from '../../mixins/check-conditions'; |
| 3 | |
| 4 | const Pagination = { |
| 5 | name: 'cx-vui-pagination', |
| 6 | template: '#cx-vui-pagination', |
| 7 | mixins: [ checkConditions ], |
| 8 | props: { |
| 9 | current: { |
| 10 | type: Number, |
| 11 | default: 1 |
| 12 | }, |
| 13 | total: { |
| 14 | type: Number, |
| 15 | default: 0 |
| 16 | }, |
| 17 | pageSize: { |
| 18 | type: Number, |
| 19 | default: 10 |
| 20 | }, |
| 21 | prevText: { |
| 22 | type: String, |
| 23 | default: '' |
| 24 | }, |
| 25 | nextText: { |
| 26 | type: String, |
| 27 | default: '' |
| 28 | }, |
| 29 | customCss: { |
| 30 | type: String, |
| 31 | default: '' |
| 32 | }, |
| 33 | elementId: { |
| 34 | type: String, |
| 35 | default: '' |
| 36 | }, |
| 37 | conditions: { |
| 38 | type: Array, |
| 39 | default() { |
| 40 | return []; |
| 41 | } |
| 42 | }, |
| 43 | }, |
| 44 | |
| 45 | data() { |
| 46 | return { |
| 47 | baseClass: 'cx-vui-pagination', |
| 48 | currentPage: this.current, |
| 49 | currentPageSize: this.pageSize |
| 50 | }; |
| 51 | }, |
| 52 | |
| 53 | watch: { |
| 54 | total ( val ) { |
| 55 | let maxPage = Math.ceil( val / this.currentPageSize ); |
| 56 | |
| 57 | if ( maxPage < this.currentPage ) { |
| 58 | this.currentPage = ( maxPage === 0 ? 1 : maxPage ); |
| 59 | } |
| 60 | }, |
| 61 | current ( val ) { |
| 62 | this.currentPage = val; |
| 63 | }, |
| 64 | pageSize ( val ) { |
| 65 | this.currentPageSize = val; |
| 66 | } |
| 67 | }, |
| 68 | |
| 69 | computed: { |
| 70 | classesList() { |
| 71 | |
| 72 | let classesList = [ |
| 73 | this.baseClass, |
| 74 | ]; |
| 75 | |
| 76 | if ( this.customCss ) { |
| 77 | classesList.push( this.customCss ); |
| 78 | } |
| 79 | |
| 80 | return classesList; |
| 81 | }, |
| 82 | |
| 83 | prevClasses () { |
| 84 | return [ |
| 85 | `${this.baseClass}-item`, |
| 86 | `${this.baseClass}-prev`, |
| 87 | { |
| 88 | [`${this.baseClass}-disabled`]: this.currentPage === 1 || false, |
| 89 | [`${this.baseClass}-custom-text`]: this.prevText !== '' |
| 90 | } |
| 91 | ]; |
| 92 | }, |
| 93 | |
| 94 | nextClasses () { |
| 95 | return [ |
| 96 | `${this.baseClass}-item`, |
| 97 | `${this.baseClass}-next`, |
| 98 | { |
| 99 | [`${this.baseClass}-disabled`]: this.currentPage === this.allPages || false, |
| 100 | [`${this.baseClass}-custom-text`]: this.nextText !== '' |
| 101 | } |
| 102 | ]; |
| 103 | }, |
| 104 | |
| 105 | firstPageClasses () { |
| 106 | return [ |
| 107 | `${this.baseClass}-item`, |
| 108 | { |
| 109 | [`${this.baseClass}-item-active`]: this.currentPage === 1 |
| 110 | } |
| 111 | ]; |
| 112 | }, |
| 113 | |
| 114 | lastPageClasses () { |
| 115 | return [ |
| 116 | `${this.baseClass}-item`, |
| 117 | { |
| 118 | [`${this.baseClass}-item-active`]: this.currentPage === this.allPages |
| 119 | } |
| 120 | ]; |
| 121 | }, |
| 122 | |
| 123 | allPages () { |
| 124 | const allPage = Math.ceil( this.total / this.currentPageSize ); |
| 125 | |
| 126 | return ( allPage === 0 ) ? 1 : allPage; |
| 127 | }, |
| 128 | }, |
| 129 | methods: { |
| 130 | changePage ( page ) { |
| 131 | |
| 132 | if ( this.currentPage !== page ) { |
| 133 | |
| 134 | this.currentPage = page; |
| 135 | this.$emit( 'update:current', page ); |
| 136 | this.$emit( 'on-change', page ); |
| 137 | } |
| 138 | }, |
| 139 | |
| 140 | prev () { |
| 141 | const current = this.currentPage; |
| 142 | |
| 143 | if ( current <= 1 ) { |
| 144 | return false; |
| 145 | } |
| 146 | |
| 147 | this.changePage( current - 1 ); |
| 148 | }, |
| 149 | |
| 150 | next () { |
| 151 | const current = this.currentPage; |
| 152 | |
| 153 | if ( current >= this.allPages ) { |
| 154 | return false; |
| 155 | } |
| 156 | |
| 157 | this.changePage(current + 1); |
| 158 | }, |
| 159 | |
| 160 | fastPrev () { |
| 161 | |
| 162 | const page = this.currentPage - 5; |
| 163 | |
| 164 | if ( page > 0 ) { |
| 165 | this.changePage( page ); |
| 166 | } else { |
| 167 | this.changePage( 1 ); |
| 168 | } |
| 169 | }, |
| 170 | |
| 171 | fastNext () { |
| 172 | const page = this.currentPage + 5; |
| 173 | |
| 174 | if ( page > this.allPages ) { |
| 175 | this.changePage( this.allPages ); |
| 176 | } else { |
| 177 | this.changePage( page ); |
| 178 | } |
| 179 | }, |
| 180 | }, |
| 181 | }; |
| 182 | |
| 183 | export default Pagination; |
| 184 |