Vue
by tonytlwu
HTML
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.10/lodash.min.js"></script>
<div id="app">
<h2>To do: ({{ incompleteTodos.length }})</h2>
<form @submit.prevent="addNewItem">
<input type="text" v-model="newItem" placeholder="Type something and press return"/>
</form>
<ol>
<li v-for="(todo, index) in incompleteTodos">
<label>
<input type="checkbox" v-model="todo.completed" />
<span>
{{ todo.text }}
</span>
<a href="#" class="remove" @click="removeItem(index)">Remove</a>
</label>
</li>
</ol>
<hr />
<h2>
Completed items: ({{ completedTodos.length }})
</h2>
<ol>
<li v-for="(todo, index) in completedTodos">
<label>
<input type="checkbox" v-model="todo.completed" />
<span>
{{ todo.text }}
</span>
<a href="#" class="remove" @click="removeItem(index)">Remove</a>
</label>
</li>
</ol>
</div>
CSS
body {
background: #20262E;
padding: 20px;
font-family: Helvetica;
}
#app {
background: #fff;
border-radius: 4px;
padding: 20px;
transition: all 0.2s;
}
li {
margin: 8px 0;
}
h2 {
font-weight: bold;
margin-bottom: 15px;
}
del {
color: rgba(0, 0, 0, 0.3);
}
input[type="text"] {
padding: 10px;
width: 200px;
}
label .remove {
display: none;
color: red;
}
label:hover .remove {
display: inline-block;
margin-left: 10px;
color: red;
opacity: 0.5;
}
Vue
new Vue({
el: "#app",
data: {
todos: [
{ text: "Learn about Vue", completed: false },
{ text: "Learn about Fliplet", completed: false },
{ text: "Play around in JSFiddle", completed: false },
{ text: "Show us what you've got", completed: false }
],
completed: [],
newItem: ''
},
computed: {
completedTodos: function () {
return this.todos.filter(function (t) {
return t.completed;
});
},
incompleteTodos: function () {
return this.todos.filter(function (t) {
return !t.completed;
});
}
},
methods: {
addNewItem() {
var todos = _.cloneDeep(this.todos);
todos.splice(0, 0, { text: this.newItem, completed: false });
this.todos = todos;
this.newItem = '';
},
removeItem(index) {
this.todos.splice(index, 1);
}
}
})