I currently have an array:
[
0: {title: "Banana", price: 1.00, count: 2},
1: {title: "Taco", price: 3.99, count: 1},
2: {title: "Burrito", price: 6.50, count: 1},
3: {title: "Soda", price: 1.25, count: 1},
]
which I display using the following code snippet:
var dataItems = "";
for (i = 0; i < itemsArray.length; i++){
var rand = Math.floor(Math.random()*1000)+1;
dataItems += "<div id='"+rand+"' class='row pb-3 mx-3 mt-3' style='border-bottom:1px solid #eeeeee;'>";
dataItems += "<div id='fs-7' class='col-2 text-center font-weight-bold quant'>";
dataItems += itemsArray[i].count+"x";
dataItems += "</div>";
dataItems += "<div id='fs-7' class='col-5'>";
dataItems += itemsArray[i].title+" "+i;
dataItems += "</div>";
dataItems += "<div class='col-3 pricep font-weight-bold text-right' style='color:#27c727;' id='price-item'>";
dataItems += parseFloat(itemsArray[i].price).toFixed(2).toLocaleString()+" €";
dataItems += "</div>";
dataItems += "<div onclick='removeItem("+i+")' class='col-2'><img src='delete.png' style='max-height:20px;' class='img-fluid' /></div>";
dataItems += "</div>";
}
$("#list-items").html(dataItems);
In the removeItem function:
function removeItem(item){
itemsArray.splice(item,1);
}
However, when I delete an item, the position numbers of the remaining items do not update accordingly. How can I dynamically adjust and refresh the position numbers after removing an element?
Currently, my list looks like this:
2x Banana 1.00€ delete (position 0)
1x Taco 3.99€ delete (position 1)
1x Burrito 6.50€ delete (position 2)
1x Soda 1.25€ delete (position 3)
If I remove Banana, the list would appear as follows:
1x Taco 3.99€ delete (position 1)
1x Burrito 6.50€ delete (position 2)
1x Soda 1.25€ delete (position 3)