<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1.0">
<title>Title</title>
<script src="./vue-js/vue.js"></script>
<link rel="stylesheet" href="./vue-css/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" @keyup.enter="add">
</label>
<input type="button" class="btn btn-primary" value="添加" @click="add">
<label>
搜索名称关键字:
<input type="text" class="form-control" v-model="keywords" id="search" v-focus v-color="'blue'">
</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>
<!-- 自定义search方法,在方法内部通过执行for循环,把索引符合搜索关键字的数据保存到一个新数组中-->
<tr v-for="item in search(keywords)" :key="item.id">
<td>{{ item.id }}</td>
<td v-text="item.name"></td>
<td>{{ item.ctime | dateFormat('yyyy-mm-dd') }}</td>
<td>
<a href="" @click.prevent="del(item.id)">删除</a>
</td>
</tr>
</tbody>
</table>
</div>
<div id="app2" v-color="'pink'" v-fontweight="900" v-fontsize="90">{{ dt | dateFormat('') }}</div>
<script>
//定义一个过滤器
Vue.filter('dateFormat',function (dateStr,pattern="") {
var dt = new Date(dateStr);
var y = dt.getFullYear().toString().padStart(2,'0');
var m = (dt.getMonth() + 1).toString().padStart(2,'0');
var d = dt.getDate().toString().padStart(2,'0');
//return y + '-' + m + '-' + d;
//return `${y}-${m}-${d}`;
if(pattern.toLowerCase() === 'yyyy-mm-dd'){
return `${y}-${m}-${d}`;
}else{
var hh = dt.getHours().toString().padStart(2,'0');
var mm = dt.getMinutes().toString().padStart(2,'0');
var ss = dt.getSeconds().toString().padStart(2,'0');
return `${y}-${m}-${d} ${hh}:${mm}:${ss}`;
}
});
//自定义全局按键修饰符即键盘码
Vue.config.keyCodes.f2 = 113;
Vue.directive('focus',{
bind:function () {//当指令绑定到元素上的时候,会立即执行这个bind函数,只执行一次
//el.style.color = 'red'
},
inserted:function(el){//元素插入DOM中的时候,会执行inserted函数
el.focus()
},
updated:function () {//当Vnode更新时候,会执行updated,可能会触发多次
}
});
Vue.directive('color',{
bind:function (el,binding) {//当指令绑定到元素上的时候,会立即执行这个bind函数,只执行一次
//el.style.color = 'red'
el.style.color = binding.value;
}
});
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(){
//1、获取id和name,直接从data上面获取
//2、组织出一个对象
//3、把这个对象,调用数组的相关方法,添加到当前data的list中去
//4、在Vue中,已经实现了数据的双向绑定,当修改data中的数据,vue会默认监听数据的改动,自动把最新的数据应用到页面上;
//5、VM中model数据的操作,同时在操作model数据的时候,指定业务逻辑操作
var car = {id:this.id, name:this.name, ctime:new Date()};
this.list.push(car);
this.id ='';
this.name = '';
},
del(id){//根据id删除数据
//1、根据id,找到要删除的一项索引
//2、找到索引后,直接调用splice方法
/*
this.list.some((item,i)=>{
if(item.id == id){
this.list.splice(i,1);
//在数组的some中,如果返回true,则立即终止这个数组的后续循环
return true;
}
})
*/
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;
*/
var newList = this.list.filter(item=>{
if(item.name.includes(keywords)){
return item;
}
})
return newList;
}
}
});
var vm2 = new Vue({
el:"#app2",
data:{
dt:new Date()
},
methods:{},
filters:{
dateFormat:function (dateStr,pattern = '') {
var dt = new Date(dateStr);
var y = dt.getFullYear();
var m = (dt.getMonth() + 1).toString().padStart(2,'0');
var d = dt.getDate().toString().padStart(2,'0');
//return y + '-' + m + '-' + d;
//return `${y}-${m}-${d}`;
if(pattern.toLowerCase() === 'yyyy-mm-dd'){
return `${y}-${m}-${d}`;
}else{
var hh = dt.getHours().toString().padStart(2,'0');
var mm = dt.getMinutes().toString().padStart(2,'0');
var ss = dt.getSeconds().toString().padStart(2,'0');
return `${y}-${m}-${d} ${hh}:${mm}:${ss} ~~~~~~~~`;
}
}
},
directives:{
'fontweight':{
bind:function (el,binding) {
el.style.fontWeight = binding.value
}
},
'fontsize':function(el,binding){//这个function等同于把代码写到了bind和update中去
el.style.fontSize = parseInt(binding.value) + 'px';
}
}
});
//document.getElementById('search').focus();
</script>
</body>
</html>