<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=0">
<link href="../lib/bootstrap.css" rel="stylesheet">
<title>Title</title>
</head>
<body>
<div class="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" @keyup.f2="add">
</label>
<input type="button" class="btn btn-primary" value="添加" @click="add">
<label>
搜索名稱關鍵字:
<input type="text" class="form-control" v-model="keyword" v-focus v-color="'green'">
</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(keyword)" :key="item.id">
<td>{{ item.id }}</td>
<td>{{ item.name }}</td>
<td>{{ item.time | timeFilter }}</td>
<td>
<a href="" @click.prevent="del(item.id)">刪除</a>
</td>
</tr>
</tbody>
</table>
</div>
<script src="../lib/vue-2.4.0.js"></script>
<script>
/*自定義全局指令(獲取焦點)*/
Vue.directive('focus',{
inserted:function (el) {
el.focus();
}
});
/*自定義全局指令(設置顏色)*/
Vue.directive('color',{
bind:function (el,binding) {
el.style.color = binding.value;
}
});
/*自定義按鍵修飾符*/
Vue.config.keyCodes.f2=113;
/*全局過濾器,過濾時間形式*/
Vue.filter('timeFilter',function (data) {
var time = new Date(data);
var y = time.getFullYear();
var mm = (time.getMonth()+1).toString().padStart(2,'0');
var d = time.getDate().toString().padStart(2,'0');
var h = time.getHours().toString().padStart(2,'0');
var m = time.getMinutes().toString().padStart(2,'0');
var s = time.getSeconds().toString().padStart(2,'0');
return `${y}-${mm}-${d} ${h}:${m}:${s}`
});
var vm = new Vue({
el: '.app',
data: {
id: '',
name: '',
keyword:'',
list: [{
id: 1,
name: '寶馬',
time: new Date()
}]
},
methods: {
/*添加*/
add(){
var current = {
id: this.id,
name: this.name,
time: new Date()
};
this.list.push(current);
this.id = this.name = '';
},
/*刪除*/
del(id){
/*根據id獲取當前索引的數據,使用數組的splice方法刪除*/
var index = this.list.findIndex(function (item) {
if(item.id == id){
return true;
}
});
this.list.splice(index,1);
},
search(keyword){
/*遍歷數組,過濾*/
return this.list.filter(function (item) {
//如果這個元素的name值包含了keyword搜索值,返回這個元素
if(item.name.includes(keyword)){
return item;
}
})
}
}
});
</script>
</body>
</html>