利用Vue框架写一个简单的todolist

本文介绍了一个基于Vue.js框架的To-do List应用实现,详细展示了HTML、CSS和JavaScript代码,包括组件化设计、事件监听、状态管理和响应式UI更新等关键技术点。

这个todolist使用三个部分:
HTML页面使用了SUI的Moblie的组件
js需要下载vue.js的源文件

HTML部分代码:

<div id="app">
        <!-- 头部结构 -->
        <header class="bar bar-nav">
            <a class="icon icon-star pull-left"></a>
            <a class="icon icon-edit pull-right"
            @click="hide()"
            ></a>
            <h1 class="title">标题</h1>
          </header>
          <!-- 内容区 -->
          <section>
              <input type="text" 
              v-show="!Tflag" 
              v-model="val"
              @keyup.enter="send(val)"
              >//输入生成卡片
            <div class="card"
            v-for="(item,index) in  newTodos"
            >
                <div class="card-content">
                  <div class="card-content-inner">{{item.title}}</div>
                  <button class="button button-danger pull-right"
                  @click="show(index)"
                  >
                      <i class="icon icon-remove"></i>
                  </button>
                  <button class="button button-success pull-right"
                  @click="select(index)"
                  :class="item.Xflag&&'button-fill'"
                  >
                        <i class="icon icon-check"></i>
                    </button>
                </div>
              </div>
          </section>
        <div class="mask"
        @click="Sflag=!Sflag"
        >
                  <div class="mask-bg" 
                  v-show="Sflag"
                  >
                      <div class="mask-content">
                          <p>你确定要抛弃我嘛?</p>
                          <button class="button button-fill button-danger pull-right"
                          @click="del(index)"
                          >抛弃</button>
                      </div>
                  </div>
        </div>
          <!-- 底部结构 -->
          <footer>
            <ul>
                <li v-for="item in btns">
                    <button class="button button-success"
                    :class="['button-'+item.style,type===item.txt&&'button-fill']"
                    @click="type=item.txt"
                    >
                        {{item.txt}}
                    </button>
                    
                </li>
            </ul>
          </footer>
    </div>

css代码:

*{
    margin:0;
    padding:0;
    list-style: none;
}
section{
    padding-top:54px;
}
.card-content{
    overflow: hidden;
    padding:10px;
}
.card-content button{
    margin-left:10px;
}
.mask{
    width: 100%;
    height: 100%;
}
.mask-bg{
    position: fixed;
    top:0;
    left: 0;
    z-index:99;
    width: 100%;
    height: 100%;
    background:rgba(204, 204, 204, 0.582);
}
.mask-content{
    width:80%;
    height: 130px;
    background:white;
    position: absolute;
    left:50%;
    margin-left:-40%;
    top:50%;
    margin-top:-65px;
    z-index: 100;
    border-radius: 10px;
}
.mask-content p{
    padding:10px;
}
.mask-content button{
    margin-right:20px;
    margin-bottom:5px;
}
footer ul{
   display: flex;
   justify-content: space-around;
   position: absolute;
   bottom:20px; 
   width: 100%;

}
footer ul li .button{
    width:80px;
    height: 80px;
    border-radius: 50%;
    line-height: 80px;
    text-align: center;
}

js的Vuejs代码

new Vue({
    el:'#app',
    data:{
        Tflag:true,//输入框的判断
        val:'',
        Xflag:true,//勾选框的判断
        todos:[{//生成的卡片是输入框数组
            id:1,
            title:'任务一',
            Xflag:true,
        },{
            id:2,
            title:'任务二',
            Xflag:true,
        }],
        Sflag:false,//勾选框是否勾中的判断
        btns:[{
            txt:'A',
            Bflag:true,//按钮的判断
            style:'success'
        },{
            txt:'F',
            Bflag:true,
            style:'warning'
        },{
            txt:'U',
            Bflag:true,
            style:'danger'
        }],
        type:"A",
    },
    methods: {
        hide(){//对输入框的隐藏
            this.Tflag=!this.Tflag;
        },
        send(val){//输入框进行生成卡片
            this.todos.push({//利用todos的数据形式,来把数据push进去
                id:this.todos.length+1,
                title:val,
                Xflag:true,
            });
            this.val='';//清空输入框
            this.Tflag=!this.Tflag;//隐藏输入框
        },
        del(index){//弹框的删除
            this.todos.splice(index,1)
        },
        show(index){//直接删除提示,删除根据勾选框进行判断
            if(this.todos[index].Xflag===false){
                this.Sflag=!this.Sflag;
            }else{
                this.todos.splice(index,1)
            }
        },
        select(index){//点击勾选框进行判断是否选中
            this.todos[index].Xflag=!this.todos[index].Xflag;
        }
    },
    computed: {
        
        alltodos(){//点击all按钮返回所有的值
            return this.todos
        },
        finish(){//点击finish按钮返回完成任务的所有值Xflag是勾选框的判断
            return this.todos.filter(item=>item.Xflag&&item)
        },
        unfinish(){
            return this.todos.filter(item=>!item.Xflag&&item)//遍历数组然后返回点击的相应值
        },
        newTodos(){//将原来的todos数据写成可变的,点击下面的按钮可以将每个部分的数据分开
            switch(this.type){
                case "A":
                    return this.alltodos;
                    break;
                case "F":
                    return this.finish;
                    break;
                case "U":
                    return this.unfinish;
                    break;
            }
        },
    },
})
Vue.js中创建一个简单的待办事项(Todo List)应用是一个很好的开始,它展示了如何利用组件化思想管理UI状态。以下是基本步骤: 1. **安装依赖**: 首先确保已安装Node.js和Vue CLI。在命令行中运行`vue create todo-app`创建一个新的Vue项目。 2. **设置基本结构**: 在`src/components`目录下创建两个文件夹:`Todos` 和 `TodoItem.vue`。`Todos.vue`将作为整体列表组件,而`TodoItem.vue`用于单个待办项。 3. **TodoItem.vue** (单个待办项): - 创建一个模板,包含输入框、按钮和复选框。 ```html <template> <li> <input v-model="item.text" type="text" /> <button @click="completeItem">完成</button> <span class="toggle"> <input type="checkbox" :checked="item.completed" /> </span> <span>{{ item.text }}</span> </li> </template> ``` - 添加数据属性和计算属性。 ```js <script> export default { data() { return { item: { text: '', completed: false, }, }; }, methods: { completeItem() { this.item.completed = !this.item.completed; }, }, }; </script> ``` 4. **Todos.vue** (待办事项列表): - 引入`TodoItem`组件,并绑定一个数组来存储待办事项。 ```html <template> <div> <h1>待办事项列表</h1> <ul> <li v-for="(todo, index) in todos" :key="index"> <TodoItem :item="todo" @completeItem="handleCompleteItem(index)" /> </li> </ul> <form @submit.prevent="addTodo"> <input v-model="newTodoText" placeholder="添加新任务" /> <button>Add</button> </form> </div> </template> ``` - 定义数据和方法。 ```js <script> import TodoItem from '@/components/TodoItem.vue'; export default { components: { TodoItem }, data() { return { todos: [], newTodoText: '', }; }, methods: { addTodo() { if (this.newTodoText.trim()) { this.todos.push({ text: this.newTodoText, completed: false }); this.newTodoText = ''; } }, handleCompleteItem(index) { this.todos[index].completed = !this.todos[index].completed; }, }, }; </script> ``` 5. **样式**: 在`src/App.vue`或其他适合的地方,添加一些基本CSS样式美化列表和组件。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值