So I have created this table of the 2 products that you may "add to your cart" with their respective quantities. Now, I have it set up to whenever the "add to cart" button is pressed the quantity of that product will be decremented. One thing I am having trouble with is getting the button to disable in respect to when the quantity hits zero. Any help would be appreciated, thanks.
const data = [
{Title: "Beer Heineken 6-pack", Quantity: 5, Price: 6.75, Action: null},
{Title: "Sun-dry Fish 1 lb.", Quantity: 3, Price: 10, Action: null}
];
$(document).ready(function() {
$("div#preload h2").html();
let table = $('<table>').addClass('table table-
striped').appendTo(document.body);
table.append($('<thead>').append($('<tr>')));
table.append($('<tbody>'));
let headerRow = table.find('thead tr');
Object.keys(data[0]).forEach(function(key) {
headerRow.append($('<th>').text(key));
})
let tbody = table.find('tbody');
let tableRender = function(){
data.forEach(function(dataItem, index) {
let tr = $('<tr>').appendTo(tbody);
Object.keys(dataItem).forEach(function(key){
if(dataItem[key] !== null) {
tr.append($('<td>').text(dataItem[key]));
}
else{
tr.append($('<td>').append($('<button>')
.addClass('btn btn-secondary').text("Add To Cart")
.click(function() {
dataItem.Quantity = dataItem.Quantity < 1 ? 0 : dataItem.Quantity - 1;
tbody.empty();
tableRender();
})));
}
})
})
}
tableRender();
})