效果展示:(说明:使用webpack重构购物车程序,使用vue-cli生成项目脚手架)

文件结构:

代码:

(1)将原来编写的btn-grp组件单独编写到BtnGrp.vue文件中

  • 可以看到现在代码清晰了很多,template标签部分编写模板,script标签部分编写组件的交互代码,编写方式和原先写在HTML的代码一致

  • 现在整个前端应用都是基于组件化的,代码变得更加清晰了

<!--vue组件-->
<template>
<div style="float:right;">
<input class="form-control"
v-model="searchProduct"
@keyup.enter="filterProduct"
type="text"
placeholder="请输入商品名称"/>
</div>
</template> <script type="application/javascript">
// import App from '../App';
export default{
name:"BtnGroup",
props:['products'],
data:function(){
return {
searchProduct:''
}
},
methods:{
/* 商品筛选 */
filterProduct:function(){
// console.log(this.$parent.products)
var searchArr = [];
for(let i=;i<this.$parent.products.length;i++){
searchArr.push(this.$parent.products[i].name)
}
var searchIndex = searchArr.indexOf(this.searchProduct);
if(searchIndex !== -){
this.$parent.products = this.$parent.products.filter(function(element, index, self){
/* element数组元素、index元素索引、self数组本身 */
return index == searchIndex;
})
}else{
alert('暂无符合商品');
}
}
} }
// module.exports = BtnGroup;
// export default BtnGroup;
// export default {
// props: ['buttons']
// }
</script>
<!-- Add "scoped" attribute to limit CSS to this component only -->
<!--翻译:添加“scoped”属性以仅将CSS限制为此组件-->
<style type="text/css" scoped> </style>

(2)将原来写在HTML中的代码重构到App.vue中

  • 此处因为需要用到BtnGrp组件,所以需要先import 组件,然后在components中引用该组件

<!--主页组件-->

