forked from tushargugnani/learningVueJS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path10.filter-list-computed-properties.html
74 lines (69 loc) · 2.9 KB
/
10.filter-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
<!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>
</head>
<body>
<div id="app">
<h5>List of Products</h5>
<h3>Filter By Category</h3>
<select v-model="category">
<option valeu="Accessories">Accessories</option>
<option valeu="Laptop">Laptop</option>
<option valeu="Stationary">Stationary</option>
</select>
<ul>
<li v-for="product in filterProductsByCategory">Product Name : {{product.name}} - Price : {{product.price}} ({{product.category}})</li>
</ul>
<h3>Filter By Name</h3>
<input type="text" v-model="name" placeholder="Filter By Name"/>
<ul>
<li v-for="product in filterProductsByName">Product Name : {{product.name}} - Price : {{product.price}} ({{product.category}})</li>
</ul>
<h3>Filter By Price Range</h3>
<label for="vol">Price (between 0 and 1000):</label>
<input type="range" v-model="range" min="0" max="1000" step="10"/>
Selected : {{range}}
<ul>
<li v-for="product in filterProductsByRange">Product Name : {{product.name}} - Price : {{product.price}} ({{product.category}})</li>
</ul>
</div>
</body>
<script type="text/javascript">
var app = new Vue({
el: '#app',
data: {
category: '',
name: '',
range: '500',
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: {
filterProductsByCategory: function(){
return this.products.filter(product => !product.category.indexOf(this.category))
},
filterProductsByName: function() {
return this.products.filter(product => !product.name.indexOf(this.name))
},
filterProductsByRange: function(){
return this.products.filter(product => (product.price > 0 && product.price < this.range) ? product : '')
}
}
});
</script>
</html>