Blame view

src/components/page/page.vue 11.1 KB
7fa943eb   梁灏   init
1
  <template>
c9080021   梁灏   update readme
2
      <ul :class="simpleWrapClasses" :style="styles" v-if="simple">
7fa943eb   梁灏   init
3
          <li
012cbf28   梁灏   update locale
4
              :title="t('i.page.prev')"
7fa943eb   梁灏   init
5
6
              :class="prevClasses"
              @click="prev">
1c871a35   梁灏   update Page Icons
7
              <a><i class="ivu-icon ivu-icon-ios-arrow-back"></i></a>
7fa943eb   梁灏   init
8
          </li>
87c51597   梁灏   support Page
9
          <div :class="simplePagerClasses" :title="currentPage + '/' + allPages">
7fa943eb   梁灏   init
10
11
              <input
                  type="text"
87c51597   梁灏   support Page
12
                  :value="currentPage"
c17c5ad6   Sergio Crisostomo   normalize autocom...
13
14
                  autocomplete="off"
                  spellcheck="false"
7fa943eb   梁灏   init
15
16
17
18
19
20
21
                  @keydown="keyDown"
                  @keyup="keyUp"
                  @change="keyUp">
              <span>/</span>
              {{ allPages }}
          </div>
          <li
012cbf28   梁灏   update locale
22
              :title="t('i.page.next')"
7fa943eb   梁灏   init
23
24
              :class="nextClasses"
              @click="next">
1c871a35   梁灏   update Page Icons
25
              <a><i class="ivu-icon ivu-icon-ios-arrow-forward"></i></a>
7fa943eb   梁灏   init
26
27
          </li>
      </ul>
c9080021   梁灏   update readme
28
      <ul :class="wrapClasses" :style="styles" v-else>
d6342fe1   jingsam   fixed ie bug
29
          <span :class="[prefixCls + '-total']" v-if="showTotal">
012cbf28   梁灏   update locale
30
              <slot>{{ t('i.page.total') }} {{ total }} <template v-if="total <= 1">{{ t('i.page.item') }}</template><template v-else>{{ t('i.page.items') }}</template></slot>
7fa943eb   梁灏   init
31
32
          </span>
          <li
012cbf28   梁灏   update locale
33
              :title="t('i.page.prev')"
7fa943eb   梁灏   init
34
35
              :class="prevClasses"
              @click="prev">
1c871a35   梁灏   update Page Icons
36
              <a><template v-if="prevText !== ''">{{ prevText }}</template><i v-else class="ivu-icon ivu-icon-ios-arrow-back"></i></a>
7fa943eb   梁灏   init
37
          </li>
012cbf28   梁灏   update locale
38
          <li title="1" :class="firstPageClasses" @click="changePage(1)"><a>1</a></li>
2c8be006   何志勇   分页向前(后)跳转5页有歧义
39
40
          <li :title="t('i.page.prev5')" v-if="currentPage > 5" :class="[prefixCls + '-item-jump-prev']" @click="fastPrev"><a><i class="ivu-icon ivu-icon-ios-arrow-back"></i></a></li>
          <li :title="currentPage - 3" v-if="currentPage === 5" :class="[prefixCls + '-item']" @click="changePage(currentPage - 3)"><a>{{ currentPage - 3 }}</a></li>
87c51597   梁灏   support Page
41
42
43
44
45
          <li :title="currentPage - 2" v-if="currentPage - 2 > 1" :class="[prefixCls + '-item']" @click="changePage(currentPage - 2)"><a>{{ currentPage - 2 }}</a></li>
          <li :title="currentPage - 1" v-if="currentPage - 1 > 1" :class="[prefixCls + '-item']" @click="changePage(currentPage - 1)"><a>{{ currentPage - 1 }}</a></li>
          <li :title="currentPage" v-if="currentPage != 1 && currentPage != allPages" :class="[prefixCls + '-item',prefixCls + '-item-active']"><a>{{ currentPage }}</a></li>
          <li :title="currentPage + 1" v-if="currentPage + 1 < allPages" :class="[prefixCls + '-item']" @click="changePage(currentPage + 1)"><a>{{ currentPage + 1 }}</a></li>
          <li :title="currentPage + 2" v-if="currentPage + 2 < allPages" :class="[prefixCls + '-item']" @click="changePage(currentPage + 2)"><a>{{ currentPage + 2 }}</a></li>
