基本的に、私は最近(非常に基本的なレベルで)製品のカスタマイズをサポートするためにアップグレードしたカートを持っています。アップグレードには、名前、価格、数量に加えて、特定の自転車の色とマテリアルの種類が含まれています。カートから商品を正しく取り除くことができません(JavaScript&Jquery)
問題は、私が追加した新機能はコンボボックスやオプションを使用しているため、以前に追加した組み合わせを削除しようとすると色と素材の組み合わせが少し異なるカートにアイテムを追加するたびに私は最新のものを削除することができます。
私はこれを試して説明するよりも、コードに表示する方が簡単だと思います。 これは、カートオフの項目を削除するための私のロジックです:興味がある人のために
//removing one particular item completely from the cart
AS_shoppingCart.removeItemAll = function(name, color, material){
//for every item in the array which has the same name; remove.
for (var i in this.cartShop)
if(this.cartShop[i].name === name, this.cartShop[i].color === color, this.cartShop[i].material === material) {
this.cartShop.splice(i, 1);
break;
};
AS_shoppingCart.saveLocalCart();
};
、これは私がアレイ上のオブジェクトのインスタンスを格納する方法である:
//THE LOGIC FOR THE SHOPPING CART - OOP
var AS_shoppingCart = {};
//cart where the item objects will be stored
AS_shoppingCart.cartShop = [];
//item object and its properties
AS_shoppingCart.Item = function(name, price, quantity, color, material) {
this.name = name;
this.price = price;
this.quantity = quantity;
this.color = color;
this.material = material;
};
<div>
<h4>Customisation:</h4>
<table>
<tr>
<th>Color</th>
<th>Material</th>
</tr>
<tr>
<td>
<select id="colors" name="colors">
<option data-color="Default">Default</option>
<option data-color="Blue">Blue</option>
<option data-color="Green">Green</option>
<option data-color="Brown">Brown</option>
</select>
</td>
<td>
<select id="materials" name="materials">
<option data-material="Alloy">Alloy</option>
<option data-material="Steel">Steel</option>
<option data-material="Carbon Fibre">Carbon Fibre</option>
<option data-material="Titanium">Titanium</option>
</select>
</td>
</tr>
</table>
</div>
<div class="button-group">
<button class="add-to-cart" data-name="Aluminum road bike " data-price="256">Add to cart</button>
</div>
これは、それが使われるようにロジックを置くjQueryの一部です。注意;私はスニペットで無関係な部分を切り抜いた。
$(document).ready(function(){
/* CART */
//assigning a click event to DOM object
$(".add-to-cart").click(function(event){
//prevents the page from being refreshed
event.preventDefault();
//sets the name variable to a clicked data-name
var name = $(this).attr("data-name");
//sets the price to the number version of data-price attribute
var price = Number($(this).attr("data-price"));
var color = $('#colors option:selected').data('color');
var material = $('#materials option:selected').data('material');
$(".add-to-cart").attr({
"data-color" : color,
"data-material" : material
});
AS_shoppingCart.addItem(name, price, 1, color, material);
displayCart();
});
$("#show-cart").on("click",".delete-item", function(event){
var name = $(this).attr("data-name");
console.log(name);
AS_shoppingCart.removeItemAll(name);
displayCart();
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
だから私の質問は、私が実際にそれらを削除するために、カート内の古いアイテムに自分の製品アイテムのオプションを設定する必要がないことを保証する行くのですか、です。
ありがとうございます。
EDIT:私のaddItem機能を示す:
//adds items to the cart
AS_shoppingCart.addItem = function(name, price, quantity, color, material){
/*checks to see if the item with the identical name exists in the cart
if so, it will only increment the quantity of the said item (no redundancies)*/
for(let item of this.cartShop) {
if(item.name === name && item.color === color && item.material === material) {
item.quantity += quantity;
this.saveLocalCart();
return;
};
};
var item = new this.Item(name, price, quantity, color, material);
this.cartShop.push(item);
this.saveLocalCart();
};
それはサーバー側を扱うために安全ですか?アイテムを削除した後、カートの内容をリロードします。 – Yuri
このウェブサイトは決して生きるつもりはありません。それは教育目的のためだけです。申し訳ありませんが、投稿に記載するのを忘れてしまいました。 – Vocaloidas
addItem関数を表示できますか?私たちはバイブルを持つことができますか? – sniels