Blame view

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