IMWeb训练营作业-todolist

Vue-todolist


这个todolist是原来模仿youtobe的一个教程用原生的js写的,是针对移动端的一个小应用,看了直播课后根据学到的相关vue-api改的。

运行的效果图是这样子的

效果图

核心代码也主要就是相关的几个api


/* 这里主要是判断localStorage是否存储的有上次的数据 */
if (localStorage.getItem('todolist')) {
    /* 因为localStorage只能存储字符串数据,所以这里要用JSON.parse对存储的信息进行解析成js对象 */
    var todoList = JSON.parse(localStorage.getItem('todolist'));
} else {
    var todoList = {
        todo: [],
        compt: []
    }
}

new Vue({
    el: '#main',
    data: todoList,
    methods: {
        /* 增加条目的方法前面的判断是用来判断输入框是否为空,没有做正则匹配,执行完后对当前数据存储在localstorage中 */
        addtodo: function (ev) {

            if (ev.currentTarget.parentNode.querySelector('input').value) {
                this.todo.push({ things: ev.currentTarget.parentNode.querySelector('input').value });
                ev.currentTarget.parentNode.querySelector('input').value = ' ';
                setlocalStorage(todoList);
            }

        },
        /* 删除代办条目 */
        deletetodo: function (item, ev) {
            console.log(item);
            this.todo.splice(this.todo.indexOf(item), 1);
            setlocalStorage(todoList);
        },
        /* 删除已完成条目 */
        deletecompt: function (item, ev) {
            console.log(item);
            this.compt.splice(this.compt.indexOf(item), 1);
            setlocalStorage(todoList);
        },
        /* 未完成事件标记已完成 */
        tocompt: function (item, ev) {
            console.log(item);
            this.compt.unshift(item);
            this.todo.splice(this.todo.indexOf(item), 1);
            setlocalStorage(todoList);
        },
        /* 已完成事件重新加入未完成列表 */
        retodo: function (item, ev) {
            console.log(item);
            this.todo.unshift(item);
            this.compt.splice(this.compt.indexOf(item), 1);
            setlocalStorage(todoList);

        }
    }
});
/* 定义localstorage的存储方法方便调用 */
function setlocalStorage(data){
    localStorage.setItem('todolist',JSON.stringify(data));
}

完整代码参考:github
效果参考:demo页面

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值