vue--综合练习(品牌列表渲染)
2023-09-27 14:26:51 时间
需求
- 数据渲染
- 添加数据
- 删除数据
- 数据过滤
界面
代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>品牌列表渲染</title>
<script src="https://cdn.bootcss.com/vue/2.6.8/vue.min.js"></script>
<link href="https://cdn.bootcss.com/twitter-bootstrap/3.4.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" class="form-control" v-model="id">
</label>
<label>
Name:
<input type="text" class="form-control" v-model="name">
</label>
<label>
Color:
<input type="text" class="form-control" v-model="color">
</label>
<input type="button" value="添加" class="btn btn-primary" @click="add">
<label>
关键字搜索(color,name):
<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>Color</th>
<th>Ctime</th>
<th>Operation</th>
</tr>
</thead>
<tbody>
<!-- 之前, v-for 中的数据,都是直接从 data 上的list中直接渲染过来的 -->
<!-- 现在, 我们自定义了一个 search 方法,同时把 所有的关键字,通过传参的形式,传递给了 search 方法 -->
<!-- 在 search 方法内部,通过 执行 for 循环, 把所有符合 搜索关键字的数据,保存到 一个新数组中,返回 -->
<tr v-for="item in search(keywords)" :key="item.id">
<td v-text='item.id'></td>
<td v-text='item.name' ></td>
<td v-text='item.color' ></td>
<td v-text='item.ctime'></td>
<td> <a href="#" @click.prevent="del(item.id)">删除</a> </td>
</tr>
</tbody>
</table>
</div>
<script>
let vm=new Vue({
el:"#app",
data:{
id:"",
name:"",
color:"",
keywords:"",
list:[
{
id:"1",
name:"奔驰",
color:"white",
ctime:new Date()
},{
id:"2",
name:"宝马",
color:"white",
ctime:new Date()
},{
id:"3",
name:"福特",
color:"red",
ctime:new Date()
}
]
},
methods:{
add(){
this.list.push({"id":this.id,"name":this.name,"color":this.color,"ctime":new Date()})
this.id=""; this.name="";this.color="";
},
del(id){
if(confirm("确定删除吗?")){
//根据Id删除数据,并确定对应索引,调用数组的splice方法删除
this.list.forEach((item,i)=>{
if(item.id==id){
this.list.splice(i,1);
}
})
}
},
search(keywords){
//关键词过滤
return this.list.filter((item)=>{
if(item.name.includes(keywords)||item.color.includes(keywords)){
return item;
}
})
}
}
})
</script>
</body>
</html>
学习资源推荐:https://blog.csdn.net/qq_42813491/article/details/90213353
相关文章
- 在vue中使用jsx语法
- Vue 列表渲染
- 精品微信小程序竞赛管理平台+后台管理系统|前后分离VUE
- 精品springboot+VUE的学生宿舍管理系统设计与实现
- Vue.js:使用v-bind在列表选项上绑定索引
- Vue.js:Webpack基础入门学习笔记
- vue登录配合路由前置守卫小dome(vue-router配合vuex实现案例)
- 134/(前端)订单列表布局与显示——子组件挂载的方式与分类管理vue的结构搭建
- Vue学习(五):列表渲染
- Vue 显示下拉列表
- vue 条件渲染方式
- 优化无限列表性能vue-virtual-scroll-list【测试90w条数据】
- Vue技术12.4列表排序
- Vue知识(二)组件化开发
- vue点击列表变亮(v-for和v-bind的结合)
- Vue列表展示(循环显示)
- 微信 jssdk 逻辑在 vue 中的运用
- 【笔记】Vue Element+Node.js开发企业通用管理后台系统——电子书列表页面开发
- 【笔记】Vue Element+Node.js开发企业通用管理后台系统——用户登录(下)(搭建https、拦截器、跨域、数据库)
- Vue和iview-admin搭建的项目进行兼容
- 一步一步用hbuilder配置nodesjs环境做vue.js开发
- Spring Boot + Vue 前后端分离,两种文件上传方式总结
- vue源码系列文章good
- Vue scrollBehavior 滚动行为
- 前端学习之VUE基础五(Vue-Router):路由的基本概念、vue-router的基本使用、vue-router嵌套路由、vue-router动态路由匹配、vue-router编程式导航