html - 获取动态加载表的列的总数
问题描述
我正在使用以下 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>'
);
}
}
});
}
这是我的表格的 html 代码
<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>
我正在尝试的是获得利润栏的总数。我尝试了以下一个
function calcSub(){
var totalProfit= 0;
$(".profits").each(function(){
totalPrice += parseInt($(this).val());
$("#total_profit").html(totalPrice);
});
};
但这不起作用。
请建议我解决方案。我是 jQuery 的新手。提前感谢您的帮助。
解决方案
您需要使用.text()而不是.text() .val()
。.val()
当你得到一个input
值时使用。但在您的情况下,它只是获取.text()
您的td
物品。
编辑:由于two
(get_sales
和calcSub()
) 函数同时运行,您需要使用async
函数 on 并等待await
for ajax 完成,success
然后再继续calcSub()
进行计算
将此代码添加到您的文件中:
//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);
});
};
根据问题完成工作演示
var response = [{
"invoice_id": "Always Helping",
"order_date": "03-02-2002",
"product_name": "Always B +",
"product_code": "foo@google.com.au",
"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": "foo@google.com.au",
"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>
推荐阅读
- typescript - 初始化前无法访问[变量]
- mysql - 超过 1 个本地数据库 - MySQL
- javascript - 条件渲染 - Vuejs
- javascript - setInterval() 从头再来
- arrays - 为什么管道输出到 /dev/null 和 grep 会污染我的 bash 数组?
- oauth-2.0 - 将使用 Google OAuth2.0 的登录电子邮件限制为特定访问组
- ruby-on-rails - 我可以在 Rails 中进行模型测试后看到寄存器吗?
- node.js - 如何使用 Clarifai 检测本地图像的人脸
- .net - 跨应用如何授权
- postgresql - PostgreSQL WHERE 和 ORDER BY 子句的时间复杂度