Estou carregando uma tabela com o seguinte jquery
function get_sales(customer_id,from_date,to_date){
$.ajax({
type: 'POST',
url: 'ajax/sale_pl.php',
data:{customer_id:customer_id,from_date:from_date,to_date:to_date},
dataType:"json",
success: function(response){ //console.log(response);
for(i=0; i<response.length; i++)
{
$('#tdata').append('<tr>'+
'<td><a href="view_invoice.php?id='+response[i].invoice_id+'">'+response[i].invoice_id+'</a></td>'+
'<td>'+response[i].order_date+'</td>'+
'<td><h6 class="mb-1">'+response[i].product_name+'</h6></td>'+
'<td><h6 class="text-muted">'+response[i].product_code+'</h6></td>'+
'<td>'+response[i].sold_qty+'</td>'+
'<td>'+response[i].buy_price+'</td>'+
'<td>'+response[i].sell_price+'</td>'+
'<td>'+response[i].discount+'</td>'+
'<td>'+response[i].cost_price+'</td>'+
'<td>'+response[i].sold_price+'</td>'+
'<td class="profits">'+response[i].profit+'</td>'
+ '</tr>'
);
}
}
});
}
aqui está o meu código html para a tabela
<table class="table table-hover">
<thead>
<tr>
<th>Invoice id</th>
<th>Invoice Date</th>
<th>Product Name</th>
<th>Product Code</th>
<th>Sale Qty</th>
<th>Buy Price</th>
<th>Sale Price</th>
<th>Discount</th>
<th>Cost Price</th>
<th>Sold Price</th>
<th>Profit</th>
</tr>
</thead>
<tbody id="tdata">
</tbody>
<tfoot>
<tr>
<th colspan='10'>Total Profit</th>
<th id="total_profit">0</th>
</tr>
</tfoot>
</table>
o que estou tentando é obter o total da coluna de lucro. Eu tentei o seguinte
function calcSub(){
var totalProfit= 0;
$(".profits").each(function(){
totalPrice += parseInt($(this).val());
$("#total_profit").html(totalPrice);
});
};
Mas isto não está funcionando.
Por favor, me sugira a solução. Eu sou novo no jQuery. Agradecemos antecipadamente pela ajuda.
Você precisa usar .text () não .val()
. .val()
é usado quando você está obtendo um input
valor. Mas, no seu caso, é simplesmente pegar o .text()
de seus td
itens.
Editar: Uma vez que as funções two
( get_sales
e calcSub()
) estão sendo executadas simultaneamente, você precisa usar a async
função on e esperar que o await
ajax seja concluído success
antes de prosseguir calcSub()
para seus cálculos
Adicione este código ao seu arquivo:
//get sales
async function get_sales(customer_id, from_date, to_date) {
//Await for this to finish and then call total profit
await $.ajax({
type: 'POST',
url: 'color.json',
data: {
customer_id: customer_id,
from_date: from_date,
to_date: to_date
},
dataType: "json",
success: function(response) { //console.log(response);
for (i = 0; i < response.length; i++) {
$('#tdata').append('<tr>' +
'<td><a href="view_invoice.php?id=' + response[i].invoice_id + '">' + response[i].invoice_id + '</a></td>' +
'<td>' + response[i].order_date + '</td>' +
'<td><h6 class="mb-1">' + response[i].product_name + '</h6></td>' +
'<td><h6 class="text-muted">' + response[i].product_code + '</h6></td>' +
'<td>' + response[i].sold_qty + '</td>' +
'<td>' + response[i].buy_price + '</td>' +
'<td>' + response[i].sell_price + '</td>' +
'<td>' + response[i].discount + '</td>' +
'<td>' + response[i].cost_price + '</td>' +
'<td>' + response[i].sold_price + '</td>' +
'<td class="profits">' + response[i].profit + '</td>' +
'</tr>'
);
}
}
});
//call calculate total
calcSub()
}
get_sales();
function calcSub() {
var totalProfit = 0;
$(".profits").each(function() {
totalProfit += parseInt($(this).text());
$("#total_profit").html(totalProfit);
});
};
Demonstração de trabalho completa de acordo com a pergunta
var response = [{
"invoice_id": "Always Helping",
"order_date": "03-02-2002",
"product_name": "Always B +",
"product_code": "[email protected]",
"sold_qty": "2",
"buy_price": "2",
"sell_price": "2",
"discount": "2",
"cost_price": "2",
"sold_price": "2",
"profit": "50",
}, {
"invoice_id": "Blah Blah",
"order_date": "03-02-2002",
"product_name": "Always B +",
"product_code": "[email protected]",
"sold_qty": "2",
"buy_price": "2",
"sell_price": "2",
"discount": "2",
"cost_price": "2",
"sold_price": "2",
"profit": "500",
}]
for (i = 0; i < response.length; i++) {
$('#tdata').append('<tr>' +
'<td><a href="view_invoice.php?id=' + response[i].invoice_id + '">' + response[i].invoice_id + '</a></td>' +
'<td>' + response[i].order_date + '</td>' +
'<td><h6 class="mb-1">' + response[i].product_name + '</h6></td>' +
'<td><h6 class="text-muted">' + response[i].product_code + '</h6></td>' +
'<td>' + response[i].sold_qty + '</td>' +
'<td>' + response[i].buy_price + '</td>' +
'<td>' + response[i].sell_price + '</td>' +
'<td>' + response[i].discount + '</td>' +
'<td>' + response[i].cost_price + '</td>' +
'<td>' + response[i].sold_price + '</td>' +
'<td class="profits">' + response[i].profit + '</td>' +
'</tr>'
);
}
function calcSub() {
var totalProfit = 0;
$(".profits").each(function() {
totalProfit += parseInt($(this).text());
$("#total_profit").html(totalProfit);
});
};
calcSub()
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table class="table table-hover">
<thead>
<tr>
<th>Invoice id</th>
<th>Invoice Date</th>
<th>Product Name</th>
<th>Product Code</th>
<th>Sale Qty</th>
<th>Buy Price</th>
<th>Sale Price</th>
<th>Discount</th>
<th>Cost Price</th>
<th>Sold Price</th>
<th>Profit</th>
</tr>
</thead>
<tbody id="tdata">
</tbody>
<tfoot>
<tr>
<th colspan='10'>Total Profit</th>
<th id="total_profit">0</th>
</tr>
</tfoot>
</table>
Este artigo é coletado da Internet.
Se houver alguma infração, entre em [email protected] Delete.
deixe-me dizer algumas palavras