侯体宗的博客
  • 首页
  • Hyperf版
  • beego仿版
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

layui使用数据表格实现购物车功能

前端  /  管理员 发布于 5年前   642

本文实例为大家分享了layui使用数据表格实现购物车功能的具体代码,供大家参考,具体内容如下

html部分

  layui数据表格实现类购物车功能       
layui数据表格实现类购物车功能
  • 

    使用方法


    1:单击左边商品列表任一商品,右边购物车数据表格会渲染一条数据,同时计算总数,合计金额。
    2:购物车数据表格,数量/单价改变,金额改变,底部合计金额,数量,也会跟随改变
    3:购物车数据表格,‘删除',删除当前所选商品

商品列表

购物车

总数:--- 合计金额:---

css部分

*{ margin: 0; padding: 0;}#guide_panel{ width: 98%; margin: 0 auto;}#row_panel{ width: 98%; height: 720px; margin: 0 auto; /*border: solid 1px red;*/}#row_panel .table-panel{ width: 50%; height: 500px; float: left; text-align: center;}#row_panel .table-panel p{ color:#BBBBBB;}#row_panel .bottom-panel{ width: 100%; height: 40px; background: #F0F0F0; float: left; margin-top: 20px; text-align: right;}#row_panel .bottom-panel span{ line-height: 40px; font-size: 18px;}

js部分

