品牌列表案例
1.使用到vue框架和bootstrap样式库;
2.实现对列表的数据的添加、删除操作;
<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" value="添加" class="btn btn-primary" @click="add()">
<label for="">
搜索名称关键字:
<input type="text" class="form-control" v-model="keywords" id="search" v-focus>
</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>
<!--
1.之前 v-for 中的数据,都是直接从 data 上的 list 中直接渲染过来的
2.现在自定义了一个 search 方法,同时把所有的关键字通过传参的形式,传递给了 search 方法
3.在 search 方法内部,通过执行 for 循环,把所有符合关键字的数据,保存到一个新数组中,返回
-->
<tr v-for="item in search(keywords)" :key="item.id">
<td>{{ item.id }}</td>
<td v-text="item.name"></td>
<!-- 过滤器dateFormat -->
<td>{{ item.ctime | dateFormat('') }}</td>
<td>
<a href="#" @click.prevent="del(item.id)">删除</a>
</td>
</tr>
</tbody>
</table>
</div>
<script>
// 使用方法格式:Vue.filter('过滤器名称', function(){})
// 使用全局过滤器,进行时间的格式化,返回时间格式字符串文本
// 使用ES6中的padStart()方法进行字符串长度补全,使在一位数字前补0
Vue.filter('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.getDay().toString().padStart(2, '0');
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() 全局指令 v-focus
Vue.directive('focus', {
bind: function (el) {
// 只调用一次,指令第一次绑定到元素时调用。在这里可以进行一次性的初始化设置。
},
inserted: function (el) {
// 表示元素插入到DOM中时执行 inserted 函数
// 被绑定元素插入父节点时调用 (仅保证父节点存在,但不一定已被插入文档中)。
el.focus()
},
updated: function (el) {
// 所在组件的 VNode 更新时调用,但是可能发生在其子 VNode 更新之前。
}
})
// 创建 Vue 实例, 得到ViewModel
var vm = new Vue({
el: '#app',
data: {
id: '',
name: '',
keywords: '',
list: [{
id: 1,
name: '奔驰',
ctime: new Date()
}, {
id: 2,
name: '宝马',
ctime: new Date()
}]
},
methods: {
// 1.获取到id和name,直接从data上获取
// 2.组织出一个对象
// 3.把这个对象,调用数组的相关方法,添加到当前data上的list中
// 4.注意:vue中,已经实现了数据的双向绑定,每当我们修改了data中的数据,vue会默认监听到数据的改动,自动把最新的数据,应用到页面上;
add() {
var car = {
id: this.id,
name: this.name,
ctime: new Date()
};
this.list.push(car);
this.id = this.name = '';
},
del(id) {
// 1.如何根据 id ,找到要删除这一项的索引(some)
// 2.如果找到索引了,直接调用 数组的 splice 方法
// 方法一:some方法
// this.list.some((item, i) => {
// if (item.id == id) {
// this.list.splice(i, 1);
// // 在数组的 some 方法中,如果 return true ,就会立即终止这个数组的后续循环
// return true;
// }
// })
// 方法二:findIndex方法
var index = this.list.findIndex(item => {
if (item.id == id) {
return 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
}
})
// return newlist;
}
}
});
</script>
</body>