I'm creating this application where you get the total price of all items selected based on user selection. I have to get the summation of the results of each row where each row is actually composed by 2 selects (ID/item) and one input for quantity. Thing is, I added an additional function where you can keep adding rows depending on the user. Therefore, I need to create a loop function to check each row selection and sum all these up and throw the result to the html -- and this where I'm stuck.
BTW, the price of the item selected will be based on a variable array. After fetching the price, you multiply that by the quantity then you get the total price for one row. The total price is the summation of all rows where there's complete data.
Your help is greatly appreciated.
To explain it better for you, here's how it looks like and here's a jsfiddle:
<div class="complrow">
<div class="span4">ID</div>
<div class="span4">Fruit</div>
<div class="span4">Quantity</div>
</div>
<div class="complrow">
<button id="addrow">Add Rows</button><br/>
</div>
<button id="thisisit">Check!</button><br/>
<div>
<p>Total Price:</p><br/>
<p id="try"></p>
</div>
with a bit of CSS:
#try{
margin-top:20px;
border: 1px solid black;
}
.span4{
display: inline-block;
width: 100px;
margin-left: 10%;
}
and here's the js:
var firstselector = '<select class="firstselectorclass">'+
'<option value=""></option>'+
'<option value="1">1</option>'+
'<option value="2">2</option>'+
'<option value="3">3</option>'+
'<option value="4">4</option>'+
'</select>';
var secondselector = '<select class="secondselectorclass">'+
'<option value=""></option>'+
'<option value="apple">Apple</option>'+
'<option value="lemon">Lemon</option>'+
'<option value="orange">Orange</option>'+
'<option value="banana">Banana</option>'+
'</select>';
var completerow = '<div class="complrow"><div class="span4">'+ firstselector + '</div><div class="span4">'+ secondselector + '</div><div class="span4"><input type="number" min="0" id="actqty" class="actqtyclass" placeholder="0" style="max-width:50px;"></div></div>';
var result = [{"qty":"1","fruit":"apple","price":"19.00"},
{"qty":"1","fruit":"lemon","price":"29.00"},
{"qty":"1","fruit":"orange","price":"39.00"},
{"qty":"1","fruit":"banana","price":"49.00"},
{"qty":"2","fruit":"apple","price":"20.00"},
{"qty":"2","fruit":"lemon","price":"30.00"},
{"qty":"2","fruit":"orange","price":"40.00"},
{"qty":"2","fruit":"banana","price":"50.00"},
{"qty":"3","fruit":"apple","price":"21.00"},
{"qty":"3","fruit":"lemon","price":"31.00"},
{"qty":"3","fruit":"orange","price":"41.00"},
{"qty":"3","fruit":"banana","price":"51.00"},
{"qty":"4","fruit":"apple","price":"22.00"},
{"qty":"4","fruit":"lemon","price":"32.00"},
{"qty":"4","fruit":"orange","price":"42.00"},
{"qty":"4","fruit":"banana","price":"52.00"}
];
function clearResults()
{
$('#try').html('');
}
function addAnotherRow(){
var lastRow = $(".complrow:last");
$(lastRow).before(completerow);
clearResults();
}
$(document).ready(function() {
addAnotherRow();
addAnotherRow();
addAnotherRow();
clearResults();
$("#addrow").click(function(){
addAnotherRow();
});
$('#thisisit').click(function(){
clearResults();
var errorFound = false;
//Loop through rows
var complrowcombination = new Array();
var sumofquantity = 0;
$('.complrow').not(':first').not(':last').each(function (i, row)
{
var numberselected = $(row).find('.firstselectorclass').val();
var fruitselected = $(row).find('.secondselectorclass').val();
var quantity = $(row).find('.actqtyclass').val();
sumofquantity += quantity;
var thiscomplrowCombination = new Array();
thiscomplrowCombination[0] = numberselected;
thiscomplrowCombination[1] = fruitselected;
thiscomplrowCombination[2] = quantity;
//push this each line of array to the overall array
if(quantity > 0)
{
complrowcombination.push(thiscomplrowCombination);
}
});
//Check Overall Sum
if(sumofquantity == 0)
{
alert("You must enter at least one row with quantity greater than 0!");
return;
}
//If no error, get data
if(errorFound)
{
alert("Error found, cannot continue...");
return;
} else
{
$('#try').html('no error found up to this point');
}
});
});