2c8be006   何志勇   分页向前(后)跳转5页有歧义
46
47
          <li :title="currentPage + 3" v-if="allPages - currentPage === 4" :class="[prefixCls + '-item']" @click="changePage(currentPage + 3)"><a>{{ currentPage + 3 }}</a></li>
          <li :title="t('i.page.next5')" v-if="allPages - currentPage >= 5" :class="[prefixCls + '-item-jump-next']" @click="fastNext"><a><i class="ivu-icon ivu-icon-ios-arrow-forward"></i></a></li>
012cbf28   梁灏   update locale
48
          <li :title="allPages" v-if="allPages > 1" :class="lastPageClasses" @click="changePage(allPages)"><a>{{ allPages }}</a></li>
7fa943eb   梁灏   init
49
          <li
012cbf28   梁灏   update locale
50
              :title="t('i.page.next')"
7fa943eb   梁灏   init
51
52
              :class="nextClasses"
              @click="next">
1c871a35   梁灏   update Page Icons
53
              <a><template v-if="nextText !== ''">{{ nextText }}</template><i v-else class="ivu-icon ivu-icon-ios-arrow-forward"></i></a>
7fa943eb   梁灏   init
54
55
56
          </li>
          <Options
              :show-sizer="showSizer"
87c51597   梁灏   support Page
57
              :page-size="currentPageSize"
7fa943eb   梁灏   init
58
              :page-size-opts="pageSizeOpts"
f89dd9c2   梁灏   Paeg、Select add p...
59
              :placement="placement"
a7a79014   梁灏   fixed #3112
60
              :transfer="transfer"
7fa943eb   梁灏   init
61
              :show-elevator="showElevator"
87c51597   梁灏   support Page
62
63
              :_current.once="currentPage"
              :current="currentPage"
7fa943eb   梁灏   init
64
              :all-pages="allPages"
6932b4d7   梁灏   update Page compo...
65
              :is-small="isSmall"
7fa943eb   梁灏   init
66
67
68
69
70
71
72
73
              @on-size="onSize"
              @on-page="onPage">
          </Options>
      </ul>
  </template>
  <script>
      import { oneOf } from '../../utils/assist';
      import Options from './options.vue';
012cbf28   梁灏   update locale
74
      import Locale from '../../mixins/locale';
