首页 > 其他 > 详细

vue小练习TODOlist

时间:2020-08-19 07:25:11      阅读:64      评论:0      收藏:0      [点我收藏+]

今天做了一个vue小练习TODOlist,用了一些不同的方法,功能基本实现,但是还有一些不太完美的地方

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="./node_modules/vue/dist/vue.js"></script>
    <style>
        *{
            margin: 0;
            padding: 0;
        }
        #demodiv{
            margin: 100px;
        }
        li{
            list-style: none;
        }
        .active{
            color:slategray;
            background-color: #ccc;
            text-decoration: line-through;
        }
    </style>
</head>
<body>
    <div id="demodiv">
        <h1>任务列表</h1>
        <p>任务总数:{{arr.length}},还有:{{this.residue()}}未完成,[<a href="#" @click="del()">完成</a>]</p>
        <ul>
            <li v-for="(item,index) in arr" :class="item.style?‘active‘:‘‘">
                <input type="checkbox" v-model="item.style">
                <span v-if="item.edit" v-on:click="edit(index)">{{item.content}}---{{item.style}}</span>
                <input :autofocus="item.edit?‘‘:‘autofocus‘" v-else type="text" v-model="item.content" v-on:blur="edit(index)">
            </li>
        </ul>
        <input type="text" v-model="inputVal"><button :disabled=state v-on:click="add()">添加</button>
    </div>


    <script>
        new Vue({
            el:"#demodiv",
            data:{
                state:true,
                inputVal:"",
                arr:[
                    {content:"亚瑟",style:false,edit:true},
                    {content:"阿古朵",style:false,edit:true},
                    {content:"兰陵王",style:false,edit:true},
                    {content:"猪八戒",style:false,edit:true},
                    {content:"阿珂",style:false,edit:true},
                    {content:"鲁班",style:false,edit:true},
                    {content:"凯皇",style:false,edit:true},

                ]
            },
            methods:{
                add(){
                    if(this.inputVal != ""){
                        this.arr.push({content:this.inputVal,style:false,edit:true})
                        this.inputVal = "";
                    }
                },
                //剩余的
                residue(){
                    console.log(this.arr.filter(item=>item.style==false).length)
                    return this.arr.filter(item=>item.style==false).length
                },
                edit(index){
                    if(this.arr[index].style!=true){
                        this.arr[index].edit = !this.arr[index].edit;
                    }
                    
                },
                del(){
                    this.arr = this.arr.filter(item=>item.style==false)
                }
            },
            watch:{
                inputVal(newval,oldval){
                    if(this.inputVal==""){
                        this.state = true;
                    }else{
                        this.state = false;
                    }
                }
            }
        })
    </script>
</body>
</html>

技术分享图片

技术分享图片

技术分享图片

技术分享图片

技术分享图片

 


其中仍有一点疑惑,就是每次如何点击后可以让input文本框出现后自动聚焦,使用了:autofocus="item.edit?‘‘:‘autofocus‘"还是不能解决

vue小练习TODOlist

原文:https://www.cnblogs.com/kharvey/p/13527003.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!