当前位置: 移动技术网 > IT编程>脚本编程>vue.js > Vue.js实现一个todo-list的上移下移删除功能

Vue.js实现一个todo-list的上移下移删除功能

2017年12月12日  | 移动技术网IT编程  | 我要评论

pdfl初始化失败,CA4367,灯带

如图,a simple todo-list长这样

这是一个基于vue.js的一个简单的todo-list小demo。首先要实现添加非空list,点击list切换finished状态这样的一个效果,推荐学习地址---->

接下来是实现的一个上移,下移,删除的效果图:

删除效果:

讲一下思路:

上移-----首先将鼠标所指list的内容插入到上一条上面,然后删除鼠标所指的list(也就是this.items[index]),运行代码便可实现上移的效果,或者将上下两条list的内容进行调换也是可以的。

删除-----这里和上下移一样,主要是用到了操作数组的splice这个方法,既可以添加也可以删除,不懂的去补一下

小二~上代码:

----app.vue---- 

<div><input v-model="newitem" v-on:keyup.enter="addnew"></div> 
<div class="box-center"> 
 <ul class="box-list"> 
  <li v-for="item ,index in items" v-bind:class="{finished:item.isfinished}"  

  v-on:mouseover="movebtn(item)" v-on:mouseout="leavebtn(item)"> 
  <span v-on:click="togglefinished(item)" v-bind:class="{bgyellow:item.isbgyellow}">{{item.label}}</span> 
  <span class="list-btn" v-show="item.isshow"> 
   <button v-on:click="moveup(index,item)">上移</button> 
   <button v-on:click="movedown(index,item)">下移</button> 
   <button v-on:click="deletebtn(index)">删除</button> 
  </span> 
 </li> 
</ul>  
t;/div> 

----store.js---- 

const storage_key = 'todos-vuejs' 
export default { 
 fetch () { 
  return json.parse(window.localstorage.getitem( 
   storage_key) || '[]') 
 }, 
 save (items) { 
  window.localstorage.setitem(storage_key,json.stringify( 
   items)) 
 } 
} 
----app.vue---- 
<span style="font-size:14px;"><script> 
import store from './store' 
export default { 
 data: function() { 
  return { 
   title: 'a simple todo-list', 
   items: store.fetch(), 
   newitem: '', 
   msg:'点击按钮',  
   isshow: false, 
   isblock: true, 
   isbgyellow: false, 
   leftpx:0, 
   toppx:0 
  } 
 }, 
 watch: { 
  items: { 
   handler: function(items) { 
    store.save(items) 
   }, 
   deep: true 
  } 
 }, 
 methods: { 
  togglefinished: function(item) { 
   item.isfinished = !item.isfinished 
  }, 
  show:function ($event) { 
   $event.cancelbubble=true; 
   this.isblock = false; 
   this.toppx = ($event.clienty); 
   this.leftpx = ($event.clientx); 
  }, 
  stop:function(event){ 
    this.isblock = true; 
  }, 
  movebtn:function(item) { 
   // console.log(item.label) 
   item.isshow = true; 
  }, 
  leavebtn:function(item) { 
   item.isshow = false; 
  }, 
  addnew: function() { 
   //非空才可以添加 
   if(this.newitem!=''){ 
    this.items.push({ 
     label: this.newitem, 
     isfinished: false 
    })     
   } 
   this.newitem = ''; 
  }, 
  moveup:function(index,item) { 
   //在上一项插入该项 
   this.items.splice(index-1,0,(this.items[index])); 
   //删除后一项 
   this.items.splice(index+1,1); 
   item.isshow = false; 
   if(index == 0) { 
    alert("到顶啦!"); 
   } 
  }, 
  movedown:function(index,item) { 
   //在下一项插入该项 
   this.items.splice(index+2,0,(this.items[index])); 
   // 删除前一项 
   this.items.splice(index,1); 
   item.isshow = false; 
   if(index == this.items.length-1) { 
    alert("已经是最后一项啦!"); 
   } 
  }, 
  deletebtn:function(index) { 
   this.items.splice(index,1); 
  } 
 }, 
} 
</script></span><span style="font-size: 18px;"> 
</span> 

套路就是在html中插入方法或者class,methods中对数据进行操作~

总结:

这是本小白入门vue.js学习的第一个demo,有一些jquery的观念不能很好的转变,总是习惯性的操作dom节点,殊不知vue可以有更好的方式去实现

以上所述是小编给大家介绍的vue.js实现一个todo-list的上移下移删除功能,希望对大家有所帮助

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网