首页 > web前端 > js教程 > jquery.cookie实现的客户端购物车操作实例

jquery.cookie实现的客户端购物车操作实例

PHP中文网
发布: 2017-03-21 14:16:11
原创
1476 人浏览过

本文实例讲述了jquery.cookie实现的客户端购物车操作。分享给大家供大家参考,具体如下:

//购物车
var Cart = function () {
  this.Count = 0;
  this.Total = 0;
  this.Items = new Array();
};
//购物车集合对象
var CartItem = function () {
  this.Id = 0;
  this.Name = "";
  this.Count = 0;
  this.Price = 0;
};
 
//购物车操作
var CartHelper = function () {
  this.cookieName = "yxhCart";
  this.Clear = function () {
    var cart = new Cart();
    this.Save(cart);
    return cart;
  };
  //向购物车添加
  this.Add = function (id, name, count, price) {
    var cart = this.Read();
    var index = this.Find(id);
    //如果ID已存在,覆盖数量
    if (index > -1) {
      cart.Total -= (((cart.Items[index].Count * 100) * (cart.Items[index].Price * 100)) / 10000);
      cart.Items[index].Count = count;
      cart.Total += (((cart.Items[index].Count * 100) * (cart.Items[index].Price * 100)) / 10000);
    } else {
      var item = new CartItem();
      item.Id = id;
      item.Name = name;
      item.Count = count;
      item.Price = price;
      cart.Items.push(item);
      cart.Count++;
      cart.Total += (((cart.Items[index].Count * 100) * (cart.Items[index].Price * 100)) / 10000);
    }
    this.Save(cart);
    return cart;
  };
  //改变数量
  this.Change = function (id, count) {
    var cart = this.Read();
    var index = this.Find(id);
    cart.Items[index].Count = count;
    this.Save(cart);
    return cart;
  };
  //移出购物车
  this.Del = function (id) {
    var cart = this.Read();
    var index = this.Find(id);
    if (index > -1) {
      var item = cart.Items[index];
      cart.Count--;
      cart.Total = cart.Total - (((item.Count * 100) * (item.Price * 100)) / 10000);
      cart.Items.splice(index, 1);
      this.Save(cart);
    }
    return cart;
  };
  //根据ID查找
  this.Find = function (id) {
    var cart = this.Read();
    var index = -1;
    for (var i = 0; i < cart.Items.length; i++) {
      if (cart.Items[i].Id == id) {
        index = i;
      }
    }
    return index;
  };
  //COOKIE操作
  this.Save = function (cart) {
    var source = "";
    for (var i = 0; i < cart.Items.length; i++) {
      if (source != "") { source += "|$|"; }
      source += this.ItemToString(cart.Items[i]);
    }
    $.cookie(this.cookieName, source);
  };
  this.Read = function () {
    //读取COOKIE中的集合
    var source = $.cookie(this.cookieName);
    var cart = new Cart();
    if (source == null || source == "") {
      return cart;
    }
    var arr = source.split("|$|");
    cart.Count = arr.length;
    for (var i = 0; i < arr.length; i++) {
      var item = this.ItemToObject(arr[i]);
      cart.Items.push(item);
      cart.Total += (((item.Count * 100) * (item.Price * 100)) / 10000);
    }
    return cart;
  };
  this.ItemToString = function (item) {
    return item.Id + "||" + escape(item.Name) + "||" + item.Count + "||" + item.Price;
  };
  this.ItemToObject = function (str) {
    var arr = str.split(&#39;||&#39;);
    var item = new CartItem();
    item.Id = arr[0];
    item.Name = unescape(arr[1]);
    item.Count = arr[2];
    item.Price = arr[3];
    return item;
  };
};
登录后复制

希望本文所述对大家jQuery程序设计有所帮助。

相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
作者最新文章
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板