<template>
<div id="app" class="container">
<h1>购物车</h1>
<hr>
<!--<BtnGroup v-bind:buttons="buttons"></BtnGroup>-->
<div style="overflow: hidden">
<div style="float: left">
<button type="button" v-on:click="addHandler" class="btn btn-primary">添加</button>
<button type="button" v-on:click="changeHandler" class="btn btn-default">修改</button>
<button type="button" v-on:click="delateHandler" class="btn btn-default">删除</button>
</div>
<BtnGroup :products="products"></BtnGroup>
</div>
<br>
<br>
<table class="table table-bordered table-striped table-hover">
<tr>
<td><input type="checkbox" v-on:click="selectAll" name="selectAllBtn" v-model="selectAllState"/></td>
<th>ID</th>
<th>商品名称</th>
<th>商品价格</th>
<th>商品数量</th>
<th>商品总价</th>
</tr>
<tr v-for="(product,index) in products">
<td>
<input type="checkbox" v-on:click="checkItem(product)" v-model="product.state" name="checkbox" />
</td>
<td>{{index+}}</td>
<td>{{product.name}}</td>
<td>{{product.price}}</td>
<td>
<!--可以将两个按钮的方法合成一个,通过传参-->
<!--<button @click="changeCount(prod, -1)">-</button>-->
<button @click="cutCount(product)" class="btn btn-default btn-sm">-</button>
<input type="number" v-model="product.count"/>
<!--<button @click="changeCount(prod, 1)">-</button>-->
<button @click="addCount(product)" class="btn btn-default btn-sm">+</button>
</td>
<td>{{product.price * product.count}}</td>
</tr>
<tr>
<!--text-right排版文本右对齐-->
<td colspan="" class="text-right">总价:</td>
<!--text-primary辅助类文本-->
<td class="text-primary">{{totalMoney}}</td>
</tr>
</table>
</div>
</template> <script>
/* eslint-disable no-new */
import 'bootstrap/dist/css/bootstrap.min.css'
import BtnGroup from './components/BtnGroup'
export default{
name: 'App',
components: {BtnGroup},
data:function(){
return{
products: [
{
name: '小米6S',
price: ,
count: ,
state:false
},
{
name: '锤子2',
price: ,
count: ,
state:false
},
{
name: '华为P20',
price: ,
count: ,
state:false
},
{
name: 'OPPO R15',
price: ,
count: ,
state:false
},
{
name: 'OPPO R11',
price: ,
count: ,
state:false
},
],
selectAllState:false
/*
注意:这里修正下,页面里的死数据,操作类的直接写好即可,不用遍历数据获取。
只有页面中的展示信息即动态设置
*/
/*
buttons:[
{title:'添加',class:'btn-primary',handler:function(){alert('点击添加按钮')}},
{title:'修改',class:'btn-default',handler:function(){alert('点击修改按钮')}},
{title:'删除',class:'btn-default',handler:function(){
for(let i =0;i<app.data().products.length;i++){
// console.log(app.data().products[i].state)
if(app.data().products[i].state){
app.data().products.splice(i,1);
i--;
}
};
全选按钮状态清空
document.querySelectorAll('input[name="selectAllBtn"]')[0].checked = false;
}}
]
*/
}
},
methods:{
// 用户点击加减数量时调用
cutCount:function(product){
if(product.count>){
product.count--
}else{
alert('商品数量不能小于0')
}
},
addCount:function(product){
product.count++
},
checkItem:function(product){
product.state = !product.state;
},
selectAll:function() {
var checkObj = document.querySelectorAll('input[name="checkbox"]'); // 获取所有checkbox项
if (event.target.checked) {
// 点击全选事件
for (var i = ; i < checkObj.length; i++) {
checkObj[i].checked = true;//选中样式
this.products[i].state = true;//动态改变值,供删除用
}
} else {
for (var i = ; i < checkObj.length; i++) {
checkObj[i].checked = false;
this.products[i].state = false;
}
}
},
/*添加 */
addHandler:function(){
alert("点击添加按钮")
},
/* 修改 */
changeHandler:function(){
alert("点击修改按钮")
},
/* 删除 */
delateHandler:function(){
for(let i=;i<this.products.length;i++){
if(this.products[i].state){
this.products.splice(i,);
i--;
}
}
/* 注意:vue里尽量避免DOM操作。所有的操作都是数据驱动 */
this.selectAllState = false;//还原全选按钮状态
} },
computed:{
totalMoney:function(){
var price = ;
for(var i = ; i < this.products.length; ++i) {
price += parseFloat(this.products[i].price * this.products[i].count);
}
return price;
}
} }
// module.exports = App;
// export default app; </script> <style>
#app {
font-family: 'Avenir', Helvetica, Arial, sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
text-align: center;
color: #2c3e50;
margin-top: 60px;
}
</style>

