-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.html
107 lines (106 loc) · 2.79 KB
/
index.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Shopping List App</title>
<link rel="stylesheet" href="main.css" />
<!-- End head. -->
</head>
<body>
<div id="shopping-list">
<div class="header">
<h1>{{ header.toLocaleUpperCase() }}</h1>
<button
v-if="state === 'default'"
class="btn btn-primary"
@click="changeState('add')"
>
Add Item
</button>
<button v-else class="btn btn-cancel" @click="changeState('default')">
Cancel Adding Item
</button>
</div>
<div v-if="state === 'add'" class="add-item-form">
<input
v-model="newItem"
type="text"
placeholder="Add an item"
@keyup.enter="addItem"
/>
<button class="btn btn-primary" @click="addItem">
Save Item
</button>
</div>
<ul>
<item v-for="item in sortedItems" :product="item"></item>
</ul>
</div>
<script src="https://cdn.jsdelivr.net/npm/vue"></script>
<script>
Vue.component('item', {
props: ['product'],
template: `<li :class="{strikeout: product.purchased}" @click="togglePurchased(product)">
{{ product.label }}
</li>`,
methods: {
togglePurchased: (item) => {
item.purchased = !item.purchased;
}
}
});
var shoppingList = new Vue({
el: '#shopping-list',
data: {
state: 'default',
header: 'shopping list app',
newItem: '',
sortDirection: 1,
items: [
{
label: '10 party hats',
purchased: false,
highPriority: false,
},
{
label: '2 board games',
purchased: true,
highPriority: false,
},
{
label: '20 cups',
purchased: false,
highPriority: false,
},
],
},
computed: {
reversedItems() {
return this.items.slice(0).reverse();
},
sortedItems() {
return this.items.slice(0).sort( (a, b) => {
return a.label.localeCompare(b.label);
})
}
},
methods: {
addItem: function () {
this.items.push({
label: this.newItem,
purchased: false,
});
this.newItem = '';
},
changeState: function (newState) {
this.state = newState;
this.newItem = '';
},
togglePurchased(item) {
item.purchased = !item.purchased;
},
},
});
</script>
</body>
</html>