示例:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>综合练习</title>
<script src="vue.js"></script>
<link rel="stylesheet" href="bootstrap.css">
</head>
<body>
<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">
</label>
<input type="button" value="添加" class="btn btn-primary" @click="add">
<label>
搜索关键字:
<input type="text" class="form-control" v-model="keywords">
</label>
</div>
</div>
<table class="table table-bordered table-hover table-striped">
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>CTime</th>
<th>Operation</th>
</tr>
</thead>
<tbody>
<tr v-for="item in search(keywords)" :key="item.id">
<th>{{ item.id }}</th>
<th>{{ item.name }}</th>
<th>{{ item.CTime | dateFormat() }}</th>
<th>
<a href="" @click.prevent="del(item.id)">删除</a>
</th>
</tr>
</tbody>
</table>
</div>
<script>
Vue.filter('dateFormat',function(dateStr,patten=""){
var dt = new Date(dateStr);
var y = dt.getFullYear();
var m = dt.getMonth()+1;
var d = dt.getDate();
if (patten.toLowerCase() == "yyyy-mm-dd") {
return `${y}-${m}-${d}`;
}else {
var hh = dt.getHours();
var mm = dt.getMinutes();
var ss = dt.getSeconds();
return `${y}-${m}-${d} ${hh}:${mm}:${ss}`;
}
});
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){
/*this.list.some((item,i)=>{
if (item.id == id){
this.list.splice(i,1);
return true
}
})*/
var index = this.list.findIndex(item => {
if (item.id == id){
return true;
}
});
console.log(index);
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 this.list.filter(item => {
if (item.name.includes(keywords)){
return item;
}
})
}
}
});
</script>
</body>
</html>
效果: