forked from sunseekers/Vue2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dome2.html
182 lines (172 loc) · 4.1 KB
/
dome2.html
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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title>自动筛选</title>
<meta name="description" content="">
<meta name="keywords" content="">
<style type="text/css">
body {
font-family: Helvetica Neue, Arial, sans-serif;
font-size: 14px;
color: #444;
}
table {
border: 2px solid #42b983;
border-radius: 3px;
background-color: #fff;
}
th {
background-color: #42b983;
color: rgba(255,255,255,0.66);
cursor: pointer;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
}
td {
background-color: #f9f9f9;
}
th, td {
min-width: 120px;
padding: 10px 20px;
}
th.active {
color: #fff;
}
th.active .arrow {
opacity: 1;
}
.arrow {
display: inline-block;
vertical-align: middle;
width: 0;
height: 0;
margin-left: 5px;
opacity: 0.66;
}
.arrow.asc {
border-left: 4px solid transparent;
border-right: 4px solid transparent;
border-bottom: 4px solid #fff;
}
.arrow.dsc {
border-left: 4px solid transparent;
border-right: 4px solid transparent;
border-top: 4px solid red;
}
</style>
<!--HTML 模板,通常被称为[X-Templates]相当于 template写进去的一样-->
<script type="text/javascript" src='js/vue.min.js'></script>
<script type="text/x-template" id="grid-template">
<table>
<thead>
<tr>
<th v-for="key in columns"
@click="sortBy(key)"
:class="{ active: sortKey == key }">
<!--capitalize 是自定义过滤器函数,key 为capitalize的第一个参数-->
{{ key | capitalize }}
<span class="arrow" :class="sortOrders[key] > 0 ? 'asc' : 'dsc'">
</span>
</th>
</tr>
</thead>
<tbody>
<tr v-for="entry in filteredData">
<td v-for="key in columns">
{{ entry[key] }}
</td>
</tr>
</tbody>
</table>
</script>
</head>
<body>
<div id='demo'>
<from id='search'>
Search <input name='query' v-model='searchQuery'>
</from>
<demo-grid
:data='gridData'
:columns='gridColumns'
:filter-key='searchQuery'>
</demo-grid>
</div>
<script type="text/javascript">
Vue.component('demo-grid',{
template:'#grid-template',
props:{
data:Array,
columns:Array,
filterKey:String
},
//选用哪种样式;响应数据的变化
data:function(){
var sortOrders={};
this.columns.forEach(function(key){
sortOrders[key]=1;
})
return {
sortKey:'',
sortOrders:sortOrders
}
},
computed:{
filteredData(){
var sortKey=this.sortKey;
var filterKey=this.filterKey && this.filterKey.toLowerCase();
var order=this.sortOrders[sortKey]||1;
var data=this.data;
//通过测试的 name
if(filterKey){
data=data.filter(function(row){
return Object.keys(row).some(function(key){
return String(row[key]).toLowerCase().indexOf(filterKey)>-1;
})
})
}
//通过测试的 power
if(sortKey){
data=data.slice().sort(function(a,b){
a=a[sortKey];
b=b[sortKey];
return (a===b?0:a>b?1:-1)*order
})
}
return data;
}
},
filters:{
//首字母大写
capitalize(str){
return str.charAt(0).toUpperCase()+str.slice(1)
}
},
methods:{
//升序或者降序
sortBy(key){
this.sortKey=key;
this.sortOrders[key]=this.sortOrders[key]*-1
console.log(this.sortOrders[key]);
}
}
})
var demo=new Vue({
el:'#demo',
data:{
searchQuery:'',
gridColumns:['name','power'],
gridData:[
{name:'Chuck Norris',power: Infinity},
{name:'bruce Lee',power:9000},
{name:'Jackie chan',power:7000},
{name:'Jet Li',power:8000}
]
}
})
</script>
</body>
</html>