Vue实现简单ToDolist案例
<template
>
<div
>
<div
>
<input type
="text" v
-model
="inputValue" @keydown
.enter
="add">
<span @click
="shaix" style
="cursor: pointer;">筛选
</span
>
<h3
>正在进行({{noList
}})</h3
>
<div
>
<div v
-for="(item,index) in list" v
-show
="!item.isSuc">
<input type
="checkbox" @click
.prevent
="handleNo(item)">
<span
class="todo-sp1"
v
-show
="updateIndex!=index"
@dblclick
="update(item,index)">
{{item
.title
}}
</span
>
<input type
="text" v
-show
="updateIndex==index"
v
-model
="item.title"
@keydown
.enter
="updateSave"
@keydown
.esc
="updateBack(item)"
@blur
="updateSave">
<button @click
="del(index)">删除
</button
>
</div
>
</div
>
<h3
>已经完成({{yesList
}})</h3
>
<div
>
<div v
-for="(item,index) in list" v
-show
="item.isSuc">
<input type
="checkbox" checked @click
.prevent
="handleYes(item)">
<span
class="todo-sp1"
v
-show
="updateIndex!=index"
@dblclick
="update(item,index)">
{{item
.title
}}
</span
>
<input type
="text" v
-show
="updateIndex==index"
v
-model
="item.title"
@keydown
.enter
="updateSave"
@keydown
.esc
="updateBack(item)"
@blur
="updateSave">
<button @click
="del(index)">删除
</button
>
</div
>
</div
>
</div
>
</div
>
</template
>
<script
>
export default{
data(){
return{
inputValue
:'',
list
:[],
updateIndex
:-1,
backSave
:''
}
},
created() {
let list
= localStorage
.list
if(list
){
this.list
= JSON.parse(list
)
}
},
computed
:{
noList(){
let num
= 0
this.list
.map(item
=>{
if(!item
.isSuc
){
num
++
}
})
return num
},
yesList(){
let num
= 0
this.list
.map(item
=>{
if(item
.isSuc
){
num
++
}
})
return num
}
},
methods
:{
add(){
if(this.inputValue
.trim() == ''){
return
}
this.list
.push({
title
:this.inputValue
,
isSuc
:false
})
this.inputValue
= ''
this.save()
},
del(index
){
this.list
.splice(index
,1)
this.save()
},
handleNo(item
){
item
.isSuc
= true
this.save()
},
handleYes(item
){
item
.isSuc
= false
this.save()
},
update(item
,index
){
this.updateIndex
= index
this.backSave
= item
.title
},
updateSave(){
this.updateIndex
= -1
this.save()
},
updateBack(item
){
this.updateIndex
= -1
item
.title
= this.backSave
},
shaix(){
this.$router
.push({
name
:'shaix'
})
},
save(){
localStorage
.list
= JSON.stringify(this.list
)
}
}
}
</script
>
<style scoped
="scoped">
.todo
-sp1
{
display
: inline
-block
;
width
: 200px
;
cursor
: move
;
}
</style
>
筛选+搜索:
<template
>
<div
>
<select name
="" id
="" v
-model
="sel">
<option value
="">请选择
</option
>
<option value
="all">全部
</option
>
<option value
="no">正在进行
</option
>
<option value
="yes">已经完成
</option
>
</select
>
搜索
:
<input type
="text" v
-model
="kw" @keydown
.enter
="search">
<h3
>筛选结果
:</h3
>
<div
>
<div v
-for="(item,index) in showlist">
{{item
.title
}}
</div
>
</div
>
</div
>
</template
>
<script
>
export default{
data(){
return{
sel
:'',
list
:[],
showlist
:[],
kw
:''
}
},
created() {
let list
= localStorage
.list
if(list
){
this.list
= JSON.parse(list
)
}
},
methods
:{
search(){
this.showlist
= []
this.list
.map(item
=>{
if(item
.title
.includes(this.kw
)){
this.showlist
.push(item
)
}
})
}
},
watch
:{
sel(newSel
){
this.showlist
= []
if(newSel
== 'all'){
this.showlist
= this.list
}else if(newSel
== 'no'){
this.list
.map(item
=>{
if(!item
.isSuc
){
this.showlist
.push(item
)
}
})
}else if(newSel
== 'yes'){
this.list
.map(item
=>{
if(item
.isSuc
){
this.showlist
.push(item
)
}
})
}
}
}
}
</script
>
<style
>
</style
>
转载请注明原文地址:https://ipadbbs.8miu.com/read-13738.html