简体   繁体   English

从包含购物车项目的cookie更新和删除json数据

[英]update and remove json data from a cookie containing shopping cart items

I am storing the shopping cart items (json data) in cookie via jquery. 我通过jquery将购物车项目(json数据)存储在cookie中。 I am able to add items to the cookie. 我能够将项目添加到cookie。 Following is the javascript code. 以下是javascript代码。

$.cookie.json = true;
// empty cart
var emptydata = { "cartItems": [] };

var devicedata = new function (deviceId, qty) {
return { "deviceId": deviceId, "quantity": qty };
}


// to create json data
function createJSON(id, qty) {
    if (!$.cookie('devicelist')) {
      $.cookie('devicelist', emptydata);
    }
 devicedata.deviceId = id;
 devicedata.quantity = qty;
 cart = $.cookie('devicelist');
 cart.cartItems.push(devicedata);
 return cart;
}

// function to be called , when Add to cart button is clicked
function saveItems() {

   var deviceId = $('#lbldeviceId').html();
   var Qty = $('#txtQty').val();

   var jsondata = createJSON(deviceId, Qty);

   $.cookie('devicelist', jsondata);

}

sample json data 样本json数据

 { "cartItems": [
   {
     "deviceId": "1",
     "quantity": "1"
   },
   {
     "deviceId": "2",
     "quantity": "1"
   }
]}

How can i update the quantity of an existing device and remove an existing device in the cookie? 如何更新现有设备的数量并删除Cookie中的现有设备? Hope it can be done by looping through the items. 希望可以通过遍历项目来完成。 If any other alternatives are there, please suggest me. 如果还有其他选择,请建议我。 Also is my approach good or should i go for some other approach? 我的方法还是好的,还是应该采用其他方法?

Your approach seems to be right... 您的方法似乎是正确的...

For updating and deleting items in the cart, you can use this code: 要更新和删除购物车中的物品,可以使用以下代码:

function modifyItem(deviceId, Qty)
{
    var cart_items = $.cookie('devicelist').cartItems;

    $(cart_items).each( function(i, v) {
      if (v && v.deviceId == deviceId) {      
      cart_items[i].quantity = Qty;   
      }
    });
    var obj = { "cartItems": cart_items };
    $.cookie('devicelist', obj);
}

function deleteItem(deviceId)
{
    var cart_items = $.cookie('devicelist').cartItems;

    $(cart_items).each( function(i, v) {
      if (v && v.deviceId == deviceId) {    
      cart_items.splice(i, 1);    
      }
    });
    var obj = { "cartItems": cart_items };
    $.cookie('devicelist', obj);
} 

Example of how to call them: 如何调用它们的示例:

<a href="javascript:void(0);" onClick="javascript:modifyItem(3, 5);">Modify deviceId 3</a>
<a href="javascript:void(0);" onClick="javascript:deleteItem(4);">Delete deviceId 4</a>

EDIT: 编辑:

As per OP's request in the comment, i modified the above functions: 根据OP在评论中的要求,我修改了上述功能:

function modifyItem(deviceId, Qty)
{
    cart = $.cookie('devicelist');  
    $(cart.cartItems).each( function(i, v) {
      if (v && v.deviceId == deviceId) {
        v.quantity = Qty;
        $.cookie('devicelist', cart);
      }
    });    
}

function deleteItem(deviceId)
{
    cart = $.cookie('devicelist');  
    $(cart.cartItems).each( function(i, v) {
      if (v && v.deviceId == deviceId) {
        cart.cartItems.splice(i, 1);
        $.cookie('devicelist', cart);
      }
    }); 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM