Blame view

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