Vue3 todos列表demo

<template>
    <h2>Vue3 todos列表</h2>
    <p><input type="text" v-model="test" @keydown.enter="addItem"></p>
    <table border="1" cellpadding="0" cellspacing="0" style="margin: 0 auto;width: 500px;">
        <tr>
            <td>筛选</td>
            <td>序号</td>
            <td>任务</td>
            <td>状态</td>
            <td>操作</td>
        </tr>
        <tr v-for="(item, index) in filterList" :key="item.id">
            <td><input type="checkbox" v-model="item.done"></td>
            <td>{{ index+ 1}}</td>
            <td>{{ item.text }}</td>
            <td>{{ item.done ? "完成" : "未完成" }}</td>
            <td><a href="#" @click.prevent="removeItem(item.id)">删除</a></td>
        </tr>
    </table>
    <p><input type="checkbox" v-model="allChecked">展示全部</p>
    <p>已完成 <b>{{ doneLength }}</b> / 总数<b>{{ list.length }}</b></p>
</template>

<script setup>
import { reactive, computed, ref } from "vue";

const {
    list,
    test,
    allChecked,
    addItem,
    removeItem,
    filterList,
    doneLength
} = todoList()

function todoList() {
    // 列表
    const list = reactive([
        {
            id: 1,
            text: "吃饭",
            done: false
        },
        {
            id: 2,
            text: "睡觉",
            done: false
        },
        {
            id: 3,
            text: "打豆豆",
            done: true
        }
    ])
    const test = ref("")
    const nextId = (4)
    const allChecked = ref(false)
    // 回车添加
    const addItem = () => {
        if (!test.value.trim()) return
        const obj = {
            id: nextId.value,
            text: test.value,
            done: false
        }
        list.push(obj)
        test.value = ""
        nextId.value++
    }
    //删除一项数据 
    const removeItem = (id) => {
        const index = list.findIndex(el => el.id == id)
        list.splice(index, 1)
    }
    // 筛选todos列表
    const filterList = computed(() => list.filter(el => !el.done || allChecked.value))
    //筛选已经完成的任务
    const doneLength = computed(() => list.filter(el => el.done).length)
    return {
        list,
        test,
        allChecked,
        addItem,
        removeItem,
        filterList,
        doneLength
    }
}
</script>

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

web网页精选

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值