Blame view

components/page/page.vue 7.38 KB
7fa943eb   梁灏   init
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
  <template>
      <ul :class="simpleWrapClasses" v-if="simple">
          <li
              title="上一页"
              :class="prevClasses"
              @click="prev">
              <a>←</a>
          </li>
          <div :title="current + '/' + allPages">
              <input
                  type="text"
                  :value="current"
                  @keydown="keyDown"
                  @keyup="keyUp"
                  @change="keyUp">
              <span>/</span>
              {{ allPages }}
          </div>
          <li
              title="下一页"
              :class="nextClasses"
              @click="next">
              <a>→</a>
          </li>
      </ul>
      <ul :class="wrapClasses" v-else>
          <span :class="[`${prefixCls}-total`]" v-if="showTotal">
              <slot>共 {{ total }} 条</slot>
          </span>
          <li
              title="上一页"
              :class="prevClasses"
              @click="prev">
              <a>←</a>
          </li>
          <li title="第一页" :class="[`${prefixCls}-item`,{[`${prefixCls}-item-active`]: current == 1}]" @click="changePage(1)"><a>1</a></li>
          <li title="向前 5 页" v-if="current - 3 > 1" :class="[`${prefixCls}-item-jump-prev`]" @click="fastPrev"><a>…</a></li>
          <li :title="current - 2" v-if="current - 2 > 1" :class="[`${prefixCls}-item`]" @click="changePage(current - 2)"><a>{{ current - 2 }}</a></li>
          <li :title="current - 1" v-if="current - 1 > 1" :class="[`${prefixCls}-item`]" @click="changePage(current - 1)"><a>{{ current - 1 }}</a></li>
          <li :title="current" v-if="current != 1 && current != allPages" :class="[`${prefixCls}-item`,`${prefixCls}-item-active`]"><a>{{ current }}</a></li>
          <li :title="current + 1" v-if="current + 1 < allPages" :class="[`${prefixCls}-item`]" @click="changePage(current + 1)"><a>{{ current + 1 }}</a></li>
          <li :title="current + 2" v-if="current + 2 < allPages" :class="[`${prefixCls}-item`]" @click="changePage(current + 2)"><a>{{ current + 2 }}</a></li>
          <li title="向后 5 页" v-if="current + 3 < allPages" :class="[`${prefixCls}-item-jump-next`]" @click="fastNext"><a>…</a></li>
          <li :title="'最后一页:' + allPages" :class="[`${prefixCls}-item`, {[`${prefixCls}-item-active`]: current == allPages}]" @click="changePage(allPages)"><a>{{ allPages }}</a></li>
          <li
              title="下一页"
              :class="nextClasses"
              @click="next">
              <a>→</a>
          </li>
          <Options
              :show-sizer="showSizer"
              :page-size="pageSize"
              :page-size-opts="pageSizeOpts"
              :show-elevator="showElevator"
              :_current.once="current"
              :current.sync="current"
              :all-pages="allPages"
              @on-size="onSize"
              @on-page="onPage">
          </Options>
      </ul>
  </template>
  <script>
      import { oneOf } from '../../utils/assist';
      import Options from './options.vue';
  
      const prefixCls = 'ivu-page';
  
      export default {
          components: { Options },
          props: {
              current: {
                  type: Number,
                  default: 1
              },
              total: {
                  type: Number,
                  default: 0
              },
              pageSize: {
                  type: Number,
                  default: 10
              },
              pageSizeOpts: {
                  type: Array,
                  default () {
                      return [10, 20, 30, 40]
                  }
              },
              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
              }
          },
          data () {
              return {
                  prefixCls: prefixCls
              }
          },
          computed: {
              allPages () {
                  return Math.ceil(this.total / this.pageSize);
              },
              simpleWrapClasses () {
                  return [
                      `${prefixCls}`,
                      `${prefixCls}-simple`
                  ]
              },
              wrapClasses () {
                  return [
                      `${prefixCls}`,
                      {
                          'mini': !!this.size
                      }
                  ]
              },
              prevClasses () {
                  return [
                      `${prefixCls}-prev`,
                      {
                          [`${prefixCls}-disabled`]: this.current == 1
                      }
                  ]
              },
              nextClasses () {
                  return [
                      `${prefixCls}-next`,
                      {
                          [`${prefixCls}-disabled`]: this.current == this.allPages
                      }
                  ]
              }
          },
          methods: {
              changePage (page) {
                  if (this.current != page) {
                      this.current = page;
                      this.$emit('on-change', page);
                  }
              },
              prev () {
                  const current = this.current;
                  if (current <= 1) {
                      return false;
                  }
                  this.changePage(current - 1);
              },
              next () {
                  const current = this.current;
                  if (current >= this.allPages) {
                      return false;
                  }
                  this.changePage(current + 1);
              },
              fastPrev () {
                  const page = this.current - 5;
                  if (page > 0) {
                      this.changePage(page);
                  } else {
                      this.changePage(1);
                  }
              },
              fastNext () {
                  const page = this.current + 5;
                  if (page > this.allPages) {
                      this.changePage(this.allPages);
                  } else {
                      this.changePage(page);
                  }
              },
              onSize (pageSize) {
                  this.pageSize = pageSize;
                  this.changePage(1);
              },
              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) {
                      this.prev()
                  } else if (key === 40) {
                      this.next()
                  } 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);
                  }
              }
          }
      }
  </script>