vue实现一个具有添加、删除、按关键字过滤的列表管理

直接贴代码了:

<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="UTF-8">

<meta name="viewport" content="width=device-width, initial-scale=1.0">

<meta http-equiv="X-UA-Compatible" content="ie=edge">

<title>Document</title>

<script src="./lib/vue.js"></script>

<link rel="stylesheet" href="./lib/bootstrap.css">

</head>

<body>

<div id="app">

<!-- //用一个div来放 文本框及添加按钮 -->

<div>

<label>

Id:

<input type="text" class="form-control" v-model="id">

</label>

<label>

Name:

<input type="text" class="form-control" v-model="name">

</label>

<input type="button" value="添加" class="btn btn-primary"@click="add()">

<label>

搜索关键字:

<input type="text" class="form-control" v-model="keywords">

</label>

<!-- //v-model实现数据双向绑定 -->

</div>

 

<!-- 定义一个列表 -->

<table>

<thead>

<tr>

<th width='50px'>Id </th>

<th width='50px'>Name </th>

<th width='100px'>Ctime </th>

<th width='50px'>Operation </th>

</tr>

</thead>

<tbody>

<tr v-for="item in search(keywords)" :key="item.id">

<td>{{item.id}}</td>

<td>{{item.name}}</td>

<td width='100px'>{{item.ctime}}</td>

<td>

<a href=""@click.prevent="del(item.id)">删除</a>

</td>

</tr>

</tbody>

</table>

</div>

<script>

var vm=new Vue({

el:'#app',

data:{

id:'',

name:'',

keywords:'',

list:[

{id:1,name:"汤博",ctime: new Date()},

{id:2,name:"汤伟",ctime: new Date()},

]

},

methods:{

add(){

var car={id:this.id,name:this.name,ctime:new Date()}

this.list.push(car)

},

del(id){

this.list.some((item,i)=>{

if(item.id==id)

{

this.list.splice(i,1);

return true;

}

})

},

search(keywords){

var newList=this.list.filter(item=>{

if(item.name.includes(keywords)){

return item

}

})

return newList

 

// this.list.forEach(element => {

// if(element.name.indexOf(keywords)!=-1){

// newList.push(element)

// }

// return newList

// })

 

// this.list.filter(item=>{

// if(item.name.includes(keywords)!=-1){

// newList.push(item)

// }

// return newList

// })

},

 

}

})

 

</script>

</body>

</html>

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值