简单的前端js+ajax 购物车框架(入门篇)


Posted in Javascript onOctober 29, 2011

今天在公司实在没有事做,突然就想到写下商城的购物车的前端框架,当然我这里只有购物车的增删改查,也许写的并不是那么完善,但最重要的是一个入门,也希望js达人给些建议,好让我更上一个台阶。
HOHO~~~开始咯:
Js:

//为了省事,就没写自己的js ajax了 用了jquery的,当然你也可以添加到jquery的扩展方法内,哈哈,我太懒了,所以就写这里了。 
var _$ = { AJAX: function (urlparm, d, beforecall, successcall) { 
$.ajax({ 
url: "ashx/ajax_shoppingCart.ashx?" + urlparm, 
data:d, 
dataType:"Json", 
type: "POST", 
before: beforecall, 
success:successcall 
}); 
} 
}; 
(function () { 
var Jusoc = {}; 
Jusoc = { 
_inital: function () { window.Jusoc = Jusoc; }, 
Base: {}, 
DAO: {}, 
BLL: {}, 
UI: {} 
} 
Jusoc.Base = { 
Validate: { 
} 
} 
//AJAX() 
Jusoc.DAO = { 
Shopping: { 
Get: function (beforecall, successcall) { 
_$.AJAX("action=get", null, beforecall, successcall); 
}, 
Remove: function (pid, beforecall, successcall) { 
_$.AJAX("action=remove", { "pid": pid }, beforecall, successcall); 
}, 
Add: function (pid, pcount, beforecall, successcall) { 
_$.AJAX("action=add", { "pid": pid, "pcount": pcount }, beforecall, successcall); 
}, 
Set: function (pid, pcount, beforecall, successcall) { 
_$.AJAX("action=set", { "pid": pid, "pcount": pcount }, beforecall, successcall); 
} 
} 
} 
Jusoc.BLL = { 
Shopping: (function () { 
var Data = null; 
var isLock = false; 
var _successcall = null; 
var _beforecall = null; 
function Unlock() { 
isLock = false; 
} 
function Lock() { 
isLock = true; 
if(Data&&Data !=null) 
{ 
Data = null; 
} 
} 
function CallBackFunction(xhr) { 
Unlock(); 
// if (xhr[0] == "ERROR") { 
// alert(xhr[1]); 
// return; 
// } 
// else if (xhr[0] == "SUCCESS") { 
// Jusoc.BLL.Shopping.SetData(xhr[1]); 
// } 
Jusoc.BLL.Shopping.SetData(xhr); 
if (_successcall != null && _successcall) { 
_successcall.call(window, xhr); 
} 
_successcall = null; 
} 
function PrepareRequst(beforecall, successcall) { 
if (isLock) { 
return false; 
} 
Lock(); 
if (beforecall != null && beforecall) { 
_beforecall = beforecall; 
} 
if (successcall != null && successcall) { 
_successcall = successcall; 
} 
} 
return { 
Get: function (beforecall, successcall) { 
if(PrepareRequst(beforecall, successcall)==false)return false; 
Jusoc.DAO.Shopping.Get(_beforecall, CallBackFunction); 
}, 
Remove: function (pid, beforecall, successcall) { 
if(PrepareRequst(beforecall, successcall)==false)return false; 
Jusoc.DAO.Shopping.Remove(pid, _beforecall, CallBackFunction); 
}, 
Set: function (pid, pcount, beforecall, successcall) { 
if(PrepareRequst(beforecall, successcall)==false)return false; 
Jusoc.DAO.Shopping.Set(pid, pcount, beforecall, CallBackFunction); 
}, 
Add: function (pid, pcount, beforecall, successcall) { 
if(PrepareRequst(beforecall, successcall)==false)return false; 
Jusoc.DAO.Shopping.Add(pid, pcount, _beforecall, CallBackFunction); 
}, 
GetData: function () { 
//alert(Data); 
return Data; 
}, 
SetData: function (data) { Data = data; }, 
RemoveData: function () { 
if (Data != null && Data) 
Data= null; 
} 
} 
})(), 
XHR: { 
} 
} 
Jusoc.UI = { 
ShoppingCart: (function () { 
function Constract() { 
Jusoc.BLL.Shopping.Get(null,SetShoppingCart); 
} 
function SetShoppingCart(data) { 
//这里来填充购物车中的数据 
var data = Jusoc.BLL.Shopping.GetData(); 
//这里 先构建 整个的购物车 
var html = "<table class=\"shoppingcart-list\" id=\"sm\">"+ 
"<tr>"+ 
"<th>"+ 
"书啊"+ 
"</th>"+ 
"<th>"+ 
"书名"+ 
"</th>"+ 
"<th>"+ 
" 单价"+ 
"</th>"+ 
"<th>"+ 
" 数量"+ 
"</th>"+ 
"<th>"+ 
" 操作"+ 
"</th>"+ 
"</tr>"; 
for(var i =0;i<data.length;i++) 
{ 
html += "<tr>"+ 
"<td>"+ 
"<img src=\"ss\" height=\"36px\" width=\"28px\"/>"+ 
"</td>"+ 
"<td>"+ 
data[i].Name+ 
"</td>"+ 
"<td>"+ 
"¥"+data[i].Money+ 
"</td>"+ 
"<td>"+ 
"<div class=\"item-change\">"+ 
"<input type=\"text\" value='"+data[i].Count+"' />"+ 
"<span title=\"数量加一\" class=\"add\" onclick=\"Jusoc.UI.ShoppingCart.Plus(1,this.parentNode.childNodes[0].value,this.parentNode.childNodes[0])\"></span> <span "+ 
"title=\"数量减一\" class=\"cut\" onclick=\"Jusoc.UI.ShoppingCart.Minus(1,this.parentNode.childNodes[0].value,this.parentNode.childNodes[0])\"></span>"+ 
"</div>"+ 
"</td>"+ 
"<td>"+ 
"<span class=\"RemoveLink\" onclick=\"Jusoc.UI.ShoppingCart.Remove(1,this.parentNode.parentNode)\">Remove From Cark</span>"+ 
"</td>"+ 
"</tr>"; 
} 
html+="</table>"; 
document.body.innerHTML+=html; 
} 
function AddToPanel(data) { 
//这里是对 添加一个商品到购物车 来修改前台样式 
var obj = document.getElementById("sm"); 
var html = "<td>"+ 
"<img src=\"soo\" height=\"36px\" width=\"28px\"/>"+ 
"</td>"+ 
"<td>"+ 
data.Name+ 
"</td>"+ 
"<td>"+ 
"¥"+data.Money+ 
"</td>"+ 
"<td >"+ 
"<div class=\"item-change\">"+ 
"<input type=\"text\" value='"+data.Count+"' />"+ 
"<span title=\"数量加一\" class=\"add\" onclick=\"Jusoc.UI.ShoppingCart.Plus(1,this.parentNode.childNodes[0].value,this.parentNode.childNodes[0])\"></span> <span "+ 
"title=\"数量减一\" class=\"cut\" onclick=\"Jusoc.UI.ShoppingCart.Minus(1,this.parentNode.childNodes[0].value,this.parentNode.childNodes[0])\"></span>"+ 
"</div>"+ 
"</td>"+ 
"<td>"+ 
"<span class=\"RemoveLink\" onclick=\"Jusoc.UI.ShoppingCart.Remove(1,this.parentNode.parentNode)\">Remove From Cark</span>"+ 
"</td>"; 
var row = obj.insertRow(1); 
row.innerHTML = html; 
return; 
obj.childNodes[0].innerHTML += html; 
} 
function UpdatePanel(obj, count) { 
//这里是从购物车中 增加 或者 减少 修改操作 
obj.value = count; 
} 
function RemoveFromPanel(child) 
{ 
var obj = document.getElementById("sm"); 
obj.childNodes[0].removeChild(child); 
} 
return { 
PageLoad: function () { 
Constract(); 
}, 
Add: function (pid, pcount) { 
Jusoc.BLL.Shopping.Add(pid,pcount, null, AddToPanel); 
}, 
Plus: function (pid, pcount, obj) { 
pcount = parseInt(pcount) + 1; 
Jusoc.BLL.Shopping.Set(pid, pcount, function () { alert("before") }, function (data) { UpdatePanel(obj, pcount) }); 
}, 
Minus:function(pid,pcount,obj){ 
pcount = parseInt(pcount) - 1; 
Jusoc.BLL.Shopping.Set(pid,pcount,null,function(data){ UpdatePanel(obj,pcount)}); 
}, 
Remove:function(pid,obj){ 
Jusoc.BLL.Shopping.Remove(pid,null,function(data){ RemoveFromPanel(obj);}); 
} 
} 
})() 
} 
Jusoc._inital(); 
})()

