涉及到的知识点
- bootstrap.css
- 全局过滤器的使用
- 私有过滤器的使用
- 全局键盘码的设定
- 定义全局指令
- 对象数组的添加、删除、查询
<!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>基础内容回顾</title>
<script src="lib/vue.js">
</script>
<link href="lib/bootstrap.css" rel="stylesheet">
<!-- 新 Bootstrap 核心 CSS 文件 -->
<!-- <link href="http://apps.bdimg.com/libs/bootstrap/3.2.0/css/bootstrap.min.css" rel="stylesheet> -->
</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" v-model="id" class="form-control"> </label>
<label>
名称:
<input type="text" v-model="name" class="form-control" @keyup.enter="add"></label>
<input type="button" value="添加" class="btn btn-primary" @click="add">
<label>
按关键字查询:
<input type="text" class="form-control" v-model="keywords" v-focus v-color>
</label>
<input type="button" class="btn btn-primary" value="搜索">
</div>
</div>
<table class="table table-borderd table-hover table-striped">
<tbody>
<thead>
<th>Id</th>
<th>课程名称</th>
<th>时间</th>
<th>操作</th>
</thead>
</tbody>
<tbody>
<tr v-for="item in search(keywords)" :key="item.id">
<td>{{item.id}}</td>
<!-- <td>{{item.name | nameFilter('替换') }}</td> -->
<td>{{item.name}}</td>
<td>{{item.ctime| timeFilter('')}}</td>
<td><a href="" @click.prevent="del(item.id)">删除</a></td>
</tr>
</tbody>
</table>
<p v-color v-fontweight="600" v-fontsize="50">{{msg}}</p>
</div>
<script>
//过滤器的使用
// Vue.filter('nameFilter',function(msg,arg){
// return msg.replace(/语/g,arg)
// })
//全局过滤器 进行时间的格式化
Vue.filter('timeFilter', function (currentdate, forma) {
var da = new Date(currentdate)
var y = da.getFullYear()
var m = (da.getMonth() + 1).toString().padStart(2, '0') //月份是从0开始的
var d = da.getDate().toString().padStart(2, '0')
if (forma.toLowerCase() == 'yyyy-mm-dd') {
return `${y}-${m}-${d}`
}
else {
// padStart方法在规定长度中,不足长度在最前面则按规则补位
//padEnd方法在规定长度中,不足长度在最后面则按规则补位
var hh = da.getHours().toString().padStart(2, '0')
var mm = da.getMinutes().toString().padStart(2, '0')
var ss = da.getSeconds().toString().padStart(2, '0')
return ` ${y}-${m}-${d}-${hh}:${mm}:${ss}`
}
})
//全局键盘码的设定
Vue.config.keyCodes.f1 = 112
//定义全局指令 Vue.directive
//定义获取焦点指令 v-focus 参数1指令名称,设定时不加v- 调用时需要加v- 参数2是一个对象
Vue.directive('focus', {
//js相关的代码,一般放在inserted指令中执行
inserted: function (el) {
el.focus()
}
})
//自定义一个设置字体颜色的指令
//样式通过指令绑定给了元素,不管元素有没有被插入到页面中去,已经存在了内联样式
//內联语句处理器中访问原生DOM事件
Vue.directive('color', {
bind: function (el) {
//css相关代码,一般放在bind指令中执行
el.style.color = 'red'
}
})
new Vue({
el: '#app',
data: {
id: '',
name: '',
keywords: '',
msg: '我是一个好学生',
list: [
{ id: 1, name: '语文', ctime: new Date() },
{ id: 2, name: '数学', ctime: new Date() },
{ id: 3, name: '英语', ctime: new Date() },
]
},
methods: {
add() {
this.list.push({ id: this.id, name: this.name, ctime: new Date() });
this.id = '';
this.name = '';
},
del(id) {
//第一种删除方法 使用some
// 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;
}
})
this.list.splice(index, 1)
},
search(keywords) {
// var newList = []
// this.list.forEach(items => {
// if (items.name.indexof(keywords) !=-1) {
// newList.push(item)
// }
// })
// return newList
return newList = this.list.filter(item => {
if (item.name.includes(keywords)) {
return item
}
})
},
},
directives: { //定义私有的自定义指令 v-fontweight
//fontWeight注意大小写
'fontweight': {
bind: function (el, bindings) {
el.style.fontWeight = bindings.value
}
},
//注意大小写
'fontsize': function (el, bindings2) {
el.style.fontSize = parseInt(bindings2.value) + 'px'
}
}
});
</script>
</body>
</html>