传统的用html+jquery来实现购物车系统要非常的复杂,但是购物车系统完全是一个数据驱动的系统,因此采用诸如Vue.js、angular.js这些框架要简单的多。饿了吗开源的组件库Element是基于Vue.js 2.0实现的,该组件库封装了开发中需要的各种组件,并且提供了友好的API文档供开发者查看,下面就是我用Element实现的一个简单的购物车系统。(https://github.com/iwideal/MyGit/tree/master/HTML/shopping-cart)

首先,我们用Vue.js推荐的命令行工具来搭建项目骨架。

# 创建一个基于 webpack 模板的新项目
$ vue init webpack shopping-cart
# 安装依赖,走你
$ cd shopping-cart
$ npm install
$ npm run dev
 
这时候,生成的项目骨架如图:

这时候,我们要像maven一样,给项目添加依赖,在package.json文件中添加Element依赖:

  "dependencies": {
"element-ui": "^1.1.6",
"vue": "^2.1.0"
},

添加完依赖之后,这是我们可以在项目中使用依赖了,在main.js文件中,导入ElementUI:

// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-default/index.css'
import App from './App.vue' Vue.use(ElementUI) /* eslint-disable no-new */
new Vue({
el: '#app',
template: '<App/>',
components: { App }
})

重新安装依赖,走你:

$ cd shopping-cart
$ npm install
$ npm run dev
 
这时,我们在App.vue中,写入我们的购物车代码。
预览一下,整体效果如下:

 一、创建购物车骨架,即复杂型的表格。我们这时可以从Element的官网(http://element.eleme.io/#/zh-CN/component/table)copy过来模板,我们选择带有checkbox的表格:
 
<template>
<div>
<el-table
:data="tableData"
border
style="width: 100%"
@selection-change="selected">
<el-table-column
type="selection"
width="50">
</el-table-column>
<el-table-column
label="商品名称"
width="680">
<template scope="scope">
<div style="margin-left: 50px">
<img :src="scope.row.goods.img" style="height: 50px;width: 50px">
<span style="font-size: 18px;padding-left: 200px;">{{scope.row.goods.descript}}</span>
</div>
</template>
</el-table-column>
<el-table-column
label="单价"
width="150"
prop="price">
</el-table-column>
<el-table-column
label="数量"
width="200">
<template scope="scope">
<div>
<el-input
v-model="scope.row.number" @change="handleInput(scope.row)">
<el-button slot="prepend" @click="del(scope.row)"><i class="el-icon-minus"></i></el-button>
<el-button slot="append" @click="add(scope.row)"><i class="el-icon-plus"></i></el-button>
</el-input>
</div>
</template>
</el-table-column>
<el-table-column
label="小计"
width="150"
prop="goodTotal">
</el-table-column>
<el-table-column label="操作">
<template scope="scope">
<el-button type="danger" @click="handleDelete(scope.$index, scope.row)">
删除<i class="el-icon-delete2 el-icon--right"></i>
</el-button>
</template>
</el-table-column>
</el-table>
<br>
<el-button type="info" style="float: right">{{"商品总额:" + moneyTotal}}</el-button>
</div>
</template>

购物车骨架搭建好之后,我们就可以向里面插入数据了:

data() {
return {
tableData: [{
goods:{
img:'http://i1.mifile.cn/a1/pms_1474859997.10825620!80x80.jpg',
descript:'小米手环2',
},
price:149,
number:1,
goodTotal:149,
},{
goods:{
img:'http://i1.mifile.cn/a1/pms_1482321199.12589253!80x80.jpg',
descript:'小米活塞耳机 清新版 黑色',
},
price:29,
number:1,
goodTotal:29,
},{
goods:{
img:'http://i1.mifile.cn/a1/pms_1468288696.74437986!80x80.jpg',
descript:'米家LED随身灯 增强版 蓝色',
},
price:14.9,
number:1,
goodTotal:14.9,
},{
goods:{
img:'http://i1.mifile.cn/a1/pms_1476688193.46995320.jpg?width=140&height=140',
descript:'10000mAh小米移动电源2 银色',
},
price:79,
number:1,
goodTotal:79,
}],
moneyTotal:0,
multipleSelection:[],
}
}

这时候,我们需要对购物车中的各种事件做处理,包括删除商品、增加(减少)商品数量、选中商品等:

methods: {
handleDelete(index, row) {
this.$confirm('确定删除该商品?', '提示', {
confirmButtonText: '确定',
cancelButtonText: '取消',
type: 'warning'
}).then(() => {
//删除数组中指定的元素
this.tableData.splice(index,1);
this.$message({
type: 'success',
message: '删除成功!'
});
}).catch(() => {
this.$message({
type: 'info',
message: '已取消删除'
});
});
},
handleInput:function(value){
if(null==value.number || value.number==""){
value.number=1;
}
value.goodTotal=(value.number*value.price).toFixed(2);//保留两位小数
//增加商品数量也需要重新计算商品总价
this.selected(this.multipleSelection);
},
add:function(addGood){
//输入框输入值变化时会变为字符串格式返回到js
//此处要用v-model,实现双向数据绑定
if(typeof addGood.number=='string'){
addGood.number=parseInt(addGood.number);
};
addGood.number+=1;
},
del:function(delGood){
if(typeof delGood.number=='string'){
delGood.number=parseInt(delGood.number);
};
if(delGood.number>1){
delGood.number-=1;
}
},
//返回的参数为选中行对应的对象
selected:function(selection){
this.multipleSelection=selection;
this.moneyTotal=0;
//此处不支持forEach循环,只能用原始方法了
for(var i=0;i<selection.length;i++){
//判断返回的值是否是字符串
if(typeof selection[i].goodTotal=='string'){
selection[i].goodTotal=parseInt(selection[i].goodTotal);
};
this.moneyTotal+=selection[i].goodTotal;
}
},

饿了吗开源组件库Element模拟购物车系统的更多相关文章

  1. 推荐3个小程序开源组件库——Vant、iView、ColorUI

    推荐3个小程序开源组件库 在进行小程序开发时,经常会遇到编写组件方面的阻碍,这让我们花费大量的时间在页面以及 CSS 样式编写上.因此可以使用开源组件库,有些复杂的组件可以直接拿来使用,节省开发时间, ...

  2. 基于vue2.0前端组件库element中 el-form表单 自定义验证填坑

    eleme写的基于vue2.0的前端组件库: http://element.eleme.io 我在平时使用过程中,遇到的问题. 自定义表单验证出坑: 1: validate/resetFields 未 ...

  3. GearCase UI - 自己构建一套基于 Vue 的简易开源组件库

    最近 1 ~ 2 月除了开发小程序之外,还一直在继续深入的学习 Vuejs.利用零碎.闲暇的时间整合了一套基于 Vue 的 UI 组件库.命名为 GearCase UI,意为齿轮盒.现在把该项目进行开 ...

  4. AndroidUI开源组件库BottomView 第三方自定义UI控件

    这里分享一个Android的非常经典实用而且简单方便的第三方UI控件库:BottomView(小米的米UI也用到了这个) 原文  http://blog.csdn.net/opzoonzhuzheng ...

  5. 官方教程:Apache Kylin和Superset集成,使用开源组件,完美打造OLAP系统

    本文转自Apache Kylin公众号apachekylin. Superset 是一个数据探索和可视化平台,设计用来提供直观的,可视化的,交互式的分析体验. Superset 提供了两种分析数据源的 ...

  6. 如何快速为团队打造自己的组件库(上)—— Element 源码架构

    文章已收录到 github,欢迎 Watch 和 Star. 简介 详细讲解了 ElementUI 的源码架构,为下一步基于 ElementUI 打造团队自己的组件库打好坚实的基础. 如何快速为团队打 ...

  7. 16款优秀的Vue UI组件库推荐

    16款优秀的Vue UI组件库推荐 Vue 是一个轻巧.高性能.可组件化的MVVM库,API简洁明了,上手快.从Vue推出以来,得到众多Web开发者的认可.在公司的Web前端项目开发中,多个项目采用基 ...

  8. [转载]前端——实用UI组件库

    https://www.cnblogs.com/xuepei/p/7920888.html Angular UI 组件 ngx-bootstrap 是一套Bootstrap 组件 官网:https:/ ...

  9. 强烈推荐优秀的Vue UI组件库

    Vue 是一个轻巧.高性能.可组件化的MVVM库,API简洁明了,上手快.从Vue推出以来,得到众多Web开发者的认可.在公司的Web前端项目开发中,多个项目采用基于Vue的UI组件框架开发,并投入正 ...

随机推荐

  1. 32.密码学知识-SSL/TLS-9——2019年12月19日

    9. SSL/TLS "SSL/TLS --- 为了更安全的通信" 本章中我们将学习SSL/TLS的相关知识. SSL/TLS是世界上应用最广泛的密码通信方法.比如说,当在网上商城 ...

  2. 关于下拉框的onchange事件和onclick选择value值。

    下拉框的onchange事件和onclick,一般最好都选择onchange事件,onclick可能会不兼容有些浏览器. 下面是代码: <!DOCTYPE html><html la ...

  3. ansible 基础操作

    ansible是什么? 可以批量在远程主机上执行命令 准备条件: 1.创建一台环境干净的虚拟机. 2.克隆出三台虚拟机. 3.安装wget: wget -O /etc/yum.repos.d/Cent ...

  4. 【leetcode】1170. Compare Strings by Frequency of the Smallest Character

    题目如下: Let's define a function f(s) over a non-empty string s, which calculates the frequency of the ...

  5. 【leetcode】1147. Longest Chunked Palindrome Decomposition

    题目如下: Return the largest possible k such that there exists a_1, a_2, ..., a_k such that: Each a_i is ...

  6. LeetCode--102--二叉树的层次遍历(python)

    给定一个二叉树,返回其按层次遍历的节点值. (即逐层地,从左到右访问所有节点). 例如:给定二叉树: [3,9,20,null,null,15,7], 3    / \  9 20  / \ 15 7 ...

  7. A1036

    输入n行不同学生的name性别id和成绩,输出成绩最高的女生名字和id,成绩最低的男生名字和id求出二者的差 如果有性别缺少,输出Absent并在结果行输出NA 注意变量不要搞混,可以用结构体……不过 ...

  8. 51nod1820 长城之旅

    题目描述 BB 痛失一血(打了场Comet OJ回来就没了) 不过后来又刷了一道水题 题解 LCM+取模=结论题 结论1 \(gcd(k^{2^i}+1,k^{2^j}+1)=1 (i\neq j 且 ...

  9. JS循环结构

    什么是循环结构? 反复一遍又一遍做着相同(相似)的事情 循环结构的两大要素? 循环条件:什么时候开始,什么时候结束 循环操作:循环体,循环过程中 做了什么 一.while语句 while语句 属于前测 ...

  10. codeforces 682C

    鸽了两天,还是我太蒟了,mayan游戏调不出来,难题又不会,只有刷水DFS才能勉强维持一下生计这样子,我还是要提高姿势水平啊! 题目描述: 给定一棵树,每条边有边权,每个点有点权,如果某个点到其子节点 ...