vue 总结 ( 修改,添加,删除list中的数据)

  var vm=new Vue({
    	      el:'#app',
    	      data:{
    	      	id:'1',
    	      	name:'',
    	      	soso:'',
    	      	list:[{"id":1,"name":"大黄蜂","time":new Date()},
    	          {"id":2,"name":"奔驰","time":new Date()}
    	      ]},
    	      methods:{
    	      	add:function(){
    	      		this.list.push({"id":this.id,"name":this.name,"time":new Date()})
    	      	},
    	      	delte:function(id){  
    	      		    if(!confirm("确定要删除吗?")){
                    	return ;
               }
    	      		  // 默认去遍历list集合,将集合中的每个元素传入到function的item里,
    	      		var index=this.list.findIndex(function(item){
    	      			return item.id==id;
    	      		})
    	      		  this.list.splice(index,1);  
    	      	},
    	      	//根据关键字实现数组的过滤
    	      	search:function(soso){
    	      		var newList=[];
    	      		this.list.forEach(item=>{
    	      			if(item.name.indexOf(soso)!=-1){
    	      				newList.push(item)
    	      			}
    	      		})
    	      		console.log(newList)
    	      		return newList;
    	      	}
    	    
    	      }
          })


//根据索引查找记录
           var trIndex=1;
			vm.list.findIndex(function (item,index){
				if(index==trIndex){
					alert(item.name);
				}
			})
//根据list某个属性查找记录
           vm.userList.findIndex(function(item){
              if (item.id==Id){
               vm.tel=item.phone;
               vm.name=item.name;
               vm.Id=Id;
              }
            })
//添加记录
  vm.List.push({"name":vm.name,"tel":vm.tel,"positioName":vm.positioName,"id":id});
//删除记录
  var index = vm.List.findIndex(function(item) {
                      if (item.id == id){
                      return true;
                    }
                  })
                    vm.List.splice(index, 1)
//修改list某条记录
         //要修改的记录 索引
			var index=1;
 		   var data={"id":3,"name":"新奔驰","time":new Date()}
 		   //1代表修改的记录条数
 		   vm.list.splice(index,1,data)

 

你可能感兴趣的:(vue学习汇总)