7fa943eb   梁灏   init
75
76
77
78
  
      const prefixCls = 'ivu-page';
  
      export default {
87c51597   梁灏   support Page
79
          name: 'Page',
012cbf28   梁灏   update locale
80
          mixins: [ Locale ],
7fa943eb   梁灏   init
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
          components: { Options },
          props: {
              current: {
                  type: Number,
                  default: 1
              },
              total: {
                  type: Number,
                  default: 0
              },
              pageSize: {
                  type: Number,
                  default: 10
              },
              pageSizeOpts: {
                  type: Array,
                  default () {
b0893113   jingsam   :art: add eslint
98
                      return [10, 20, 30, 40];
7fa943eb   梁灏   init
99
100
                  }
              },
f89dd9c2   梁灏   Paeg、Select add p...
101
102
103
104
105
106
              placement: {
                  validator (value) {
                      return oneOf(value, ['top', 'bottom']);
                  },
                  default: 'bottom'
              },
a7a79014   梁灏   fixed #3112
107
108
              transfer: {
                  type: Boolean,
81b641ee   梁灏   Page support glob...
109
                  default () {
fe5ffd7f   梁灏   fixed #4196 #4165
110
                      return !this.$IVIEW || this.$IVIEW.transfer === '' ? false : this.$IVIEW.transfer;
81b641ee   梁灏   Page support glob...
111
                  }
a7a79014   梁灏   fixed #3112
112
              },
7fa943eb   梁灏   init
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
              size: {
                  validator (value) {
                      return oneOf(value, ['small']);
                  }
              },
              simple: {
                  type: Boolean,
                  default: false
              },
              showTotal: {
                  type: Boolean,
                  default: false
              },
              showElevator: {
                  type: Boolean,
                  default: false
              },
              showSizer: {
                  type: Boolean,
                  default: false
e0cd7f90   梁灏   fixed #134
133
              },
87c51597   梁灏   support Page
134
              className: {
e0cd7f90   梁灏   fixed #134
135
136
                  type: String
              },
c9080021   梁灏   update readme
137
              styles: {
e0cd7f90   梁灏   fixed #134
138
                  type: Object
ac50d539   梁灏   Page add prev-tex...
139
140
141
142
143
144
145
146
              },
              prevText: {
                  type: String,
                  default: ''
              },
              nextText: {
                  type: String,
                  default: ''
7fa943eb   梁灏   init
147
148
149
150
              }
          },
          data () {
              return {
87c51597   梁灏   support Page
151
152
153
                  prefixCls: prefixCls,
                  currentPage: this.current,
                  currentPageSize: this.pageSize
b0893113   jingsam   :art: add eslint
154
              };
7fa943eb   梁灏   init
155
          },
87c51597   梁灏   support Page
156
          watch: {
ea060d98   Rijn   fix #1543
157
158
159
160
161
162
              total (val) {
                  let maxPage = Math.ceil(val / this.currentPageSize);
                  if (maxPage < this.currentPage && maxPage > 0) {
                      this.currentPage = maxPage;
                  }
              },
87c51597   梁灏   support Page
163
164
165
166
167
168
169
              current (val) {
                  this.currentPage = val;
              },
              pageSize (val) {
                  this.currentPageSize = val;
              }
          },
7fa943eb   梁灏   init
170
          computed: {
6932b4d7   梁灏   update Page compo...
171
172
173
              isSmall () {
                  return !!this.size;
              },
7fa943eb   梁灏   init
174
              allPages () {
87c51597   梁灏   support Page
175
                  const allPage = Math.ceil(this.total / this.currentPageSize);
4935594e   梁灏   fixed #21
176
                  return (allPage === 0) ? 1 : allPage;
7fa943eb   梁灏   init
177
178
179
180
              },
              simpleWrapClasses () {
                  return [
                      `${prefixCls}`,
e0cd7f90   梁灏   fixed #134
181
182
                      `${prefixCls}-simple`,
                      {
87c51597   梁灏   support Page
183
                          [`${this.className}`]: !!this.className
e0cd7f90   梁灏   fixed #134
184
                      }
b0893113   jingsam   :art: add eslint
185
                  ];
7fa943eb   梁灏   init
186
              },
93064e3b   梁灏   add Page UI
187
188
189
              simplePagerClasses () {
                  return `${prefixCls}-simple-pager`;
              },
7fa943eb   梁灏   init
190
191
192
193
              wrapClasses () {
                  return [
                      `${prefixCls}`,
                      {
87c51597   梁灏   support Page
194
                          [`${this.className}`]: !!this.className,
7fa943eb   梁灏   init
195
196
                          'mini': !!this.size
                      }
b0893113   jingsam   :art: add eslint
197
                  ];
7fa943eb   梁灏   init
198
199
200
201
202
              },
              prevClasses () {
                  return [
                      `${prefixCls}-prev`,
                      {
ac50d539   梁灏   Page add prev-tex...
203
204
                          [`${prefixCls}-disabled`]: this.currentPage === 1,
                          [`${prefixCls}-custom-text`]: this.prevText !== ''
7fa943eb   梁灏   init
205
                      }
b0893113   jingsam   :art: add eslint
206
                  ];
7fa943eb   梁灏   init
207
208
209
210
211
              },
              nextClasses () {
                  return [
                      `${prefixCls}-next`,
                      {
ac50d539   梁灏   Page add prev-tex...
212
213
                          [`${prefixCls}-disabled`]: this.currentPage === this.allPages,
                          [`${prefixCls}-custom-text`]: this.nextText !== ''
7fa943eb   梁灏   init
214
                      }
b0893113   jingsam   :art: add eslint
215
                  ];
d6342fe1   jingsam   fixed ie bug
216
217
218
219
220
              },
              firstPageClasses () {
                  return [
                      `${prefixCls}-item`,
                      {
87c51597   梁灏   support Page
221
                          [`${prefixCls}-item-active`]: this.currentPage === 1
d6342fe1   jingsam   fixed ie bug
222
                      }
b0893113   jingsam   :art: add eslint
223
                  ];
d6342fe1   jingsam   fixed ie bug
224
225
226
227
228
              },
              lastPageClasses () {
                  return [
                      `${prefixCls}-item`,
                      {
87c51597   梁灏   support Page
229
                          [`${prefixCls}-item-active`]: this.currentPage === this.allPages
d6342fe1   jingsam   fixed ie bug
230
                      }
b0893113   jingsam   :art: add eslint
231
                  ];
7fa943eb   梁灏   init
232
233
234
235
              }
          },
          methods: {
              changePage (page) {
87c51597   梁灏   support Page
236
237
                  if (this.currentPage != page) {
                      this.currentPage = page;
6355250e   Rijn   emit current upda...
238
                      this.$emit('update:current', page);
7fa943eb   梁灏   init
239
240
241
242
                      this.$emit('on-change', page);
                  }
              },
              prev () {
87c51597   梁灏   support Page
243
                  const current = this.currentPage;
7fa943eb   梁灏   init
244
245
246
247
248
249
                  if (current <= 1) {
                      return false;
                  }
                  this.changePage(current - 1);
              },
              next () {
87c51597   梁灏   support Page
250
                  const current = this.currentPage;
7fa943eb   梁灏   init
251
252
253
254
255
256
                  if (current >= this.allPages) {
                      return false;
                  }
                  this.changePage(current + 1);
              },
              fastPrev () {
87c51597   梁灏   support Page
257
                  const page = this.currentPage - 5;
7fa943eb   梁灏   init
258
259
260
261
262
263
264
                  if (page > 0) {
                      this.changePage(page);
                  } else {
                      this.changePage(1);
                  }
              },
              fastNext () {
87c51597   梁灏   support Page
265
                  const page = this.currentPage + 5;
7fa943eb   梁灏   init
266
267
268
269
270
271
272
                  if (page > this.allPages) {
                      this.changePage(this.allPages);
                  } else {
                      this.changePage(page);
                  }
              },
              onSize (pageSize) {
87c51597   梁灏   support Page
273
                  this.currentPageSize = pageSize;
07f400e5   梁灏   fixed #136
274
                  this.$emit('on-page-size-change', pageSize);
8409abb8   梁灏   update Page #759
275
                  this.changePage(1);
7fa943eb   梁灏   init
276
277
278
279
280
281
              },
              onPage (page) {
                  this.changePage(page);
              },
              keyDown (e) {
                  const key = e.keyCode;
6d2b7bcb   梁灏   fixed #1575
282
                  const condition = (key >= 48 && key <= 57) || (key >= 96 && key <= 105) || key === 8 || key === 37 || key === 39;
7fa943eb   梁灏   init
283
284
285
286
287
288
289
290
291
292
  
                  if (!condition) {
                      e.preventDefault();
                  }
              },
              keyUp (e) {
                  const key = e.keyCode;
                  const val = parseInt(e.target.value);
  
                  if (key === 38) {
b0893113   jingsam   :art: add eslint
293
                      this.prev();
7fa943eb   梁灏   init
294
                  } else if (key === 40) {
b0893113   jingsam   :art: add eslint
295
                      this.next();
6d2b7bcb   梁灏   fixed #1575
296
                  } else if (key === 13) {
7fa943eb   梁灏   init
297
298
299
300
                      let page = 1;
  
                      if (val > this.allPages) {
                          page = this.allPages;
6d2b7bcb   梁灏   fixed #1575
301
                      } else if (val <= 0 || !val) {
7fa943eb   梁灏   init
302
303
304
305
306
307
308
309
310
311
                          page = 1;
                      } else {
                          page = val;
                      }
  
                      e.target.value = page;
                      this.changePage(page);
                  }
              }
          }
b0893113   jingsam   :art: add eslint
312
      };
d6342fe1   jingsam   fixed ie bug
313
  </script>