vuejs学习小案例
2023-09-14 09:04:07 时间
1、购物车:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>vue</title>
<style>
[v-cloak] {
display: none;
}
table {
border: 1px solid #e9e9e9;
border-collapse: collapse;
border-spacing: 0;
empty-cells: show;
}
th, td {
padding: 8px 16px;
border: 1px solid #e9e9e9;
text-align: center;
}
th {
background: #f7f7f7;
color: #5c6b77;
font-weight: bold;
white-space: nowrap;
}
</style>
</head>
<body>
<div id="app" v-cloak>
<template v-if="list.length">
<table>
<thead>
<tr>
<th></th>
<th>商品名称</th>
<th>商品单价</th>
<th>购买数量</th>
<th>操作</th>
</tr>
</thead>
<tbody>
<tr v-for="(item,index) in list">
<td>{{index+1}}</td>
<td>{{item.name}}</td>
<td>{{item.price}}</td>
<td>
<button
@click="handleReduce(index)"
:disabled="item.count === 1">-
</button>
{{item.count}}
<button @click="handleAdd(index)">+</button>
</td>
<td>
<button @click="handleRemove(index)">移除</button>
</td>
</tr>
</tbody>
</table>
<div>总价:${{ totalPrice }}</div>
</template>
<div v-else>购物车为空</div>
</div>
<script src="../../js/tool/vue.js"></script>
<script>
var app = new Vue({
el: "#app",
data: {
list: [
{id: 1, name: 'iphone', price: 6188, count: 1},
{id: 2, name: 'iphone7', price: 6188, count: 1},
{id: 3, name: 'iphone8', price: 6188, count: 1},
{id: 4, name: 'iphone9', price: 6188, count: 1}
]
},
computed: {
totalPrice: function () {
var total = 0;
for (var i = 0; i < this.list.length; i++) {
var item = this.list[i];
total += item.price * item.count;
}
return total.toString().replace(/\B(?=(\d{3})+$)/g, ',');
}
},
methods: {
handleReduce: function (index) {
if (this.list[index].count === 1) return;
this.list[index].count--;
},
handleAdd: function (index) {
this.list[index].count++;
},
handleRemove: function (index) {
this.list.splice(index, 1);
}
}
})
</script>
</body>
</html>
效果:
***************************
2、Bootstrap添加,搜索关键字
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>demo</title>
<link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://cdn.staticfile.org/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script>
</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>
<!-- 在Vue中,使用事件绑定机制,为元素指定处理函数的时候,如果加了小括号,就可以给函数传参了 -->
<input type="button" value="添加" class="btn btn-primary" @click="add()">
<label>
搜索名称关键字:
<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>Ctime</th>
<th>Operation</th>
</tr>
</thead>
<tbody>
<!-- 通过search方法渲染数据,而是直接通过从data上面获取数据直接进行渲染 -->
<tr v-for="item in search(keywords)" :key="item.id">
<td>{{ item.id }}</td>
<td v-text="item.name"></td>
<td>{{ item.ctime }}</td>
<td>
<a href="" @click.prevent="del(item.id)">删除</a>
</td>
</tr>
</tbody>
</table>
</div>
<script src="../../js/tool/vue.js"></script>
<script>
var vm = new Vue({
el: '#app',
data: {
id: '',
name: '',
keywords: '', // 搜索的关键字
list: [
{id: 1, name: '奔驰', ctime: new Date()},
{id: 2, name: '宝马', ctime: new Date()}
]
},
methods: {
add() { // 添加的方法
var car = {id: this.id, name: this.name, ctime: new Date()}
this.list.push(car);
this.id = this.name = '';
},
del(id) { // 根据Id删除数据
var index = this.list.findIndex(item => {
if (item.id == id) {
return true;
}
})
this.list.splice(index, 1);
},
search(keywords) { // 根据关键字,进行数据的搜索
return this.list.filter(item => {
if (item.name.includes(keywords)) {
return item;
}
});
}
}
});
</script>
</body>
</html>
效果:
*************************
3、
相关文章
- 学习索引结构的一些案例——Jeff Dean在SystemML会议上发布的论文(中)
- 使用阿里云身份证扫描识别接口案例——CSDN博客
- 【数据运营】在运营中,为什么文本分析远比数值型分析重要?一个实际案例,五点分析(下)
- Flink(45):Flink之TableAPI和FlinkSQL的案例二
- 机器学习笔记 - 机器学习/深度学习实战案例合集
- Java数组--获取数组中的最大值案例
- ckeditor 触发事件(案例)
- 打通C/4HANA和S/4HANA的一个原型开发:智能服务创新案例
- 【Android笔记93】Android小案例(三)之模仿小米商城(首页商品展示界面)
- XAI之GS:全局代理(Global Surrogate,对黑盒机器学习执行模型可解释性的技术)的简介、常用工具包、案例应用之详细攻略
- Python语言学习:Python语言学习之硬件交互应用(arduino、树莓派等)相关的简介、案例应用之详细攻略
- Python语言学习:Python语言学习之网络爬虫/反爬虫技术相关的简介、案例应用之详细攻略
- ML之ME/LF:机器学习中回归预测模型评估指标(MSE/RMSE/MAE)简介、使用方法、代码实现、案例应用之详细攻略
- ML之ME/LF:机器学习之风控业务中常用模型监控指标CSI(特征稳定性指标)的简介、使用方法、案例应用之详细攻略
- ML之lightgbm:LightGBM参数手册、调参技巧/调参顺序/网格搜索实现、lightgbm.train/LGBMClassifier/LGBMRegressor函数简介及其案例应用之详细攻略
- mysql索引_效率测试(包含测试sql脚本300万条数据),可用作教学案例。
- 机器学习案例(六):Python 大数据进行信用卡欺诈检测(完整源码和分析)
- 实战案例丨使用云连接CC和数据复制服务DRS实现跨区域RDS迁移和数据同步
- 学习索引结构的一些案例——Jeff Dean在SystemML会议上发布的论文(上)
- css案例13——透明度(整体透明、背景透明)
- VB.net:VB.net编程语言学习之基于VS软件利用VB.net语言实现对CAD/VRML进行二次开发的简介、案例应用之详细攻略
- Scratch3.0——助力新进程序员理解程序(案例一十三、黑红相见)
- 【深度学习入门案例】LAC词法分析
- Shell脚本经典案例:文件的备份
- 【应用案例】CANape支持基于模型的ECU开发