Vue+Bootstrap实现购物车程序(3)的更多相关文章

  1. Vue+Bootstrap实现购物车程序(2)

    先简单看下效果图:(在原先基础上添加了删除和筛选操作) 代码: <!DOCTYPE html> <html> <head lang="en"> ...

  2. Vue+Bootstrap实现购物车程序(1)

    先看下案例效果:(简单的数量控制及价格运算) 代码: <!DOCTYPE html> <html> <head lang="en"> <m ...

  3. vue.js实现购物车功能

    购物车是电商必备的功能,可以让用户一次性购买多个商品,常见的购物车实现方式有如下几种: 1. 用户更新购物车里的商品后,页面自动刷新. 2. 使用局部刷新功能,服务器端返回整个购物车的页面html 3 ...

  4. 基于 Vue BootStrap的迷你Chrome插件

    代码地址如下:http://www.demodashi.com/demo/14306.html 安装 安装 Visual Studio Code 和Chrome, 自行FQ 详细安装这里略过 安装包管 ...

  5. 用vue.js实现购物车功能

    购物车是电商必备的功能,可以让用户一次性购买多个商品,常见的购物车实现方式有如下几种: 1. 用户更新购物车里的商品后,页面自动刷新. 2. 使用局部刷新功能,服务器端返回整个购物车的页面html 3 ...

  6. 利用JSP编程技术实现一个简单的购物车程序

    实验二   JSP编程 一.实验目的1. 掌握JSP指令的使用方法:2. 掌握JSP动作的使用方法:3. 掌握JSP内置对象的使用方法:4. 掌握JavaBean的编程技术及使用方法:5. 掌握JSP ...

  7. 简单购物车程序(Python)

    #简单购物车程序:money_all=0tag=Trueshop_car=[]shop_info={'apple':10,'tesla':100000,'mac':3000,'lenovo':3000 ...

  8. vue项目向小程序迁移调研

    概述 今天调研了一下vue项目怎么向小程序迁移,有些心得,记录下来,供以后开发时参考,相信对其他人也有用. 基本上vue项目向小程序迁移不外乎2种方法,一种是用小程序的web-view组件,另一种是用 ...

  9. python学习:购物车程序

    购物车程序 product_list = [ ('mac',9000), ('kindle',800), ('tesla',900000), ('python book',105), ('bike', ...

随机推荐

  1. BZOJ1266:上学路线route (最短路+最小割)

    可可和卡卡家住合肥市的东郊,每天上学他们都要转车多次才能到达市区西端的学校.直到有一天他们两人参加了学校的信息学奥林匹克竞赛小组才发现每天上学的乘车路线不一定是最优的. 可可:“很可能我们在上学的路途 ...

  2. UI:通讯录实现

    通讯录实现草图: 代码: #pragma mark (.h文件)-------------------------------------------------------------------- ...

  3. maven项目没有错,但是在项目头上有红叉的解决方法

    转自:https://blog.csdn.net/myblog_dhy/article/details/41695107 我在使用maven的过程中,要导入某个maven项目,导入的过程中会弹出一个框 ...

  4. linux 软件开发的源

    deb http://mirrors.aliyun.com/ubuntu/ quantal main restricted universe multiversedeb http://mirrors. ...

  5. windows下 zookeeper dubbo 安装+配置+demo 详细图文教程

    Java集群优化——dubbo+zookeeper构建 互联网的发展,网站应用的规模不断扩大,常规的垂直应用架构已无法应对,分布式服务架构以及流动计算架构势在必行,Dubbo是一个分布式服务框架,在这 ...

  6. postgresql数据库基本信息查看

    切换至postgresql数据库用户pguser 或 postgres(根据自己实际情况) 1.   SELECT version(); 2.对的 2. 查看数据库大小: SELECT pg_size ...

  7. bzoj 1497 [NOI2006]最大获利【最大权闭合子图+最小割】

    不要被5s时限和50000点数吓倒!大胆网络流!我一个5w级别的dinic只跑了1s+! 看起来没有最大权闭合子图的特征--限制,实际上还是有的. 我们需要把中转站看成负权点,把p看成点权,把客户看成 ...

  8. bzoj 4817: [Sdoi2017]树点涂色【树链剖分+LCT】

    非常妙的一道题. 首先对于操作一"把点x到根节点的路径上所有的点染上一种没有用过的新颜色",长得是不是有点像LCT中的access操作?进而发现,如果把同一颜色的点连起来作为LCT ...

  9. MySQL调优之数据类型选择原则

    本文涉及:高可用数据库设计时数据类型的选择原则 在进行数据库设计时,如果能够选择最恰当的数据类型就可以为后期的数据库调优打好最坚实的基础 选择数据类型的原则 更小的通常更好 例如存储订单状态字段很多时 ...

  10. 《windows核心编程系列》十九谈谈使用远程线程来注入DLL。

    windows内的各个进程有各自的地址空间.它们相互独立互不干扰保证了系统的安全性.但是windows也为调试器或是其他工具设计了一些函数,这些函数可以让一个进程对另一个进程进行操作.虽然他们是为调试 ...