[英]How to add same product with different specifications in AngularJS
I have a list of products and a shopping cart. 我有产品清单和购物车。 Let's say I have a product something like this: 假设我有一个类似这样的产品:
{
"ProductId" : "1",
"ProductName" : "Knee Caps",
"Specifications" : {
"Size" : ["S","M","L","XL","XXL"],
"GrossWeight" : "200gm each",
"Colour" : ["Beige","Grey"]
},
"SellingPrice" : 80
}
What I did so far is this: 到目前为止,我所做的是:
if(localStorage.getItem("productTable") != null)
{
productTable = JSON.parse(localStorage["productTable"]);
productTable[Id]={ProductId : Id, Colour : selectedColour, Size : selectedSize, QuantityAdded : selectedQuantity};
}
else {
productTable[Id]={ProductId : Id, Colour : selectedColour, Size : selectedSize, QuantityAdded : selectedQuantity};
}
What this code is doing is, it add's the product on the first time but if i want the same product with different Sizes and Color's , it replaces the original one. 该代码的作用是,它是第一次添加产品,但是如果我想要具有不同尺寸和颜色的相同产品,它将替换原始产品。 Now I know this approach is not completely correct. 现在我知道这种方法并不完全正确。 Can someone please help me to figure out how can i add same product with different specifications? 有人可以帮我弄清楚如何添加具有不同规格的同一产品吗?
From what I understood, you wanted to keep Specifications
property if it exists. 据我了解,您想保留“ Specifications
属性(如果存在)。 I hope this will work. 我希望这会起作用。 Feel free to let me know in case you get some error. 如果您遇到任何错误,请随时告诉我。 Hopefully, you can get the clue aboout what I am doing 希望你能了解我在做什么
if(localStorage.getItem("productTable") != null)
{
productTable = JSON.parse(localStorage["productTable"]);
productTable[Id]={
ProductId : Id,
Colour : selectedColour,
Size : selectedSize,
Specifications : checkSpecification(productTable[Id].Specifications,size,weight,color)
QuantityAdded : selectedQuantity
};
}
else {
productTable[Id]={ProductId : Id, Colour : selectedColour, Size : selectedSize, QuantityAdded : selectedQuantity};
}
function checkSpecification(obj,size,weight,color){
if(obj){
obj['Size'].push(size);
obj['GrossWeight'] = weight || '';
obj['Colour'].push(color);
return obj;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.