-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSimpleTable.vue
156 lines (154 loc) · 3.95 KB
/
SimpleTable.vue
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
<template>
<div class="container-fluid">
<table class="table table-bordered">
<thead>
<tr>
<th v-for="column in columns" @click="sortData(sortColumn[$index])" :class="{'cansort': sortColumn[$index]}">{{column}}<span class="glyphicon" :class="{'glyphicon-menu-down': sortColumn[$index] === sort.key && sort.val === -1,'glyphicon-menu-up': sortColumn[$index] === sort.key && sort.val === 1 }"></span></th>
</tr>
</thead>
<tbody>
<tr v-for="tr in data | orderBy sort.key sort.val | limitBy record currentPage*record ">
<td v-for="td in tr">{{td}}</td>
</tr>
</tbody>
</table>
<ul class="pagination pagination-sm">
<li>
<a href="javascript:void(0);" aria-label="Previous" @click="preOrNext(0)">
<span aria-hidden="true">«</span>
</a>
</li>
<li :class="{'active': n-1 == currentPage}" v-show="currentPage > 7">
<a href="javascript:void(0);" @click="changePage(0)">
1
<span class="sr-only">(current)</span>
</a>
</li>
<li :class="{'active': n-1 == currentPage}" v-for="n in tabPageNum">
<a href="javascript:void(0);" @click="changePage(n-1)">
{{n}}
<span class="sr-only">(current)</span>
</a>
</li>
<li :class="{'active': n-1 == currentPage}" v-show="currentPage < pageNum - 5 && pageNum > 9">
<a href="javascript:void(0);" @click="changePage(pageNum - 1)">
{{pageNum}}
<span class="sr-only">(current)</span>
</a>
</li>
<li>
<a href="javascript:void(0);" aria-label="Next" @click="preOrNext(1)">
<span aria-hidden="true">»</span>
</a>
</li>
<li>
<a href="javascript:void(0);">
共{{ pageNum }}页
<span aria-hidden="true"></span>
</a>
</li>
</ul>
</div>
</template>
<script>
export default {
data () {
return {
currentPage: 0,
sort: {
key: '',
val: 1
}
}
},
props: {
columns: {
type: Array
},
data: {
type: Array
},
record: {
type: Number,
default: 10
},
sortColumn: {
type: Array,
default: () => {
return []
}
}
},
computed: {
pageNum () {
return Math.ceil(this.data.length / this.record)
},
tabPageNum () {
let left = 1
let right = this.pageNum
let ar = []
if (this.pageNum >= 11) {
if (this.currentPage > 7 && this.currentPage < this.pageNum - 5) {
left = this.currentPage - 4
right = this.currentPage + 2
} else {
if (this.currentPage <= 7) {
left = 1
right = 9
} else {
right = this.pageNum
left = this.pageNum - 7
}
}
}
while (left <= right) {
ar.push(left)
left++
}
return ar
}
},
methods: {
changePage (index) {
this.currentPage = index
},
preOrNext (toPre) {
if ((this.currentPage === 0 && !toPre) || (this.currentPage + 1 === this.pageNum && toPre)) return
toPre ? this.currentPage = this.currentPage + 1 : this.currentPage = this.currentPage - 1
},
sortData (name) {
if (!this.sortColumn || this.sortColumn.length === 0) return
if (!name) return
this.sort = {
key: name,
val: -this.sort.val
}
}
},
watch: {
data () {
this.currentPage = 0
this.sort = {
key: '',
val: 1
}
}
}
}
</script>
<style scoped>
.container-fluid {
position: relative;
}
table {
background-color: #fff;
}
.cansort {
cursor: pointer;
}
.pagination {
position: absolute;
top: -53px;
right: 15px;
}
</style>