gpt4 book ai didi

javascript - 将两列与数据表相乘

转载 作者:行者123 更新时间:2023-11-30 13:56:54 26 4
gpt4 key购买 nike

我想将两列与 相乘.我的 table 是这样的:

<table border="5" class="teste1" method="POST" id="table">
<thead>
<tr>
<th class="filter" style="width:42px; text-align: center; font-size: 12px">Quantidade</th>
<th class="filter" style="width:25px; text-align: center; font-size: 12px">Preço</th>
</tr>
</thead>
<tbody>
<tr>
<?php
while($rows_cursos = mysqli_fetch_array($resultado_cursos)) {
?>
<td style="font-size: 12px"> <?php echo $rows_cursos['Quantidade']; ?></td>
<td style="font-size: 12px"> <?php echo $rows_cursos['Preco']; ?></td>
</tr>
<?php } ?>
</tbody>';
<?php
}
?>
</table>

然后我尝试乘以 quantity * price 的列如下:

(function ($) {$(document).ready(function () {
$('#table').dataTable({
drawCallback: function () {
var api = this.api();
$( api.table().footer() ).html(
api.column( 1 * 2 ).data().sum()
);
}
});
})(jQuery)

但它不起作用,我想在表格底部的 <tfoot> </ tfoot> 中连续显示数据 table 。我希望这个乘法的总和始终根据过滤器返回结果。

最佳答案

我假设,您的 HTML 是由 PHP 脚本准备的(在我看来,这不是最佳可用选项)。

但是,如果您的 <tfoot> 中有某个节点保留显示总成本(例如 span#sumproduct ),您可以:

//source data
const srcData = [['apple',5,3],['pear',4,2],['banana',3,1]]

//datatables initialization
$('#example').DataTable({
dom: 'ft',
data: srcData,
columns: ['item', 'qty', 'price'].map(title => ({title})),
footerCallback: function(){
const sumProduct = this
.api()
.rows({search:'applied'})
.data()
.toArray()
.reduce((res,[item, qty, price]) => res += qty*price, 0);
$('#sumproduct').text(sumProduct);
}
})
<!doctype html><html><head><link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/v/dt/jq-3.3.1/dt-1.10.18/rg-1.1.0/datatables.min.css" /><script type="application/javascript" src="https://code.jquery.com/jquery-3.3.1.min.js"></script><script type="text/javascript" src="https://cdn.datatables.net/v/dt/jq-3.3.1/dt-1.10.18/rg-1.1.0/datatables.min.js"></script></head><body><table id="example"><tfoot><tr><td colspan="3">Overall cost is: <span id="sumproduct"></span></td></tr></tfoot></table></body></html>

关于javascript - 将两列与数据表相乘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57092397/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com