我正在嘗試對除第一列之外的所有列求和。我的 HTML 代碼是;
<div id="TableDetails">
<table id="detailTable" style="width:100%">
<thead>
<tr>
<th>Popname</th>
<th>Expense</th>
<th>Income</th>
<th>Margin</th>
<th>Margin Profit</th>
<th>Time</th>
</tr>
</thead>
<tbody id="details">
</tbody>
<tfoot id="totalValues">
<tr>
<th>Total: </th>
<th id="totalofIncome"></th>
<th id="totalofExpense"></th>
<th id="totalofMargin"></th>
<th id="totalofMarginProfit"></th>
<th id="totalofTime"></th>
</tr>
</tfoot>
</table>
</div>
我在 jquery 中用這樣的 for 回圈填充 tbody
for (var i = 0; i < result.Data.length; i ) {
data = '<tr>'
'<td> ' result.Data[i].Popname '</td>'
'<td> ' parseFloat(result.Data[i].Income.replace(",", ".")) '</td>'
'<td>' parseFloat(result.Data[i].Expense.replace(",", ".")) '</td>'
'<td>' parseFloat(result.Data[i].Profit.replace(",", ".")) '</td>'
'<td>' parseFloat(result.Data[i].ProfitMargin.replace(",", ".")) '</td>'
'<td>' parseFloat(result.Data[i].Time.replace(",", ".")) '</td>'
'</tr>';
}
$("#details").empty().append(data);
我將“,”替換為“。” 總和為 double.Value 總是帶有“x.xx” 我讀了很多主題,看了很多視頻,但無法弄清楚。有什么幫助嗎?
var table = $("#details");
var totalIncome = 0, // etc for the others
$(table).find('td').not(":first").each(function () {
totalIncome = parseFloat(this.val());
})
我想對除第一列 popname 之外的所有列求和
uj5u.com熱心網友回復:
只需在您瀏覽時對您的列求和,然后設定text相關總單元格的 :
const result = {
Data: [{
Popname: "Row1",
Income: "1,00",
Expense: "2,00",
Profit: "3,00",
ProfitMargin: "4,00",
Time: "5,00"
},
{
Popname: "Row2",
Income: "6,00",
Expense: "7,00",
Profit: "8,00",
ProfitMargin: "9,00",
Time: "10,00"
},
]
}
let data = "";
let tIncome = 0;
let tExpense = 0;
let tProfit = 0;
let tProfitMargin = 0;
let tTime = 0;
for (var i = 0; i < result.Data.length; i ) {
const income = parseFloat(result.Data[i].Income.replace(",", "."));
const expense = parseFloat(result.Data[i].Expense.replace(",", "."));
const profit = parseFloat(result.Data[i].Profit.replace(",", "."));
const profitMargin = parseFloat(result.Data[i].ProfitMargin.replace(",", "."));
const time = parseFloat(result.Data[i].Time.replace(",", "."));
data = '<tr>'
'<td> ' result.Data[i].Popname '</td>'
'<td> ' income '</td>'
'<td>' expense '</td>'
'<td>' profit '</td>'
'<td>' profitMargin '</td>'
'<td>' time '</td>'
'</tr>';
tIncome =income;
tExpense = expense;
tProfit = profit
tProfitMargin = profitMargin
tTime = time;
}
$("#details").empty().append(data);
$("#totalofIncome").text(tIncome);
$("#totalofExpense").text(tExpense);
$("#totalofMargin").text(tProfit);
$("#totalofMarginProfit").text(tProfitMargin);
$("#totalofTime").text(tTime);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="TableDetails">
<table id="detailTable" style="width:100%">
<thead>
<tr>
<th>Popname</th>
<th>Expense</th>
<th>Income</th>
<th>Margin</th>
<th>Margin Profit</th>
<th>Time</th>
</tr>
</thead>
<tbody id="details">
</tbody>
<tfoot id="totalValues">
<tr>
<th>Total: </th>
<th id="totalofIncome"></th>
<th id="totalofExpense"></th>
<th id="totalofMargin"></th>
<th id="totalofMarginProfit"></th>
<th id="totalofTime"></th>
</tr>
</tfoot>
</table>
</div>
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/352591.html
標籤:javascript 查询
