vue基础

1. 品牌管理案例
实现功能:
1. 添加新品牌

2. 删除品牌

3.修改品牌

4. 根据条件筛选品牌

步骤:
1.1、搭建项目的基本界面的结构
引入bootstrap
引入vue
1.2、实现表格的渲染
使用v-for进行表单的渲染

注意设置key属性

1.3、实现新增的功能
数据绑定,为了能在后面拿到这数据。

定义一个方法,给按钮添加一个事件。

点击这个按钮之后为什么会进行页面的刷新,因为这个按钮是定义在表单里的,所以点击这个按钮会触发表单的提交,这个时候可以组织按钮的默认事件。

绑定事件的方法可以加括号也可以不加,加上括号之后可以给这个方法传递参数。

//添加
addCar() {
// 判断是否为空
if (this.id && this.name) {
//判断ID是否重复
if (this.carList.some((item) => {
return item.id == this.id
})) {
alert("ID重复");
// 清空数据
this.id = ''
this.name = ''
} else {
this.carList.push({
// 获取id和name
// 添加到carlist
id: this.id,
name: this.name,
ctime: new Date()
});
// 清空数据
this.id = ''
this.name = ''
}
} else {
alert("数据为空")
}
},
1.4、实现删除的功能
获取删除元素的索引
要想获取索引,需要先获取id,这个id可以传在调用方法的时候传进
根据id获取索引
遍历的方式
通过es6提供的some方法
调用数组的方法进行删除
使用数组的splice方法
deleteCar(id) {
// splice() 方法通过删除或替换现有元素或者原地添加新的元素来修改数组,并以数组形式返回被修改的内容。此方法会改变原数组。
// 参数1 开始位置 参数二是删除个数

//拿到位置,删除一个
// this.carList.splice(index,1)
// 根据id拿到位置
// findIndex()方法返回数组中满足提供的测试函数的第一个元素的索引。否则返回-1。

this.carList.splice(this.carList.findIndex((item) => {
return item.id == id
}), 1)
},
1.5、实现修改的功能
amendCar(name) {


ietm.name == this.reviseName
},
1.6、实现查询的功能
getCarList() {

// 处理 看一下包含关键字的汽车
return this.carList.filter((item) => {
// return true
// return item.name.search(this.keyWord) != -1
return item.name.includes(this.keyWord)
})
}
},
1.7、完整代码
<!DOCTYPE html>
<html>

<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>
<link rel="stylesheet" href="./bootstrap-3.4.1-dist/css/bootstrap.min.css">
<script src="./vue-2.4.0.js"></script>
<script src="./jquery-1.11.1.min.js"></script>
<script src="./bootstrap-3.4.1-dist/js/bootstrap.min.js"></script>
</head>

<body>
<div id='app'>

<div class="panel panel-info">
<div class="panel-heading">
<h3 class="panel-title">添加项目</h3>
</div>
<div class="panel-body">

<form action="" method="POST" class="form-inline" role="form">

<div class="form-group">
<label class="" for="">ID</label>
<input v-model="id" type="text" class="form-control" id="" placeholder="">
</div>
<div class="form-group">
<label class="" for="">Name</label>
<input v-model="name" type="text" class="form-control" id="" placeholder="">
</div>

<button type="submit" class="btn btn-primary" @click.prevent="addCar">添加</button>
<div class="form-group">
<label class="" for="">搜索</label>
<input v-model="keyWord" type="text" class="form-control" id="" placeholder="">
</div>
</form>

</div>
</div>

<table class="table table-hover table-bordered">
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>CTime</th>
<th>Delete</th>
<th>operation</th>
</tr>
</thead>
<tbody>
<tr v-for="(item,index) in getCarList()">
<td>{{item.id}}</td>
<td>{{item.name}}</td>
<td> {{item.ctime | moment('YYYY-MM-DD hh:mm:ss') }}</td>
<td><button class="btn" data-toggle="modal" data-target="#myModal" @click="amendCar(item)">修改Name</button></td>
<td><button class="btn" data-toggle="modal" data-target="#myModal1" @click="deleteCar(item.id)">删除</button>
<!-- 按钮触发模态框 -->
<!-- <button class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal">开始演示模态框</button> -->
<!-- 模态框(Modal) -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
</div>
<div class="modal-body">
<input type="text" v-model="reviseName">
</div>
<div class="modal-footer">
<button type="button" class="btn btn-primary" id="Gai">提交更改</button>
</div>
</div>
<!-- /.modal-content -->
</div>
<!-- /.modal -->
</div>
<div class="modal fade" id="myModal1" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-primary" id="del">确认删除</button>
</div>
</div>
<!-- /.modal-content -->
</div>
<!-- /.modal -->
</div>
</td>
</tr>
</tbody>
</table>

</div>

<script>
Vue.filter("moment", function(data, format) {
// 获取时间
let Y = data.getFullYear()
let M = (data.getMonth() + 1).toString().padStart(2, 0)
let D = (data.getDate()).toString().padStart(2, 0)
let H = (data.getHours()).toString().padStart(2, 0)
let Mi = (data.getMinutes()).toString().padStart(2, 0)
let S = (data.getSeconds()).toString().padStart(2, 0)
return format.replace("YYYY", Y).replace("MM", M).replace("DD", D).replace("hh", H).replace("mm", Mi).replace("ss", S)
})
const vm = new Vue({
el: '#app',
data: {
id: '',
name: '',
keyWord: '',
reviseName: '',
carList: [{
id: 1,
name: '宝马',
ctime: new Date()
}, {
id: 2,
name: '奔驰',
ctime: new Date()
}]
},
methods: {
//添加
addCar() {
// 判断是否为空
if (this.id && this.name) {
//判断ID是否重复
if (this.carList.some((item) => {
return item.id == this.id
})) {
alert("ID重复");
// 清空数据
this.id = ''
this.name = ''
} else {
this.carList.push({
// 获取id和name
// 添加到carlist
id: this.id,
name: this.name,
ctime: new Date()
});
// 清空数据
this.id = ''
this.name = ''
}
} else {
alert("数据为空")
}
},
//删除
/* 第一种方法 */
// deleteCar(index) {
// splice() 方法通过删除或替换现有元素或者原地添加新的元素来修改数组,并以数组形式返回被修改的内容。此方法会改变原数组。
// 参数1 开始位置 参数二是删除个数
// this.carList.splice(index, 1)
// },
/* 第二种方法 */
deleteCar(id) {
var del = document.getElementById("del");
console.log(del);
del.onclick = () => {
this.carList.splice(this.carList.findIndex((item) => {
return item.id == id;
}), 1)
}
// console.log(index);
// 根据id拿到位置
// findIndex()方法返回数组中满足提供的测试函数的第一个元素的索引。否则返回-1。

},

//修改
amendCar(item) {
// this.carList.findIndex((item) => {
// console.log(item.name);
// return item.name.replace(item.name, this.reviseName)
// return item.name == this.reviseName;
// });
var Gai = document.getElementById("Gai");
console.log(Gai);
Gai.onclick = () => {
item.name = this.reviseName
}
// ietm.name == this.reviseName
},
//搜索
getCarList() {
// 处理 看一下包含关键字的汽车
return this.carList.filter((item) => {
return item.name.includes(this.keyWord)
})
},
}
})
</script>
</body>

</html>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值