here you go.. Modified your addProduct function to check for 3 items in data.total
. if not give an alert..
try this
function addProduct(name,price){
if(data.total < 3){
function add(){
for(var i=0; i<data.total; i++){
var row = data.rows[i];
if (row.name == name){
row.quantity += 1;
return;
}
}
data.total += 1;
data.rows.push({
name:name,
quantity:1,
price:price
});
}
add();
totalCost += price;
$('#cartcontent').datagrid('loadData', data);
$('div.cart .total').html('Total: $'+totalCost);
}else{
alert('cannot have more than 3 itrems');
}
}
updated
if you need to check for quantity then.. check for quantity instead of total.. :)
try this
function addProduct(name,price){
var totalQuantity=sumQuantity(data);
if(totalQuantity < 3){
function add(){
for(var i=0; i<data.total; i++){
var row = data.rows[i];
if (row.name == name){
row.quantity += 1;
return;
}
}
data.total += 1;
data.rows.push({
name:name,
quantity:1,
price:price
});
}
add();
totalCost += price;
$('#cartcontent').datagrid('loadData', data);
$('div.cart .total').html('Total: $'+totalCost);
}else{
alert('cannot have more than 3 itrems');
}
}
//new function to sum up all the quantity.
function sumQuantity(data){
var sum=0;
for(var i=0; i<data.total; i++){
sum += data.rows[i].quantity;
}
return sum;
}