商城开发项目,现在需要做出APP,无奈出场前android但不是很精通。最后选择phonegap实现app。
由于之前办理购物车分为登陆和登陆后两种情况,登录前必须充分利用本地存储。而基于phonegap本地存储的发展是使用Html5的localstorage功能实现。
特分享与此~
//内置数组对象成员方法补充,移除元素
Array.prototype.remove=function(dx)
{if(isNaN(dx)||dx>this.length){return false;}for(var i=0,n=0;i
//购物车类定义
cart = function(){this.lstore = window.localStorage;this.init();
};
cart.prototype={init:function(){var cart_goods_material=this.lstore.getItem('cart');try{this.cart_goods=JSON.parse(cart_goods_material);if(this.cart_goods==null)this.cart_goods=[];}catch(e){this.cart_goods=[];}},getall:function(){ //获得购物车全部商品列表return this.cart_goods;},insert:function(goods){ //插入商品var index=this.finds(goods.goods_id);if(index>-1){this.cart_goods[index].goods_number+=goods.goods_number;}else{this.cart_goods.push(goods);}var cart_goods_cooked = JSON.stringify(this.cart_goods);this.lstore.setItem('cart',cart_goods_cooked);return true;},update:function(goods_id,data){var index=this.finds(goods_id);if(index>-1){for(var i in data){this.cart_goods[index][i]=data[i];} }var cart_goods_cooked = JSON.stringify(this.cart_goods);this.lstore.setItem('cart',cart_goods_cooked);return true;},addnum:function(goods_id,num){ //更新购物车商品数量var index=this.finds(goods_id);var num=this.cart_goods[index]['goods_number']+num;this.update(goods_id,{'goods_number':num}); },del:function(goods_id){ var index=this.finds(goods_id);this.cart_goods.remove(index);var cart_goods_cooked = JSON.stringify(this.cart_goods);this.lstore.setItem('cart',cart_goods_cooked);return true;},finds:function(goods_id){var index=-1;for(var i in this.cart_goods ){var g=this.cart_goods[i];if(g.goods_id==goods_id){index = i;break;}}return index;},clear:function(){this.lstore.setItem('cart','');this.cart_goods=[];},amount:function(){var amount=0;for(var i in this.cart_goods ){amount += this.cart_goods[i]['goods_number']*this.cart_goods[i]['goods_price'];}return amount;}
}
版权声明:本文博客原创文章。博客,未经同意,不得转载。