案例-品牌管理(铺)
目标: 数据铺设
- 需求1: 把默认数据显示到表格上
- 需求2: 注意资产超过100的, 都用红色字体标记出来
细节:
① 先铺设静态页面 — 去.md文档里, 复制数据和标签模板
② 此案例使用bootstrap, 需要下载, 并导入到工程main.js中
③ 用v-for配合默认数据, 把数据默认铺设到表格上显示
④ 直接在标签上, 大于100价格, 动态设置red类名
图示:
- 因为案例使用了bootstrap, 工程化开发, 模块化用npm/yarn下载引入使用
yarn add bootstrap
- 在main.js - 引入bootstrap
import "bootstrap/dist/css/bootstrap.css" // 默认找文件夹下的index文件(但是这个不是所以需要写路径)
- 模板代码(在这个基础上写)
<template>
<div id="app">
<div class="container">
<!-- 顶部框模块 -->
<div class="form-group">
<div class="input-group">
<h4>品牌管理</h4>
</div>
</div>
<!-- 数据表格 -->
<table class="table table-bordered table-hover mt-2">
<thead>
<tr>
<th>编号</th>
<th>资产名称</th>
<th>价格</th>
<th>创建时间</th>
<th>操作</th>
</tr>
</thead>
<tbody>
<tr >
<td></td>
<td></td>
<!-- 如果价格超过100,就有red这个类 -->
<td class="red"></td>
<td></td>
<td><a href="#" >删除</a></td>
</tr>
</tbody>
<!--
<tfoot >
<tr>
<td colspan="5" style="text-align: center">暂无数据</td>
</tr>
</tfoot>
-->
</table>
<!-- 添加资产 -->
<form class="form-inline">
<div class="form-group">
<div class="input-group">
<input
type="text"
class="form-control"
placeholder="资产名称"
/>
</div>
</div>
<div class="form-group">
<div class="input-group">
<input
type="text"
class="form-control"
placeholder="价格"
/>
</div>
</div>
<!-- 阻止表单提交 -->
<button class="btn btn-primary">添加资产</button>
</form>
</div>
</div>
</template>
<script>
export default {
data() {
return {
name: "", // 名称
price: 0, // 价格
list: [
{ id: 100, name: "外套", price: 199, time: new Date('2010-08-12')},
{ id: 101, name: "裤子", price: 34, time: new Date('2013-09-01') },
{ id: 102, name: "鞋", price: 25.4, time: new Date('2018-11-22') },
{ id: 103, name: "头发", price: 19900, time: new Date('2020-12-12') }
],
};
},
};
</script>
<style >
.red{
color: red;
}
</style>
正确代码, 不可复制
<tbody>
<tr v-for="obj in list" :key="obj.id">
<td>{{ obj.id }}</td>
<td>{{ obj.name }}</td>
<!-- 如果价格超过100,就有red这个类 -->
<td :class="{red: obj.price > 100}">{{ obj.price }}</td>
<td>{{ obj.time }}</td>
<td><a href="#" >删除</a></td>
</tr>
</tbody>
<script>
// 1. 明确需求
// 2. 标签+样式+默认数据
// 3. 下载bootstrap, main.js引入bootstrap.css
// 4. 把list数组 - 铺设表格
// 5. 修改价格颜色
</script>
1.9_案例-品牌管理(增)
目标: 数据新增
-
需求1: 实现表单数据新增进表格功能
-
需求2: 判断用户输入是否为空给提示
-
分析
① 添加资产按钮 – 绑定点击事件
② 给表单v-model绑定vue变量收集用户输入内容
③ 添加数组到数组中
④ 判断用户内容是否符合规定
图示:
在上个案例代码基础上接着写
正确代码,不可复制
<!-- 添加资产 -->
<form class="form-inline">
<div class="form-group">
<div class="input-group">
<input
type="text"
class="form-control"
placeholder="资产名称"
v-model="name"
/>
</div>
</div>
<div class="form-group">
<div class="input-group">
<input
type="text"
class="form-control"
placeholder="价格"
v-model.number="price"
/>
</div>
</div>
<!-- 4. 阻止表单提交(刷新网页数据又回去了) -->
<button class="btn btn-primary" @click.prevent="addFn">添加资产</button>
</form>
<script>
// 目标: 新增
// 1. 按钮 - 事件
// 2. 给表单v-model绑定vue变量
export default {
// ...省略其他
methods: {
addFn(){
// 5. 判断是否为空
if (this.name.trim().length === 0 || this.price === 0) {
alert("不能为空")
return
}
// 3. 把值以对象形式-插入list
this.list.push({
// 当前数组最后一个对象的id+1作为新对象id值
id: this.list[this.list.length - 1].id + 1,
name: this.name,
price: this.price,
time: new Date()
})
}
}
};
</script>
1.10_案例-品牌管理(删)
目标: 数据删除
-
需求1: 点击删除的a标签, 删除数据
-
需求2: 删除没数据了要提示暂无数据的tfoot
-
分析
① a标签绑定点击事件
② 给事件方法传id
③ 通过id, 找到对应数据删除
④ 删除光了要让tfoot显示
⑤ 删除光了再新增, 有bug(id值问题)需要修复
图示:
在上个案例代码基础上接着写
正确的代码(不可复制)
<td><a href="#" @click="delFn(obj.id)">删除</a></td>
<script>
// 目标: 删除功能
// 1. 删除a标签-点击事件
// 2. 对应方法名
// 3. 数据id到事件方法中
// 4. 通过id, 找到这条数据在数组中的下标
// 5. splice方法删除原数组里的对应元素
// 6. 设置tfoot, 无数据给出提示
// 7. 无数据再新增, id要判断一下
export default {
// ...其他代码
methods: {
// ...其他代码
delFn(id){
// 通过id找到这条数据在数组中下标
let index = this.list.findIndex(obj => obj.id === id)
this.list.splice(index, 1)
}
}
};
</script>
bug:全删掉再新增会报错
修复:
// 添加按钮
addbtn(){
// console.log(1111,this.name,this.price);
if(this.name === ''){
return alert('请输入合法名称')
}
// 💥修复 id 的bug
const id = this.list.length ? this.list[this.list.length - 1].id + 1 : 200
// push 影响原来的数组,页面的会自动更新
this.list.push( {
id: id ,
name: this.name,
price:this.price,
time: new Date()
})
// 添加成功后清空两个输入框
this.name = ""
this.price = 0
},
我的:
<template>
<div id="app">
<div class="container">
<!-- 顶部框模块 -->
<div class="form-group">
<div class="input-group">
<h4>品牌管理</h4>
</div>
</div>
<!-- 数据表格 -->
<table class="table table-bordered table-hover mt-2">
<thead>
<tr >
<th>编号</th>
<th>资产名称</th>
<th>价格</th>
<th>创建时间</th>
<th>操作</th>
</tr>
</thead>
<tbody>
<tr v-for="item in list " :key="item.id">
<td>{{item.id}}</td>
<td>{{item.name}}</td>
<!-- 如果价格超过100,就有red这个类 -->
<td :class="{red:item.price>100}">{{item.price}}</td>
<td>{{item.time}}</td>
<td><a href="#" @click.prevent="removeBtn(item.id)">删除</a></td>
</tr>
</tbody>
<!-- 如果数组长度为0,就创建 tfoot 结构到页面中 -->
<tfoot v-if="list.length === 0">
<tr>
<td colspan="5" style="text-align: center">暂无数据</td>
</tr>
</tfoot>
</table>
<!-- 添加资产 -->
<form class="form-inline">
<div class="form-group">
<div class="input-group">
<input
type="text"
class="form-control"
placeholder="资产名称"
v-model.trim="name"
/>
</div>
</div>
<div class="form-group">
<div class="input-group">
<input
type="text"
class="form-control"
placeholder="价格"
v-model.number="price"
/>
</div>
</div>
<!-- 阻止表单提交 -->
<button @click.prevent="addbtn" class="btn btn-primary">添加资产</button>
</form>
</div>
</div>
</template>
<script>
import 'bootstrap/dist/css/bootstrap.css'
import moment from "moment"
export default {
data() {
return {
name: "", // 名称
price: 0, // 价格
list: [
{ id: 100, name: "外套", price: 199, time: new Date('2010-08-12')},
{ id: 101, name: "裤子", price: 34, time: new Date('2013-09-01') },
{ id: 102, name: "鞋", price: 25.4, time: new Date('2018-11-22') },
{ id: 103, name: "头发", price: 19900, time: new Date('2020-12-12') }
],
};
},
methods: {
// 添加按钮
addbtn(){
// console.log(1111,this.name,this.price);
if(this.name === ''){
return alert('请输入合法名称')
}
// 💥修复 id 的bug
const id = this.list.length ? this.list[this.list.length - 1].id + 1 : 200
// push 影响原来的数组,页面的会自动更新
this.list.push( {
id: id ,
name: this.name,
price:this.price,
time: new Date()
})
// 添加成功后清空两个输入框
this.name = ""
this.price = 0
},
// 删除按钮
removeBtn(id){
console.log(id);
// 过滤掉 不要某一项 但是 filter 不改变原数组
this.list = this.list.filter(v => v.id != id)
}
},
filters: {
shijian:(val) => {
return moment(val).format('YYYY-MM-DD')
}
},
};
</script>
<style >
.red{
color: red;
}
</style>