forked from tushargugnani/learningVueJS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path12.sort-list-computed-properties.html
87 lines (79 loc) · 2.85 KB
/
12.sort-list-computed-properties.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<!-- development version, includes helpful console warnings -->
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<style>
.asc:after{
content: "\25B2"
}
.desc:after{
content: "\25BC"
}
</style>
</head>
<body>
<div id="app">
<h3>List of Products</h3>
<table border="1">
<thead>
<tr>
<th @click="sort('name')" v-bind:class="[sortBy === 'name' ? sortDirection : '']">Name</th>
<th @click="sort('price')" v-bind:class="[sortBy === 'price' ? sortDirection : '']">Price</th>
<th @click="sort('category')" v-bind:class="[sortBy === 'category' ? sortDirection : '']">Category</th>
</tr>
</thead>
<tbody>
<tr v-for="product in sortedProducts">
<td>{{product.name}}</td>
<td>{{product.price}}</td>
<td>{{product.category}}</td>
</tr>
</tbody>
</table>
</div>
</body>
<script type="text/javascript">
var app = new Vue({
el: '#app',
data: {
sortBy: 'name',
sortDirection: 'asc',
products: [
{ name: "Keyboard", price: 44, category: 'Accessories'},
{ name: "Mouse", price: 20, category: 'Accessories'},
{ name: "Monitor", price: 399, category: 'Accessories'},
{ name: "Dell XPS", price: 599, category: 'Laptop'},
{ name: "MacBook Pro", price: 899, category: 'Laptop'},
{ name: "Pencil Box", price: 6, category: 'Stationary'},
{ name: "Pen", price: 2, category: 'Stationary'},
{ name: "USB Cable", price: 7, category: 'Accessories'},
{ name: "Eraser", price: 2, category: 'Stationary'},
{ name: "Highlighter", price: 5, category: 'Stationary'}
]
},
computed: {
sortedProducts: function(){
return this.products.sort((p1,p2) => {
let modifier = 1;
if(this.sortDirection === 'desc') modifier = -1;
if(p1[this.sortBy] < p2[this.sortBy]) return -1 * modifier;
if(p1[this.sortBy] > p2[this.sortBy]) return 1 * modifier;
return 0;
});
}
},
methods: {
sort: function(s){
if(s === this.sortBy) {
this.sortDirection = this.sortDirection === 'asc' ? 'desc' : 'asc';
}
this.sortBy = s;
}
},
});
</script>
</html>