todolist小案例

<!DOCTYPE html>

<html lang="en">

<head>

    <meta charset="UTF-8">

    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <title>自己写一个</title>

    

    <style>

        .head1{

            width: 600px;

            height: 50px;

            background-color: seagreen;

        }

        .dbtn{

            width: 600px;

            height: 50px;

        }

        li{

            list-style: none;

            width: 500px;

            margin-top: 10px;

            background-color: seashell;

            line-height:50px;

            min-height: 50px;

            position: relative;

        }

        button{

            width: 60px;

            height: 20px;

            position: absolute;

            bottom: 0px;

            bottom: 0px;

            right: 0px;

        }

        .act1{

            background: greenyellow;

        }

        .act2{

            background: yellow;

            right: 80px;

        }

        input{

            width: 500px;

            height: 50px;

            margin-left: 30px;

        }

        #dbtn{

            width: 66px;

            height: 166px;

            position: fixed;

            left: 560px;

            justify-content: center;

            align-items: flex-start;

        }

        p{

           border: 2px solid black;

            height: 50px;

            width: 60px;

            border-radius: 15%;

            line-height: 50px;

            text-align: center;

        }

        .act3{

            background-color: gold;

        }

        

    </style>

</head>

<body>

    <div id="app">

        

        <div class="head1">开始写数据吧</div>

        <div id="dbtn">

          <p v-for="btn in btns" :class="{act3:btn.title === type?1:0}"

          @click="type=btn.title"

          >{{btn.title}}</p>

        </div>


 

        <input type="text" v-model="msg" @keyup.13 ="addTd" placeholder="输入内容 按键回车创建新的内容">

        <ul>

            <li v-for="todo in computedTd"   :class="{act1:todo.isok}">{{todo.title}}

                <button @click="todo.isok = !todo.isok" :class="{act1:todo.isok}">完成</button>

                <button @dblclick=" removeTd(id)"

                 @click="checkTd(todo.id,todo.isok)" class="act2">删除</button>

            </li>

        </ul>

 

    </div>

    <script src="https://cdn.jsdelivr.net/npm/vue"></script>

    <script>

        new Vue({

            el:"#app",

            data:{

                msg:"",

                isok:true,

                todos:[

                    {id:1,title:"只是第一条数据",isok:false,},

                    {id:2,title:"第二条数据开始吧",isok:false,}

                ],

                btns:[

                    {id:1,title:"A",theme:"primary"},

                    {id:2,title:"U",theme:"danger"},

                    {id:3,title:"F",theme:"success"} 

                ],

                type:"A"

            },

            methods:{

                removeTd(id){

                    //使用fliter 过滤器的原则  会将符合的产生新的一组数据

                   /*  this.todos = this.todos.filter(item=>{

                        if(item.id === id){

                            return false

                        }

                        return true

                    }) */  

                     //可以简写

                   /*  this.todos = this.todos.filter(item => item.id !== id) */

                    // 更加简写

                    this.todos = this.todos.filter (_=>_.id !== id)

                },

                //删除之前判断是都完成

                checkTd(id,isok){

                    if(isok){

                        this.removeTd(id)

                    }else{

                        if(confirm("确定要删除吗")){

                            this.removeTd(id)

                        }

                    }

                },

                // 添加

                addTd(){

                    this.todos.push({

                        id:parseInt(Math.random()*100000),

                        title:this.msg,

                        isok:false

                    })

                }

            },

            //computed 计算属性

            computed:{

                computedTd(){

                    switch (this.type) {

                        case "A": 

                            return this.todos

                        case "F":

                            return this.todos.filter(item=>item.isok?true:false)

                        case "U":

                            return this.todos.filter(item=>!item.isok?true:false)

                    }

                }

            }

        })

    </script>

</body>

</html>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值