深夜码字!
效果:
?
点击回车添加内容?
?
??
?
?1.在vuex创建一个数组存放数据,以及点击添加/删除的方法
import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
export default new Vuex.Store({
state: {
list:[]
},
mutations: {
//添加数据
setAdd(state,data){
state.list.push(data)
},
//删除数据
setDelete(state,index){
state.list.splice(index,1)
},
//数据
allall(state){
state.list=[]
}
},
actions: {
},
modules: {
}
})
样式
<template>
<div>
<h1>todos</h1>
<div class="home">
<div class="h-head">
<div class="arrow" @click="qx"></div>
<input text="text" v-model="input" placeholder="What needs to be done?" @keydown.enter="add">
</div>
<div class="h-body">
<ul class="todo-list">
<li class="todo" :class="{'completed':item.state}" v-for="item,index in showList" :key="index">
<div class="view">
<!-- 通过多选框 改变 finished的状态 -->
<input class="toggle" type="checkbox" v-model="item.state"/>
<!-- 当点击文字时 实现反选 -->
<label @click="item.state==!item.state">{{item.title}}---{{item.state}}</label>
<button class="destroy" @click="setDelete(index)"></button>
</div>
</li>
</ul>
<div class="footer" v-cloak v-show="list.length>0">
<span class="todo-count"> <strong>{{num}} </strong>{{num | numFilter}} left </span>
<ul class="filters">
<li>
<a href="#" @click="cli=0" :class="{'selected':cli==0 || ''}">All</a>
</li>
<li>
<a href="#" @click="cli=1" :class="{'selected':cli==1}">Active</a>
</li>
<li>
<a href="#" @click="cli=2" :class="{'selected':cli==2}">Completed</a>
</li>
</ul>
<button class="clear-completed" @click="allall">Clear completed</button>
</div>
</div>
</div>
<footer class="info">
<p>Double-click to edit a todo</p>
<p>Written by <a href="http://evanyou.me">Evan You</a></p>
<p>Part of <a href="http://todomvc.com">TodoMVC</a></p>
</footer>
</div>
</template>
html,body{
margin: 0px;
padding: 0px;
}
body{
background-color: #f5f5f5;
}
h1{
font-size: 99px;
color: #e8d9d8;
margin: 0 0;
text-align: center;
}
.home{
width: 500px;
height: auto;
background: #fff;
margin: 0 auto;
box-shadow: 0px 0px 4px 2px #ccc;
}
.h-head{
width: 100%;
height: 50px;
position: relative;
}
.h-head .arrow{
width: 40px;
height: 50px;
position: absolute;
top: -11px;
left: 0px;
transform: rotate(90deg);
}
.h-head .arrow::before{
content: '?';
font-size: 22px;
color: #e6e6e6;
padding: 10px 27px 10px 27px;
}
.h-head input{
width: 88%;
height: 44px;
margin-left: 51px;
display: #fff;
border: none;
outline: none;
font-size: 21px;
}
.h-head input::-webkit-input-placeholder{
color:#e6e6e6;
}
.h-body{
width: 100%;
height: auto;
position: relative;
}
.h-b-todo{
width: 100%;
height: 50px;
background: blue;
}
.multipleChoice{
width: 30px;
height: 30px;
border-radius: 50%;
}
.todo-list {
margin: 0;
padding: 0;
list-style: none;
}
.todo-list li {
position: relative;
font-size: 24px;
border-bottom: 1px solid #ededed;
border: 1px solid #ccc;
border-bottom: none;
}
.todo-list li:last-child {
border-bottom: none;
}
.todo-list li.editing {
border-bottom: none;
padding: 0;
}
.todo-list li.editing .edit {
display: block;
width: calc(100% - 43px);
padding: 12px 16px;
margin: 0 0 0 43px;
}
.todo-list li.editing .view {
display: none;
}
.todo-list li .toggle {
text-align: center;
width: 40px;
/* auto, since non-WebKit browsers doesn't support input styling */
height: auto;
position: absolute;
top: 0;
bottom: 0;
left: 0;
margin: auto 0;
border: none; /* Mobile Safari */
-webkit-appearance: none;
appearance: none;
}
.todo-list li .toggle {
opacity: 0;
}
.todo-list li .toggle + label {
/*
Firefox requires `#` to be escaped - https://bugzilla.mozilla.org/show_bug.cgi?id=922433
IE and Edge requires *everything* to be escaped to render, so we do that instead of just the `#` - https://developer.microsoft.com/en-us/microsoft-edge/platform/issues/7157459/
*/
background-image: url('data:image/svg+xml;utf8,%3Csvg%20xmlns%3D%22http%3A//www.w3.org/2000/svg%22%20width%3D%2240%22%20height%3D%2240%22%20viewBox%3D%22-10%20-18%20100%20135%22%3E%3Ccircle%20cx%3D%2250%22%20cy%3D%2250%22%20r%3D%2250%22%20fill%3D%22none%22%20stroke%3D%22%23ededed%22%20stroke-width%3D%223%22/%3E%3C/svg%3E');
background-repeat: no-repeat;
background-position: center left;
}
.todo-list li .toggle:checked + label {
text-align: left;
background-image: url('data:image/svg+xml;utf8,%3Csvg%20xmlns%3D%22http%3A//www.w3.org/2000/svg%22%20width%3D%2240%22%20height%3D%2240%22%20viewBox%3D%22-10%20-18%20100%20135%22%3E%3Ccircle%20cx%3D%2250%22%20cy%3D%2250%22%20r%3D%2250%22%20fill%3D%22none%22%20stroke%3D%22%23bddad5%22%20stroke-width%3D%223%22/%3E%3Cpath%20fill%3D%22%235dc2af%22%20d%3D%22M72%2025L42%2071%2027%2056l-4%204%2020%2020%2034-52z%22/%3E%3C/svg%3E');
}
.todo-list li label {
word-break: break-all;
padding: 15px 15px 15px 60px;
display: block;
line-height: 1.2;
transition: color 0.4s;
text-align: left;
}
.todo-list li.completed label {
color: #d9d9d9;
text-decoration: line-through;
text-align: left;
}
.todo-list li .destroy {
display: none;
position: absolute;
top: 0;
right: 10px;
bottom: 0;
width: 40px;
height: 40px;
margin: auto 0;
font-size: 30px;
color: #cc9a9a;
margin-bottom: 11px;
transition: color 0.2s ease-out;
border: none;
background-color: #fff;
}
.todo-list li .destroy:hover {
color: #af5b5e;
}
.todo-list li .destroy:after {
content: '×';
}
.todo-list li:hover .destroy {
display: block;
}
.todo-list li .edit {
display: none;
}
.todo-list li.editing:last-child {
margin-bottom: -1px;
}
.footer {
color: #777;
padding: 10px 15px;
height: 20px;
text-align: center;
border-top: 1px solid #e6e6e6;
height: 50px;
}
.footer:before {
content: '';
position: absolute;
right: 0;
bottom: 0;
left: 0;
height: 50px;
overflow: hidden;
box-shadow: 0 1px 1px rgba(0, 0, 0, 0.2),
0 8px 0 -3px #f6f6f6,
0 9px 1px -3px rgba(0, 0, 0, 0.2),
0 16px 0 -6px #f6f6f6,
0 17px 2px -6px rgba(0, 0, 0, 0.2);
}
.todo-count {
float: left;
text-align: left;
}
.todo-count strong {
font-weight: 300;
}
.filters {
margin: 0;
padding: 0;
list-style: none;
position: absolute;
right: 0;
left: 0;
}
.filters li {
display: inline;
}
.filters li a {
color: inherit;
margin: 3px;
padding: 3px 7px;
text-decoration: none;
border: 1px solid transparent;
border-radius: 3px;
}
.filters li a:hover {
border-color: rgba(175, 47, 47, 0.1);
}
.filters li a.selected {
background: blueviolet;
color: #fff;
}
.clear-completed,
html .clear-completed:active {
float: right;
position: relative;
line-height: 20px;
text-decoration: none;
cursor: pointer;
}
.clear-completed:hover {
text-decoration: underline;
}
.info {
margin: 65px auto 0;
color: #bfbfbf;
font-size: 10px;
text-shadow: 0 1px 0 rgba(255, 255, 255, 0.5);
text-align: center;
}
.info p {
line-height: 1;
}
.info a {
color: inherit;
text-decoration: none;
font-weight: 400;
}
.info a:hover {
text-decoration: underline;
}
@media screen and (-webkit-min-device-pixel-ratio:0) {
.toggle-all,
.todo-list li .toggle {
background: none;
}
.todo-list li .toggle {
height: 40px;
}
}
@media (max-width: 430px) {
.footer {
height: 50px;
}
.filters {
bottom: 10px;
}
}
2.在页面中调用方法,且传入数据(具体内容已经写在字里行间,笔者不做过多解释)
<script>
import {mapState,mapMutations} from 'vuex'
export default {
name: 'Home',
data(){
return {
input:'',//获取input的值
cli:0//默认为展示所有项
}
},
computed:{
...mapState(['list']),//调用值
num(){
return this.list.filter(item=>!item.state).length//返回数组长度
},
showList(){
//点击筛选
switch(this.cli) {
case 0://展示所有
return this.list;
case 1://过滤掉不被选择的
return this.list.filter(item=>!item.state);
default://过滤掉不被选择的
return this.list.filter(item=>item.state);
}
}
},
filters:{
//当勾选的事情超过一件,则item加s
numFilter(value){
return value==1?'item':'items'
}
},
methods:{
//调用方法
...mapMutations(['setAdd','setDelete','allall']),
add(){
//当点击回车键之后,添加相对应的数据
this.setAdd({
id:new Date().getTime(),//id不重复
title:this.input,//input的值
state:false//状态最开始为false
})
this.input = ''//当当前数据添加完之后,清空input里面的值
},
qx(){
//点击input前面的箭头,执行反选
this.showList.map(item=>{
switch(item.state){
case true:
return item.state = false
default:
return item.state = true
}
})
}
}
}
</script>
注意:当input的值为空时,不能再添加值,那么此时我们需要做一个判断,当值为空时,禁用回车键,也就是当ascii值为13时,禁止添加。
深夜码字辛苦,相互加油吧。晚安!
|