Tips:这里的显示页面仅仅是demo,如需要,可以自己定制。

HTML:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<title></title> 
<style type="text/css"> 
*{margin:0; padding:0;border:0} 
body{font-size: .85em;font-family: Verdana,Helvetica,SimSun,Arial, "Arial Unicode MS" ,MingLiu,PMingLiu, "MS Gothic" ,sans-serief;color: #232323;text-align:center; font-size:12px; background-color: #fff;} 
#ltRight {width: 722px;float: left;position: relative;padding: 16px 40px;text-align:left} 
/*shoppingcart-list*/ 
.shoppingcart-list{border: 1px solid #C2D8ED;border-collapse: collapse;color: #666666;width: 750px;} 
.shoppingcart-list th{background-color: #F1F7F9;color: #666;font-size: 13px;text-align: center;} 
.shoppingcart-list tr{border: 1px solid #C2D8ED;line-height: 25px;text-align:center;} 
.shoppingcart-list tr:hover{background-color: #fff;} 
th, td{padding-left: 5px;} 
.item-change{font: 12px/74px Arial;text-align: center;width: 58px;padding: 28px 0 0 25px;height: 46px;} 
.item-change input{width: 36px;height: 19px;border: 1px solid #C4C4C4;background: white;float: left;margin-top: 1px;text-align: center;line-height: 19px;margin-right: 1px;font-size: 12px;} 
.item-change span{width: 17px;height: 8px;float: left;display: block;margin-top: 1px;font-size: 0;line-height: 0;border: 1px solid #C9C9C9;text-indent: -9999px; cursor:pointer} 
.item-change .add{background: url(Images/leftmenu/icon_cart.gif) no-repeat 6px -58px;} 
.item-change .cut{background: url(Images/leftmenu/icon_cart.gif) no-repeat 6px -67px;} 
.box_window {width: 204px;position:absolute;left:-9999px;} 
.box_window .updateTip{width: 184px;border: solid 1px #DFC9B2;background-color: #FDF2E3;line-height: 20px;padding: 11px 8px 8px;} 
.t_c {text-align: center;} 
.c_red_s {color: #C30;font-weight: bold;} 
.box_window .c_b {width: 9px;height: 6px;margin: 0 auto;background: url(Images/leftmenu/p_window_b.gif) no-repeat;position: relative;margin-top: -1px;font-size: 0;overflow: hidden;} 
.RemoveLink{ cursor:pointer} 
</style> 
<script src="../js/jquery-1.6.js" type="text/javascript"></script> 
<script src="Js/base.Jusoc.js" type="text/javascript"></script> 
<script type="text/javascript"> 
window.onload =function () { 
Jusoc.UI.ShoppingCart.PageLoad(); 
} 
</script> 
</head> 
<body> 
<div id="ltRight"> 
<h5 class="htitle"> 
购物车 
<span style="float:right; padding-right:20px;">总金额:¥<em id="cart-total"></em></span> 
</h5> 
<div id="update-message" style="text-align: left; text-indent: 48px; font-weight: bold; 
padding: 6px;"> 
</div> 
<div id="item-tip" class="box_window"> 
<div class="updateTip"> 
<p class="t_c"> 
修改成功!</p> 
<p> 
您的商品总金额为¥<span class="c_red_s" id="update_total_account">@Model.Total.ToString("0.00")</span>元</p> 
<p class="t_c"> 
<a href="javascript:void(0)" onclick="$('#item-tip').css('left','-9999px');">关闭</a></p> 
</div> 
<div class="c_b"> 
</div> 
</div> 
<h5><a href="javascript:Jusoc.UI.ShoppingCart.Add(1,1)">Add one to Shopping Cart</a></h5> 
</div> 
</body> 
</html>

ashx:这个我就不就木有必要黏贴出来了,根据自己的业务去写额。

总结:OK,搞定!!

Javascript 相关文章推荐
JQuery团队打造的javascript单元测试工具QUnit介绍
Feb 26 Javascript
JS 密码强度验证(兼容IE,火狐,谷歌)
Mar 15 Javascript
JavaScript实现多维数组的方法
Nov 20 Javascript
最精简的JavaScript实现鼠标拖动效果的方法
May 11 Javascript
javascript实现模拟时钟的方法
May 13 Javascript
Js制作点击输入框时默认文字消失的效果
Sep 05 Javascript
jQuery实现遍历复选框的方法示例
Mar 06 Javascript
Bootstrap实现的标签页内容切换显示效果示例
May 25 Javascript
ionic2懒加载配置详解
Sep 01 Javascript
JS关于刷新页面的相关总结
May 09 Javascript
微信小程序中的video视频实现 自定义播放按钮、封面图、视频封面上文案
Jan 02 Javascript
详解如何在Canvas中添加事件的方法
Apr 17 Javascript
分享一个自己写的table表格排序js插件(高效简洁)
Oct 29 #Javascript
Json2Template.js 基于jquery的插件 绑定JavaScript对象到Html模板中
Oct 29 #Javascript
基于jQuery的输入框在光标位置插入内容, 并选中
Oct 29 #Javascript
基于jquery的无限级联下拉框js插件
Oct 29 #Javascript
对setInterval在火狐和chrome切换标签产生奇怪的效果之探索,与解决方案!
Oct 29 #Javascript
仿猪八戒网左下角的文字滚动效果
Oct 28 #Javascript
js实现双向链表互联网机顶盒实战应用实现
Oct 28 #Javascript
You might like
Ajax PHP简单入门教程代码
2008/04/25 PHP
php简单的上传类分享
2016/05/15 PHP
Json_decode 解析json字符串为NULL的解决方法(必看)
2017/02/17 PHP
php实现批量上传数据到数据库(.csv格式)的案例
2017/06/18 PHP
Swoole 5将移除自动添加Event::wait()特性详解
2019/07/10 PHP
Laravel 默认邮箱登录改成用户名登录的实现方法
2019/08/12 PHP
PHP中通过getopt解析GNU C风格命令行选项
2019/11/18 PHP
JS应用之禁止抓屏、复制、打印
2008/02/21 Javascript
JS对URL字符串进行编码/解码分析
2008/10/25 Javascript
JavaScript中window.open用法实例详解
2015/04/15 Javascript
javascript轮播图算法
2016/10/21 Javascript
jQuery插件echarts设置折线图中折线线条颜色和折线点颜色的方法
2017/03/03 Javascript
js实现省市级联效果分享
2017/08/10 Javascript
vue组件间通信子与父详解(二)
2017/11/07 Javascript
基于Vue实现拖拽功能
2020/07/29 Javascript
vue中的watch监听数据变化及watch中各属性的详解
2018/09/11 Javascript
对layui中table组件工具栏的使用详解
2019/09/19 Javascript
ES6如何用一句代码实现函数的柯里化
2020/01/18 Javascript
vue项目使用$router.go(-1)返回时刷新原来的界面操作
2020/07/26 Javascript
Python中处理字符串之isalpha()方法的使用
2015/05/18 Python
python实现拓扑排序的基本教程
2018/03/11 Python
python3爬虫之设计签名小程序
2018/06/19 Python
彻底搞懂 python 中文乱码问题(深入分析)
2020/02/28 Python
大学毕业生最详细的自我评价分享
2013/11/18 职场文书
教育实习生的自我评价分享
2013/11/21 职场文书
房产转让协议书
2014/04/11 职场文书
团队队名口号大全
2014/06/06 职场文书
专项法律服务方案
2014/06/11 职场文书
建筑工程技术专业求职信
2014/07/16 职场文书
实习协议书范本
2014/09/25 职场文书
离职报告范文
2014/11/04 职场文书
党员个人总结自评
2015/02/14 职场文书
答谢酒会主持词
2015/07/02 职场文书
优质服务心得体会(共4篇)
2016/01/22 职场文书
导游词之南京莫愁湖公园
2019/11/13 职场文书
关于springboot配置druid数据源不生效问题(踩坑记)
2021/09/25 Java/Android