layui.use(['layer', 'form', 'element', 'jquery', 'table', 'laydate'], function () { var form = layui.form, element = layui.element, $ = layui.$, layer = layui.layer, table = layui.table, laydate = layui.laydate; /** * 数据表格:定义表头数据 * @type {{}} */ var lan = {}; lan.cpbm = "产品编码"; lan.cpjm = "产品简码"; lan.cpmc = "产品名称"; lan.ckjj = "参考进价"; lan.lpbz = "礼品标志"; lan.spbh = "商品编号"; lan.spmc = "商品名称"; lan.sl = "数量"; lan.dj = "单价"; lan.je = "金额"; lan.cz = "操作"; /** * 购物车数据 * @type {Array} */ var goodsData = []; /** * 商品数据 * @type {string} */ var GoodsDataStr = "[{\"GoodsID\":\"20190308131925\",\"GoodsName\":\"123456\",\"NameCode\":\"123456\",\"GoodsClass\":\"13659603513393152\",\"GoodsType\":1,\"Price\":20,\"PriceUnit\":0,\"PriceNum\":0,\"XPrice\":0,\"Images\":null,\"IsPoint\":0,\"PointType\":0,\"MinDiscount\":0.8,\"IsDiscount\":1,\"Remark\":null,\"IsDelete\":0,\"IsGift\":0,\"ExchangePoint\":0,\"CreateTime\":20190308131956,\"CompID\":198,\"ShopID\":\"237\",\"MasterID\":\"237\",\"MeasureUnit\":\"个\",\"Specials\":0,\"IsShelf\":0,\"FreightTemplateID\":null,\"Id\":\"14157977930075136\",\"LAY_TABLE_INDEX\":0},{\"GoodsID\":\"20190301183528\",\"GoodsName\":\"测试2\",\"NameCode\":\"CS2\",\"GoodsClass\":\"14148378555485184\",\"GoodsType\":1,\"Price\":60,\"PriceUnit\":0,\"PriceNum\":0,\"XPrice\":0,\"Images\":null,\"IsPoint\":0,\"PointType\":0,\"MinDiscount\":0,\"IsDiscount\":0,\"Remark\":null,\"IsDelete\":0,\"IsGift\":0,\"ExchangePoint\":0,\"CreateTime\":20190301183538,\"CompID\":198,\"ShopID\":\"237\",\"MasterID\":\"237\",\"MeasureUnit\":\"次\",\"Specials\":0,\"IsShelf\":0,\"FreightTemplateID\":null,\"Id\":\"14148379235683328\",\"LAY_TABLE_INDEX\":1},{\"GoodsID\":\"20190301183516\",\"GoodsName\":\"测试1\",\"NameCode\":\"CS1\",\"GoodsClass\":\"14148378482019328\",\"GoodsType\":1,\"Price\":50,\"PriceUnit\":0,\"PriceNum\":0,\"XPrice\":0,\"Images\":null,\"IsPoint\":0,\"PointType\":0,\"MinDiscount\":0,\"IsDiscount\":0,\"Remark\":null,\"IsDelete\":0,\"IsGift\":0,\"ExchangePoint\":0,\"CreateTime\":20190301183522,\"CompID\":198,\"ShopID\":\"237\",\"MasterID\":\"237\",\"MeasureUnit\":\"次\",\"Specials\":0,\"IsShelf\":0,\"FreightTemplateID\":null,\"Id\":\"14148378983274496\",\"LAY_TABLE_INDEX\":2},{\"GoodsID\":\"6914068013626\",\"GoodsName\":\"纸巾\",\"NameCode\":\"zj\",\"GoodsClass\":\"13819788947571712\",\"GoodsType\":1,\"Price\":5,\"PriceUnit\":0,\"PriceNum\":0,\"XPrice\":0,\"Images\":null,\"IsPoint\":1,\"PointType\":0.1,\"MinDiscount\":0,\"IsDiscount\":0,\"Remark\":\"%3Cp%3Enull%3C/p%3E\",\"IsDelete\":0,\"IsGift\":0,\"ExchangePoint\":0,\"CreateTime\":20190217092044,\"CompID\":198,\"ShopID\":\"237\",\"MasterID\":\"237\",\"MeasureUnit\":\"盒\",\"Specials\":0,\"IsShelf\":0,\"FreightTemplateID\":null,\"Id\":\"14130846812542976\",\"LAY_TABLE_INDEX\":3},{\"GoodsID\":\"20190126103924660\",\"GoodsName\":\"测试呀1\",\"NameCode\":\"CSY1\",\"GoodsClass\":\"13659654677299200\",\"GoodsType\":1,\"Price\":20,\"PriceUnit\":0,\"PriceNum\":0,\"XPrice\":0,\"Images\":\"/FileSys/MemPhoto/198/20190126103959902.jpg\",\"IsPoint\":0,\"PointType\":0,\"MinDiscount\":0,\"IsDiscount\":0,\"Remark\":\"\",\"IsDelete\":0,\"IsGift\":0,\"ExchangePoint\":0,\"CreateTime\":20190126103959,\"CompID\":198,\"ShopID\":\"237\",\"MasterID\":\"237\",\"MeasureUnit\":\"次\",\"Specials\":0,\"IsShelf\":0,\"FreightTemplateID\":null,\"Id\":\"14099782024565760\",\"LAY_TABLE_INDEX\":4},{\"GoodsID\":\"20190124202652\",\"GoodsName\":\"是是是\",\"NameCode\":\"SSS\",\"GoodsClass\":\"13659603389480960\",\"GoodsType\":1,\"Price\":11,\"PriceUnit\":0,\"PriceNum\":0,\"XPrice\":1,\"Images\":null,\"IsPoint\":0,\"PointType\":0,\"MinDiscount\":0,\"IsDiscount\":0,\"Remark\":null,\"IsDelete\":0,\"IsGift\":0,\"ExchangePoint\":0,\"CreateTime\":20190124202657,\"CompID\":198,\"ShopID\":\"237\",\"MasterID\":\"237\",\"MeasureUnit\":\"1\",\"Specials\":0,\"IsShelf\":0,\"FreightTemplateID\":null,\"Id\":\"14097527869279232\",\"LAY_TABLE_INDEX\":5},{\"GoodsID\":\"699\",\"GoodsName\":\"自助烤肉\",\"NameCode\":\"ZZKR\",\"GoodsClass\":\"13819785158099968\",\"GoodsType\":1,\"Price\":50,\"PriceUnit\":0,\"PriceNum\":0,\"XPrice\":50,\"Images\":\"/FileSys/MemPhoto/198/20190114153729870.jpg\",\"IsPoint\":1,\"PointType\":800,\"MinDiscount\":0,\"IsDiscount\":0,\"Remark\":null,\"IsDelete\":0,\"IsGift\":0,\"ExchangePoint\":0,\"CreateTime\":20190114153730,\"CompID\":198,\"ShopID\":\"237\",\"MasterID\":\"237\",\"MeasureUnit\":null,\"Specials\":0,\"IsShelf\":0,\"FreightTemplateID\":null,\"Id\":\"14083087561366528\",\"LAY_TABLE_INDEX\":6},{\"GoodsID\":\"20190114153056\",\"GoodsName\":\"积分衣服\",\"NameCode\":\"JFYF\",\"GoodsClass\":\"13819785158099968\",\"GoodsType\":1,\"Price\":0,\"PriceUnit\":0,\"PriceNum\":0,\"XPrice\":0,\"Images\":\"/FileSys/GoodsIMG/198/20190114154132974.png\",\"IsPoint\":0,\"PointType\":0,\"MinDiscount\":0,\"IsDiscount\":0,\"Remark\":\"%3Cp%3Enull%3C/p%3E\",\"IsDelete\":0,\"IsGift\":1,\"ExchangePoint\":500,\"CreateTime\":20190114153221,\"CompID\":198,\"ShopID\":\"237\",\"MasterID\":\"237\",\"MeasureUnit\":\"1\",\"Specials\":0,\"IsShelf\":0,\"FreightTemplateID\":null,\"Id\":\"14083082487470080\",\"LAY_TABLE_INDEX\":7},{\"GoodsID\":\"20190114152247\",\"GoodsName\":\"积分裤子\",\"NameCode\":\"JFKZ\",\"GoodsClass\":\"13819785158099968\",\"GoodsType\":1,\"Price\":0,\"PriceUnit\":0,\"PriceNum\":0,\"XPrice\":0,\"Images\":null,\"IsPoint\":1,\"PointType\":800,\"MinDiscount\":0,\"IsDiscount\":0,\"Remark\":null,\"IsDelete\":0,\"IsGift\":1,\"ExchangePoint\":0,\"CreateTime\":20190114152321,\"CompID\":198,\"ShopID\":\"237\",\"MasterID\":\"237\",\"MeasureUnit\":\"1\",\"Specials\":0,\"IsShelf\":0,\"FreightTemplateID\":null,\"Id\":\"14083073653020672\",\"LAY_TABLE_INDEX\":8},{\"GoodsID\":\"20190114151536\",\"GoodsName\":\"裤子\",\"NameCode\":\"KZ\",\"GoodsClass\":\"13819785158099968\",\"GoodsType\":1,\"Price\":0,\"PriceUnit\":0,\"PriceNum\":0,\"XPrice\":0,\"Images\":null,\"IsPoint\":0,\"PointType\":0,\"MinDiscount\":0,\"IsDiscount\":0,\"Remark\":null,\"IsDelete\":0,\"IsGift\":1,\"ExchangePoint\":500,\"CreateTime\":20190114151658,\"CompID\":198,\"ShopID\":\"237\",\"MasterID\":\"237\",\"MeasureUnit\":\"1\",\"Specials\":0,\"IsShelf\":0,\"FreightTemplateID\":null,\"Id\":\"14083067377571840\",\"LAY_TABLE_INDEX\":9}]"; var GoodsData = JSON.parse(GoodsDataStr); /** * 页面加载时执行 */ $(function () { console.table(GoodsData);  /**  * List1数据表格渲染(商品列表)  */ var ListTable1 = table.render({  data:GoodsData,  elem: '#List1',  cellMinWidth: 95,  //height: 'full-190',  height: '460',  minheight:430,  page: true,  limit: 10,  limits: [10, 20, 30, 40],  done: function (res) {  if (res.rows && res.rows.length == 1) {   var data = res.rows[0];    GoodsList_onDblClickRow(data);  }  },  cols: [  [   { field: 'GoodsID', title: lan.cpbm, align: 'left' },   { field: 'NameCode', title: lan.cpjm, align: 'left' },   { field: 'GoodsName', title: lan.cpmc, align: 'left' },   {   field: 'XPrice', title: lan.ckjj, align: 'right', templet: function (d) {    return "¥" + d.XPrice + "";   }   },   {   field: 'IsGift', title: lan.lpbz, align: 'center', templet: function (d) {    var _fm = '
'; _fm += d.IsGift == 1 ? '' : ''; _fm += ""; return _fm; } } ] ] }); /** * List2数据表格渲染(购物车) */ var ListTable2 = table.render({ data:goodsData, elem: '#List2', cellMinWidth: 95, //height: 'full-190', height: '460', minheight:430, page: true, limit: 10, limits: [10, 20, 30, 40], cols: [ [ { field: 'GoodsID', title: lan.spbh }, { field: 'GoodsName', title: lan.spmc }, { field: 'Qty', title: lan.sl, edit: 'text' }, { field: 'Price', title: lan.dj, edit: 'text' }, { field: 'Money', title: lan.je }, { title: lan.cz, align: "center", fixed: 'right', templet: function (d) { var html = '删除 '; return html; } } ] ], done: function (res) { $("#List2").siblings().find("td").each(function () { if ($(this).attr("data-edit") == 'text') { var num = $(this).find("div").text(); $(this).append('') } }) } }); /** * 监听商品列表'行'单击事件 */ table.on('row(List1)', function (obj) { var data = obj.data; var isNew = true; var vKey = data.GoodsID; //产品编码 //遍历购物车,是否存在产品;存在,数量+1 计算金额 for (var i = 0; i < goodsData.length; i++) { var tRow = goodsData[i]; if (vKey == tRow.GoodsID) { tRow.Qty = parseInt(tRow.Qty) + 1; tRow.Money = accMul(tRow.Qty, tRow.Price,2); isNew = false; break; } } //判断当前数据是否为新增数据 if (isNew) { var tmpRow = {}; tmpRow.Id = data.Id; tmpRow.GoodsID = data.GoodsID; tmpRow.GoodsName = data.GoodsName; tmpRow.Price = data.XPrice; tmpRow.Qty = 1; tmpRow.Money =(data.XPrice).toFixed(2); tmpRow.SalesCode = ""; goodsData.push(tmpRow); } //console.log(goodsData); /** * 开始刷新购物车 */ shoppingCartRefresh(); }); /** * 监听购物车行删除事件 */ table.on('tool(List2)', function (obj, index) { var data = obj.data; if (obj.event === 'delet') { for (var i = 0; i < goodsData.length; i++) { var gd = goodsData[i]; if (data.GoodsID == gd.GoodsID) { goodsData.splice(i, 1); } } /** * 开始刷新购物车 */ shoppingCartRefresh(); } }); /** * 购物车行编辑事件 */ table.on('edit(List2)', function (obj) { var data = goodsData; for (var i = 0; i < data.length; i++) { if (obj.data.Id == data[i].Id) { if (isNaN(data[i].Qty) || data[i].Qty < 0) { data[i].Qty = 0; } if (isNaN(data[i].Price) || data[i].Price < 0) { data[i].Price = 0; } data[i].Qty = Number(data[i].Qty); //数量:声明数字类型 data[i].Price = parseFloat(data[i].Price).toFixed(2); //金额:声明小数类型 data[i].Money = accMul(data[i].Qty, data[i].Price,2); //计算总金额:保留2位小数 } } /** * 开始刷新购物车 */ shoppingCartRefresh(); }); /** * 购物车刷新函数 */ function shoppingCartRefresh(){ var TotalQty = 0; var TotalMoney = 0; //数据表格重载 ListTable2.reload({ data: goodsData }); //计算总数 合计金额 for (var i = 0; i < goodsData.length; i++) { TotalQty = accAdd(TotalQty, goodsData[i].Qty); TotalMoney = (accAdd(TotalMoney, parseFloat(goodsData[i].Money))).toFixed(2); } // console.log("总数量:"+TotalQty); // console.log("总金额:"+TotalMoney); $("#RechargeCount_TotalQty").html(TotalQty); $("#RechargeCount_TotalMoney").html(TotalMoney); } /** * 小数加法 * @param arg1 * @param arg2 * @returns {number} */ function accAdd(arg1,arg2){ var r1,r2,m; try{r1=arg1.toString().split(".")[1].length}catch(e){r1=0} try{r2=arg2.toString().split(".")[1].length}catch(e){r2=0} m=Math.pow(10,Math.max(r1,r2)); return (arg1*m+arg2*m)/m; } /** * 小数乘法 * @param arg1 * @param arg2 * @param fix * @returns {*} */ function accMul(arg1,arg2,fix) { if(!parseInt(fix)==fix) { return; } var m=0,s1=arg1.toString(),s2=arg2.toString(); try{m+=s1.split(".")[1].length}catch(e){} try{m+=s2.split(".")[1].length}catch(e){} if(m>fix){ return (Math.round(Number(s1.replace(".",""))*Number(s2.replace(".",""))/Math.pow(10,m-fix))/Math.pow(10,fix)); }else if(m<=fix){ return (Number(s1.replace(".",""))*Number(s2.replace(".",""))/Math.pow(10,m)).toFixed(fix); }else{ return (arg1*arg2).toFixed(fix); } } }); });

功能核心知识:

通过一个全局数组来存储,点击商品后,拿到的商品信息,用数据来驱动购物车表格显示。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

您可能感兴趣的文章:

  • vuejs手把手教你写一个完整的购物车实例代码
  • js购物车实现思路及代码(个人感觉不错)
  • JavaScript编写一个简易购物车功能
  • js实现简单的购物车有图有代码
  • Javascript操纵Cookie实现购物车程序
  • 简单的前端js+ajax 购物车框架(入门篇)
  • 原生js模拟淘宝购物车项目实战
  • js实现仿购物车加减效果
  • 基于Vuejs实现购物车功能
  • Javascript实现购物车功能的详细代码


  • 上一条:
    layui实现数据表格点击搜索功能
    下一条:
    基于layui实现高级搜索(筛选)功能
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • 使用 Alpine.js 排序插件对元素进行排序(0个评论)
    • 在js中使用jszip + file-saver实现批量下载OSS文件功能示例(0个评论)
    • 在vue中实现父页面按钮显示子组件中的el-dialog效果(0个评论)
    • 使用mock-server实现模拟接口对接流程步骤(0个评论)
    • vue项目打包程序实现把项目打包成一个exe可执行程序(0个评论)
    • 近期文章
    • 智能合约Solidity学习CryptoZombie二课:让你的僵尸猎食(0个评论)
    • 智能合约Solidity学习CryptoZombie第一课:生成一只你的僵尸(0个评论)
    • 在go中实现一个常用的先进先出的缓存淘汰算法示例代码(0个评论)
    • 在go+gin中使用"github.com/skip2/go-qrcode"实现url转二维码功能(0个评论)
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf分页文件功能(0个评论)
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 欧盟关于强迫劳动的规定的官方举报渠道及官方举报网站(0个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf文件功能(0个评论)
    • Laravel从Accel获得5700万美元A轮融资(0个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2016-10
    • 2016-11
    • 2017-06
    • 2017-07
    • 2017-08
    • 2017-09
    • 2017-10
    • 2017-11
    • 2018-03
    • 2018-04
    • 2018-05
    • 2018-06
    • 2018-09
    • 2018-11
    • 2018-12
    • 2019-02
    • 2020-03
    • 2020-04
    • 2020-05
    • 2020-06
    • 2021-04
    • 2021-05
    • 2021-07
    • 2021-08
    • 2021-09
    • 2021-10
    • 2021-11
    • 2022-08
    • 2022-09
    • 2022-10
    • 2022-11
    • 2022-12
    • 2023-01
    • 2023-02
    • 2023-03
    • 2023-04
    • 2023-05
    • 2023-06
    • 2023-07
    • 2023-09
    • 2023-10
    • 2023-11
    • 2023-12
    • 2024-01
    • 2024-02
    • 2024-03
    • 2024-04
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客