I have a data to display in html table. The data comes from the 3rd party server(php) using ajax request. The data are well displayed using this code: HTML:
<table id="tbl-product">
<tr>
<th>product</th>
<th>date</th>
<th>quantity</th>
<th>cost</th>
</tr>
</table>
And the ajax call where in the data will display on success ( success: function(data, status, jqXHR){
):
data.forEach(function(row) {
var productname = row.uproducts_product;
var productico = productname.replace(/\s+/g, "-") + '.png';
var productcost = row.uproducts_total_investment;
var str = '<tr>';
str += '<td>' + '<img class="product_icon" src="images/products/icon-'+ productico +'">' + '<div class="productname">'+ row.uproducts_product + '</div></td>';
str += '<td><div class="textinfo ">' + row.date + '<br/><span class="date">' + row.uproducts_date + '</span></div> </td>';
str += '<td><span class="textinfo">' + row.uproducts_quantity + '</span></td>';
str += '<td><span class="textinfo">' + productcost + '</td>';
str += '</tr>';
$('#tbl-product').append(str);
});
So, the items are displayed like this
product | date | quantity | cost
A | 12-01-2015 | 2 | 2,100
B | 01-04-2016 | 4 | 5,300
But below the product items, I want to add a row which auto compute the total quantity as well as the total cost. Example:
product | date | quantity | cost
A | 12-01-2015 | 2 | 2,100.00
B | 01-04-2016 | 4 | 5,300.00
Total | 6(quantity) | 7,400.00(cost)
Updated1 Alternatives: I created an alternative here, I added a variable for the computed total in the array where the server respond it as json data. So the data now look like this:
[{
"id":"11",
"user_id":"8000",
"product":"Shoes A",
"quantity":"1",
"open":"2015-01-04",
"investment":"3000"
},
{
"id":"12",
"user_id":"8000",
"product":"Shoes B",
"quantity":"1",
"date":"2015-03-01",
"cost":"1500"
},
{
"total_qty":"2"
"total_cost":"4500"
}]
So, If I use this method, is it easier to append it as a row below the product items? How?