备忘录 vue实现 添加修改删除

一. 效果图

在这里插入图片描述

二. 具体功能实现
  1. 使用 Date对象 获取当前时间
    在这里插入图片描述
  2. 使用 push方法 为列表添加数据
    在这里插入图片描述
  3. 使用 splice方法 删除数据
    在这里插入图片描述
  4. 修改数据的同时把整条记录删除
    在这里插入图片描述
三. 整体代码
<template>
  <div id="app">
      <header>
        <h2 class="title">备忘录</h2>
        <h4 class="time">{{now}}</h4>
      </header>
      <section>
        <textarea v-model="text" class="text" placeholder="今天天气真好!"></textarea>
        <button @click="textComplete" class="completion">完成</button>
      </section>
      <ul class="nav">
        <li v-for="(node,index) of textNodes" :key="index">
         <div class="content">
            <p>{{node.content}}</p>
            <p >{{node.date}}</p>
         </div>
         <div class="btn">
           <button @click="textDelete(index)">删除</button><!--删除数据-->
           <button @click="textPut(index)">修改</button> <!--修改数据-->
         </div>
        </li>
      </ul>
  </div>
</template>

<script>
export default {
  data(){
    return{
      textNodes:[
        {content:"今天完成了200个单词",date:"2020/5/19 星期二"},
        {content:"今天学习了vue",date:"2020/5/18 星期一"},
        {content:"今天学习了node.js",date:"2020/5/17 星期日"}
      ],
      now:this.getTime(),
      text:""
    }
  },
  methods:{
    //计算当前时间
   getTime(){  
     var now =new Date();
     var arr=["日","一","二","三","四","五","六"];
    var day=now.getDay();//得到的是数字,然我们想要的是星期几
    var dayTime=arr[day];
    return now.toLocaleDateString()+"  "+"星期"+dayTime;
   },
   textComplete(){ 
     //为列表添加数据
       this.textNodes.push({content:this.text,date:this.now});
       this.text='';
   },
   //删除数据
   textDelete(index){ 
     this.textNodes.splice(index,1);
   },
   //修改数据
   textPut(index){   
    this.text=this.textNodes[index].content;//先获取要修改的值
    this.textNodes.splice(index,1);//同时把整条记录都删去
    return this.text;
   }
  }
}
</script>

<style scoped>
*{
  margin:0px;
  padding:0px;
}
#app{
  width:360px;
  border:1px solid #f1f1f1;
  box-shadow:-1px -1px 3px #eee;
  margin:50px auto;
  background-color:pink;
}
header{
  display:flex;
  justify-content: space-between;/**两端对齐 */
}
header .title ,.time{
  color:#f7f710;
}
header .time{
  font-size:16px;
  align-self: flex-end; /**底端对齐*/
}
section{
  position:relative;
  width:360px;
  height:300px;
  border-bottom:1px solid #ccc;
}
section .text{
  box-sizing:border-box;
  width:360px;
  height:300px;
  border:none;
  outline:none;
  color:#aaa;
  font-size:16px;
  padding:10px 0px 0px 6px; 
  background-color:rgb(253, 226, 231);
}
textarea::-webkit-input-placeholder{
   color:#333;
   font-size:16px;
}
section .completion{
  position:absolute;
  right:8px;
  bottom:5px;
  width:40px;
  height:30px;
  background-color:#f7f710;
  border-radius:4px;
  border:none;
  outline:none;
}
.nav li{
  display:flex;
  list-style: none;
  width:360px;
  border-bottom:1px solid #fff;
  padding:6px 0px 6px 0px;
}
.nav li .content{
  flex-grow:1;
}
.nav .content p{
  color:#333;
  font-size:16px;
  width:260px;
  word-wrap: break-word;
}
.nav .btn{
  align-self: center;
}
.nav li .btn button{
  margin-right:10px;
  width:40px;
  height:30px;
  background-color:rgb(96, 96, 243);
  color:#fff;
  border-radius:4px;
  border:none;
  outline:none;
}
</style>
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值