vue实现todolist案例

10 篇文章 0 订阅

思路:

  1. 创建静态页面
  2. todolist案例实现添加功能
    1. 在确定按钮上添加功能:v-on添加事件功能,写在methods中
    2. 在input按钮中添加事件绑定,v-model:双向数据绑定,数据获取,在data中写入input初始值
    3. 添加事件:数据获取成功后,将其追加进新建的数组中,使用push方法,值就会根据新建的数组所对应的input中显示出来  
  3. todolist案例实现删除功能
    1. 删除添加的数据,添加删除按钮,绑定删除事件,或者直接在所想要删除的绑定事件的标签上添加删除事件
    2. 删除功能是根据下标(index)来删除的,所以需要获取参数,因此添加index到li中去获取当前index,删除方法splice
  4. todolist案例实现选框功能
    1. 完善静态页面,添加input标签,type="checktext"
    2. 选框功能也需要获取相应的index,以及获取选框status
    3. 添加未选中按钮,绑定选中事件,将list数组,改写为下列代码中所示
    4. 事件中获取index对应的item,获取item后改变status
    5. 同时按钮中使用三目运算,根据status判断是否选中

代码

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <div id="app">
        <input type="text" v-model="inputValue">
        <button @click="onSure">确定</button>
        <ul>
            <li v-for="(item,index) in list">
                <input type="checkbox" v-model="item.state">
                {{item.name}}
                <button  @click="onDelete(index)">删除</button>
                <button @click="onChoose(index)">{{item.state? '选中':'未选中'}}</button>
            </li>
        </ul>
    </div>

    <script src="../lib/vue.global.js"></script>
    <script>
        const {createApp}=Vue
        createApp({
            data() {
                return {
                    inputValue: '',
                    list:[
                        {name:'java', state:false,},
                        {name:'javaEE', state:false,},     
                    
                    ],   
                };
            },
            methods: {
                //确定
                onSure(){
                    let obj={
                        name:this.inputValue,
                        state:false,
                    }
                    this.list.push(obj)
                    this.inputValue=''
                },
                //删除
                onDelete(index){
                    this.list.splice(index,1)
                    
                },
                //选中
                onChoose(index){
                   let item= this.list[index]
                    item.state=!item.state

                }

            },
        }).mount('#app') //挂载app节点
    </script>
</body>
</html>

可以参考以下代码实现一个简单的 Vue TodoList: ``` <template> <div class="todo-list"> <h1>Vue TodoList</h1> <form @submit.prevent="addItem"> <input type="text" v-model="newItem" placeholder="Add item..."> <button type="submit">Add</button> </form> <ul> <li v-for="(item, index) in items" :key="index"> <span>{{ item }}</span> <button @click="deleteItem(index)">Delete</button> </li> </ul> </div> </template> <script> export default { data() { return { newItem: '', items: [] } }, methods: { addItem() { if (this.newItem !== '') { this.items.push(this.newItem); this.newItem = ''; } }, deleteItem(index) { this.items.splice(index, 1); } } } </script> <style> .todo-list { margin: 0 auto; max-width: 600px; } ul { list-style: none; margin: 0; padding: 0; } li { display: flex; justify-content: space-between; align-items: center; margin: 10px 0; padding: 10px; border: 1px solid #ccc; border-radius: 5px; } button { background-color: #f44336; color: #fff; border: none; border-radius: 5px; padding: 5px 10px; cursor: pointer; } button:hover { background-color: #d32f2f; } input[type="text"] { padding: 10px; border-radius: 5px; border: 1px solid #ccc; margin-right: 10px; font-size: 16px; } </style> ``` 在这个 TodoList 中,我们使用了 `v-model` 指令绑定 `newItem` 变量,通过 `addItem` 方法向 `items` 数组中添加新的项目,并使用 `v-for` 指令渲染出每个项目。同时,在每个项目中,我们添加了一个 `Delete` 按钮,用于删除对应的项目。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值