Domanda

Sto memorizzando gli elementi del carrello della spesa (JSON Data) in Cookie tramite JQuery. Sono in grado di aggiungere oggetti al cookie.Di seguito è riportato il codice 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);

}
.

Campione JSON Data

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

Come posso aggiornare la quantità di un dispositivo esistente e rimuovere un dispositivo esistente nel cookie? Spero che possa essere fatto looping attraverso gli articoli.Se alcune altre alternative ci sono, ti preghiamo di suggerirmi.Anche il mio approccio è buono o dovrei andare per qualche altro approccio?

È stato utile?

Soluzione

Il tuo approccio sembra essere giusto ...

Per l'aggiornamento e l'eliminazione degli elementi nel carrello, è possibile utilizzare questo codice:

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);
} 
.

Esempio di come chiamarli:

<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>
.

Modifica:

Secondo la richiesta dell'OP nel commento, ho modificato le funzioni di cui sopra:

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);
      }
    }); 
}
.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top