|
26 | 26 | </CLink> |
27 | 27 | </li> |
28 | 28 | <li |
29 | | - v-if="showFirstDots" |
| 29 | + v-if="beforeDots" |
30 | 30 | role="separator" |
31 | 31 | class="c-page-item c-disabled c-d-none c-d-sm-flex" |
32 | 32 | > |
|
36 | 36 | <li |
37 | 37 | v-for="(item, index) in items" |
38 | 38 | :key="index" |
39 | | - :class="setStyle(item)" |
| 39 | + :class="[{ 'c-active': activePage === item }, 'c-page-item']" |
40 | 40 | > |
41 | 41 | <CLink |
42 | 42 | class="c-page-link" |
|
48 | 48 | </li> |
49 | 49 |
|
50 | 50 | <li |
51 | | - v-if="showLastDots" |
| 51 | + v-if="afterDots" |
52 | 52 | role="separator" |
53 | 53 | class="c-page-item c-disabled c-d-none c-d-sm-flex" |
54 | 54 | > |
|
131 | 131 | }, |
132 | 132 | data () { |
133 | 133 | return { |
134 | | - showFirstDots: false, |
135 | | - showLastDots: false, |
136 | 134 | rwd: this.size, |
137 | 135 | erd: elementResizeDetectorMaker() |
138 | 136 | } |
139 | 137 | }, |
140 | 138 | watch: { |
141 | | - // activePage (val) { |
142 | | - // this.page = val |
143 | | - // }, |
144 | 139 | pages (val) { |
145 | 140 | if (val < this.activePage) { |
146 | 141 | this.$emit('update:activePage', val) |
|
163 | 158 | return `c-pagination c-pagination-${this.rwd} c-justify-content-${this.align} ` |
164 | 159 | }, |
165 | 160 | dots () { |
166 | | - return this.hideDots || this.limit < 5 ? false : true |
| 161 | + return !this.hideDots && this.limit > 4 && this.limit < this.pages |
| 162 | + }, |
| 163 | + maxPrevItems () { |
| 164 | + return Math.floor((this.limit - 1) / 2) |
| 165 | + }, |
| 166 | + maxNextItems () { |
| 167 | + return Math.ceil((this.limit - 1) / 2) |
| 168 | + }, |
| 169 | + beforeDots () { |
| 170 | + return this.dots && this.activePage > this.maxPrevItems + 1 |
| 171 | + }, |
| 172 | + afterDots () { |
| 173 | + return this.dots && this.activePage < this.pages - this.maxNextItems |
| 174 | + }, |
| 175 | + computedLimit () { |
| 176 | + return this.limit - this.afterDots - this.beforeDots |
| 177 | + }, |
| 178 | + range () { |
| 179 | + return this.activePage + this.maxNextItems |
| 180 | + }, |
| 181 | + lastItem () { |
| 182 | + return this.range >= this.pages ? this.pages : this.range-this.afterDots |
167 | 183 | }, |
168 | 184 | items () { |
169 | | - let maxPrevItems = Math.floor((this.limit - 1) / 2) |
170 | | - let maxNextItems = Math.ceil((this.limit - 1) / 2) |
171 | | - let items = [] |
172 | | -
|
173 | | - if (!this.dots) { |
174 | | - this.showFirstDots = false |
175 | | - this.showLastDots = false |
176 | | - if (this.activePage <= maxPrevItems) { |
177 | | - for (let i = 1; i <= this.limit; i++) |
178 | | - items.push(i) |
179 | | - } else { |
180 | | - let max = this.activePage + maxNextItems > this.pages ? this.pages : this.activePage + maxNextItems |
181 | | - for (let i = max - this.limit + 1; i <= max; i++) |
182 | | - items.push(i) |
183 | | - } |
184 | | - return items |
185 | | - } |
186 | | -
|
187 | | - if (this.limit >= this.pages) { |
188 | | - this.showFirstDots = false |
189 | | - this.showLastDots = false |
190 | | - for (let i = 1; i <= this.pages; i++) |
191 | | - items.push(i) |
192 | | - return items |
| 185 | + if (this.activePage - this.maxPrevItems <= 1 ) { |
| 186 | + return Array.from({ length: this.computedLimit }, (v, i) => i + 1 ) |
| 187 | + } else { |
| 188 | + return Array.from({length: this.computedLimit}, (v, i) => { |
| 189 | + return this.lastItem - i |
| 190 | + }).reverse() |
193 | 191 | } |
194 | | -
|
195 | | - if (this.activePage <= maxPrevItems) { |
196 | | - this.showFirstDots = false |
197 | | - this.showLastDots = true |
198 | | - for (let i = 1; i <= this.limit - 1; i++) |
199 | | - items.push(i) |
200 | | - return items |
201 | | - } |
202 | | -
|
203 | | - if (this.activePage > maxPrevItems && this.activePage < this.pages - maxNextItems) { |
204 | | - this.showFirstDots = true |
205 | | - this.showLastDots = true |
206 | | - for (let i = 1 ; i < this.limit - 1 ; i++) |
207 | | - items.push(this.activePage - maxPrevItems + i) |
208 | | - return items |
209 | | - } |
210 | | -
|
211 | | - if (this.activePage > maxPrevItems && this.activePage >= this.pages - maxNextItems) { |
212 | | - this.showFirstDots = true |
213 | | - this.showLastDots = false |
214 | | - for (let i = this.pages - this.limit + 2 ; i <= this.pages; i++) |
215 | | - items.push(i) |
216 | | - return items |
217 | | - } |
218 | | - }, |
| 192 | + } |
219 | 193 | }, |
220 | 194 | methods: { |
221 | 195 | onWrapperResize (el) { |
|
224 | 198 | this.rwd = 'md' : this.rwd = 'sm' |
225 | 199 | }, |
226 | 200 | setPage (number) { |
227 | | - if(number !== this.activePage) |
| 201 | + if (number !== this.activePage) { |
228 | 202 | this.$emit('update:activePage', number) |
229 | | - }, |
230 | | - setStyle (item) { |
231 | | - if(this.activePage === item) { |
232 | | - return 'c-page-item c-active' |
233 | 203 | } |
234 | | - return 'c-page-item' |
235 | 204 | } |
236 | 205 | } |
237 | 206 | } |
|
0 commit comments