<!DOCTYPE html>
<html>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<script src="vue.js"></script>
<link rel="stylesheet" href="bootstrap.min.css">
<head>
</head>
<div id="app">
<div class="panel panel-primary">
<div class="panel-heading">
<h3 class="panel-title">添加品牌</h3>
</div>
<div class="panel-body form-inline">
<label>
ID:
<input type="text" class="form-control" v-model="id">
</label>
<label>
Name:
<input type="text" class="form-control" v-model="name">
<input type="button" value="添加" class="btn btn-primary" @click="add">
</label>
<label>
搜索名称关键字:
<input type="text" class="form-control" v-model="keywords">
</label>
</div>
</div>
<table class="table table-bordered table-hover table-striped">
<thead>
<tr>
<td>ID</td>
<td>Name</td>
<td>Ctime</td>
<td>Operation</td>
</tr>
</thead>
<tbody>
<tr v-for="item in search(keywords)" :key="item.id">
<td>{{ item.id }}</td>
<td>{{ item.name }}</td>
<td>{{ item.ctime }}</td>
<td>
<a href="#" @click.prevent="del(item.id)">删除</a>
</td>
</tr>
</tbody>
</table>
</div>
<body>
<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);
this.id = this.name = '';
},
del(id){
var index = this.list.findIndex(item =>{
if(item.id == id){
return true;
}
})
this.list.splice(index,1);
},
search(keywords){
var newList = [];
this.list.forEach(item => {
if(item.name.indexOf(keywords) != -1){
newList.push(item);
}
// return newList; 为什么在这里 return 的newList 为空呢?
});
return newList; //在这里return 就没问题呢?
}
}
})
</script>
</body>
</html>
search(keywords){..}
里的内容, 为什么第一个return newlist 没值呢?
谢谢~