MVVM架构~knockoutjs实现简单的购物车
概念相关
购物车相信大家都用过,很方便,可以将多个商品添加到购物车,并且可以修改购买商品的数据,当然为了用户体验好,在修改数据时,你的价格也会出现变化的,这使用JS可以实现,但我认为,代码量挺大的,而使用knockoutjs可以大大减少代码量,而且更重要的是,当前台页面有所调整时,这个JS只需要简单调整,而不需要改后台代码!
代码相关
下面看一下实现简单购物车的代码
1 View部分
<table> <thead> <tr> <th>商品</th> <th>单价</th> <th>数量</th> <th>小计</th> <th></th> </tr> </thead> <tbody data-bind="foreach:lines"> <tr> <td data-bind="with:product"> <span data-bind="text:name"></span></td> <td data-bind="with:product"><span data-bind='text:formatCurrency(price)' /></td> <td> <input data-bind='visible: product, value: productCount, valueUpdate: "afterkeydown"' /> </td> <td><span data-bind="visible:product,text:formatCurrency(subtotal())"></span></td> <td><a href='#' data-bind='click: $parent.removeLine'>Remove</a></td> </tr> </tbody> </table> <p class='grandTotal'> Total value: <span data-bind='text: grandTotal()'></span> </p> <button data-bind='click: addLine'>Add product</button>
2 JS部分
<script type="text/ecmascript"> function formatCurrency(value) { return "¥" + value; } var Product = function (id, name, price) { self = this; self.id = id; self.name = name; self.price = price; } var CartItem = function (product) { self = this; self.product = ko.observable(product); self.productCount = ko.observable(1); self.subtotal = ko.dependentObservable(function () { return this.product() ? this.product().price * parseInt("0" + this.productCount(), 10) : 0; }.bind(self)); }; var CartList = function () { var self = this; self.lines = ko.observableArray([new CartItem(new Product(1, "test1", 100))]); self.addLine = function () { self.lines.push(new CartItem(new Product(2, "test2", 200))) }; self.removeLine = function (line) { self.lines.remove(line) }; self.grandTotal = ko.computed(function () { var total = 0; $.each(self.lines(), function () { total += this.subtotal(); }) return total; }); }; ko.applyBindings(new CartList()); </script>
3 有图有真相
完成代码如下

<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title></title> <script src="jquery-1.7.1.min.js" type="text/javascript"></script> <script src="knockout-2.1.0.js" type="text/javascript"></script> </head> <body> <table> <thead> <tr> <th>商品</th> <th>单价</th> <th>数量</th> <th>小计</th> <th></th> </tr> </thead> <tbody data-bind="foreach:lines"> <tr> <td data-bind="with:product"> <span data-bind="text:name"></span></td> <td data-bind="with:product"><span data-bind='text:formatCurrency(price)' /></td> <td> <input data-bind='visible: product, value: productCount, valueUpdate: "afterkeydown"' /> </td> <td><span data-bind="visible:product,text:formatCurrency(subtotal())"></span></td> <td><a href='#' data-bind='click: $parent.removeLine'>Remove</a></td> </tr> </tbody> </table> <p class='grandTotal'> Total value: <span data-bind='text: grandTotal()'></span> </p> <button data-bind='click: addLine'>Add product</button> <script type="text/ecmascript"> function formatCurrency(value) { return "¥" + value; } var Product = function (id, name, price) { self = this; self.id = id; self.name = name; self.price = price; } var CartItem = function (product) { self = this; self.product = ko.observable(product); self.productCount = ko.observable(1); self.subtotal = ko.dependentObservable(function () { return this.product() ? this.product().price * parseInt("0" + this.productCount(), 10) : 0; }.bind(self)); }; var CartList = function () { var self = this; self.lines = ko.observableArray([new CartItem(new Product(1, "test1", 100))]); self.addLine = function () { self.lines.push(new CartItem(new Product(2, "test2", 200))) }; self.removeLine = function (line) { self.lines.remove(line) }; self.grandTotal = ko.computed(function () { var total = 0; $.each(self.lines(), function () { total += this.subtotal(); }) return total; }); }; ko.applyBindings(new CartList()); </script> </body> </html>
感谢您的阅读!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 记一次.NET内存居高不下排查解决与启示
2013-02-21 DDD~microsoft NLayerApp项目中的层次结构图
2013-02-21 DDD~基础设施层
2012-02-21 何为.Net Remoting