传统的用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. bzoj4025 二分图 LCT + 最小生成树

    题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=4025 题解 貌似这道题有一个非常简单的做法是线段树分治+并查集. 可是我是为了练 LCT 来做 ...

  2. POJ 3741 Raid (平面最近点对)

    $ POJ~3741~Raid $ (平面最近点对) $ solution: $ 有两种点,现在求最近的平面点对.这是一道分治板子,但是当时还是想了很久,明明知道有最近平面点对,但还是觉得有点不对劲. ...

  3. 谷歌浏览器遇到js报错自动进行断点调试,如何关闭

    转载于csdn 附地址 http://blog.csdn.net/microcosmv/article/details/60793882 留备用.

  4. 【GDOI2014模拟】服务器

    前言 直到比赛最后几分钟,才发现60%数据居然是一个水dp,结果没打完. 题目 我们需要将一个文件复制到n个服务器上,这些服务器的编号为S1, S2, -, Sn. 首先,我们可以选择一些服务器,直接 ...

  5. cs231n assignment1 KNN

    title: cs231n assignment1 KNN tags: - KNN - cs231n categories: - 机器学习 date: 2019年9月16日 17:03:13 利用KN ...

  6. AGC030F - Permutation and Minimum

    https://atcoder.jp/contests/agc030/tasks/agc030_f 题解 我们先把这个排列从\(1 \sim 2n\)表达出来,然后题面中的每一对数我们可以用一条线把他 ...

  7. multipages-generator今日发布?!妈妈再也不用担心移动端h5网站搭建了!

    本文适合的读者?‍?‍?‍? 现在在手淘,京东,今日头条,美柚等过亿用户的手机app中的,都常见h5网页,他们有更新快,灵活,便于分享和传播的特性.这里有他们中的几个h5的例子:(手淘,美柚).这些a ...

  8. 在Linux环境中运行python 项目

    1首先创建一个虚拟环境或者在一个已有的虚拟环境中创建一个django项目 1.1 创建一个虚拟环境: mkvirtualenv my_django115 这会在 ~/Envs 中创建 my_djang ...

  9. R 时间戳转化

    转 http://stackoverflow.com/questions/1962278/dealing-with-timestamps-in-r You want the (standard) PO ...

  10. Docker - 部署 Ant Design Pro 的项目

    解读 Ant Design Pro 的 Docker 配置 package.json 的 scripts -f: 使用什么配置文件 -t: 标签 up: 启动服务(的容器) build: 构建或重新构 ...