功能:可以更方便的使用这个js插件来操作本地购物车数据,准确的说,是键值对,key_Val的格式;
运用:引用Jquery脚本库,然后再引用BuyCarHelper.js
方法列表:
1、加入或者修改购物车:返回true或者false
BuyCar.AddOrUpdate('商品ID',商品数量);
2、删除一条购物车数据:返回true或者false
BuyCar.Delete('商品ID');
3、清空购物车
BuyCar.DeleteAll();
4、获取购物车所有数据:返回js数组对象,item格式:113322312_5
BuyCar.QueryAll();
5、根据商品ID返回购物车中的数量:返回数字num,不存在返回0
BuyCar.GetNum('商品ID');
6、返回购物车所有商品ID,格式:1,2
BuyCar.GetSysNos();
var BuyCar = { Key: "BuyCarInfo", AddOrUpdate: function (sysno, num) { if (sysno == null || sysno == "" || sysno == undefined) { return false; } if (num == null || num == "" || num == undefined || parseInt(num) <= 0) { num = 1; } var exis = false; var obj = BuyCar.QueryAll(); if (obj == null || obj.length <= 0) { obj.push(sysno + "_" + num); } for (var i = 0; i < obj.length; i++) { var item = obj[i]; var sys = item.split("_")[0]; if (sys == sysno) { exis = true; obj[i] = sysno + "_" + num; } } if (exis == false) { obj.push(sysno + "_" + num); } var str = JSON.stringify(obj); localStorage.setItem(BuyCar.Key, str); return true; }, Delete: function (sysno) { if (sysno == null || sysno == "" || sysno == undefined) { return false; } var obj = BuyCar.QueryAll(); if (obj.length <= 0) { return true; } var newobj = []; $(obj).each(function () { var item = this; var sys = item.split("_")[0]; var num = item.split("_")[1]; if (sys != sysno) { newobj.push(item); } }); var str = JSON.stringify(newobj); localStorage.setItem(BuyCar.Key, str); return true; }, DeleteAll: function () { localStorage.setItem(BuyCar.Key, "[]"); return true; }, QueryAll: function () { var data = localStorage.getItem(BuyCar.Key); var obj = JSON.parse(data); if (obj == null || obj.length <= 0) { return []; } return obj; }, GetNum: function (sysno) { var num = 0; if (sysno == null || sysno == "" || sysno == undefined) { return num; } var obj = BuyCar.QueryAll(); if (obj.length <= 0) { return num; } $(obj).each(function () { var item = this; var sys = item.split("_")[0]; var sysnum = item.split("_")[1]; if (sys == sysno) { num = sysnum; } }); return num; }, GetSysNos: function () { var sysnos = ""; var obj = BuyCar.QueryAll(); $(obj).each(function () { var item = this; var sys = item.split("_")[0]; sysnos += "," + sys; }); return sysnos.substr(1); }}
留下您的脚步
最近评论