gpt4 book ai didi

javascript - 使用 Jquery 删除新扩展的行时自动计算表总计

转载 作者:行者123 更新时间:2023-12-03 00:59:20 25 4
gpt4 key购买 nike

所以我的代码片段给了我这个错误:this.closest不是一个函数但在我的页面中一切正常,所以让我们忘记这部分。

我的问题是我有一个表,当用户输入一些值时可以自动计算列和行,引用此 https://stackoverflow.com/a/52642412/8826120评论。现在我正在添加更多按钮,所有功能都工作正常(添加新的输入值),但是当我删除新行时,它无法自动计算总值。

How can I make my total line auto-update when the newly expanded row is removed?

您可以从下面检查它,但正如我之前所说,仅在此页面上,我遇到了错误:this.closest 不是函数。提前致谢

注意:我的函数是这样的 Remove table rows updating total data using jQuery

$(document).on('input change', '.outstanding, .received, .paid', updateTable);

function updateTable() {
updateRow($(this).closest("tr"));
updateCol($(this).closest("td"), $(this));
updateTotal($(this.closest("table")));
}

function updateRow($row) {
var sum = 0,
sum2 = 0,
sum3 = 0;
$row.find('.outstanding, .received, .paid').each(function(i) {
if (!isNaN(this.value) && this.value.length != 0) {
if ($(this).hasClass('outstanding')) {
out = $(this).val();
sum += parseFloat(this.value);
} else if ($(this).hasClass('received')) {
reci = $(this).val();
sum2 += parseFloat(this.value);
} else if ($(this).hasClass('paid')) {
paid = $(this).val()
sum3 += parseFloat(this.value);
}
}
});
$row.find('.amtOutstanding').val(sum + sum2 + sum3);
}

function updateCol($col, $input) {
var index = $col.index() + 1;
var sum = 0;
$col.closest('table').find('td:nth-child(' + index + ')').find('input').each(function(i) {
if (!isNaN(this.value) && this.value.length != 0 && !$(this).attr('id')) {
sum += parseFloat(this.value);
}
});

if ($input.hasClass('outstanding')) {
$('#sch26_outstanding').val(sum.toFixed(2));
} else if ($input.hasClass('received')) {
$('#sch26_received').val(sum.toFixed(2));
} else if ($input.hasClass('paid')) {
$('#sch26_paid').val(sum.toFixed(2));
} else if ($input.hasClass('amtOutstanding')) {
$('#sch26_amtOutstanding').val(sum.toFixed(2));
}

}

function updateSchedule26() {
var sum = 0,
sum2 = 0,
sum3 = 0,
out, reci, paid;
$('.outstanding, .received, .paid').each(function(i) {
if (!isNaN(this.value) && this.value.length != 0) {
if ($(this).hasClass('outstanding')) {
sum += parseFloat(this.value);
} else if ($(this).hasClass('received')) {
sum2 += parseFloat(this.value);
} else if ($(this).hasClass('paid')) {
sum3 += parseFloat(this.value);
}
}
});
var total = (parseInt(out) + parseInt(reci)) + parseInt(paid);
$(".amtOutstanding").val(parseFloat(total).toFixed(2));
$('#sch26_outstanding').val(sum.toFixed(2));
$('#sch26_received').val(sum2.toFixed(2));
$('#sch26_paid').val(sum3.toFixed(2));
}

function updateTotal($table) {
var sum = 0;
$table.find('.amtOutstanding').each(function(i) {
if (!isNaN(this.value) && this.value.length != 0) {
sum += parseFloat(this.value);
}
});
$('#sch26_amtOutstanding').val(sum.toFixed(2))
}

function addMoreDepIT() {
var new_raw = $(
'<tr>'+
'<td><a href="javascript:void(0);" class="remove">Remove</td>'+
'<td><input type="number" min="0" name="" id="" class="form-control outstanding"></td>'+
'<td><input type="number" min="0" name="" id="" class="form-control received"></td>'+
'<td><input type="number" min="0" name="" id="" class="form-control paid"></td>'+
'<td><input type="number" min="0" name="" id="" class="form-control amtOutstanding" readonly></td>'+
'</tr>'
);
new_raw.insertBefore('#addMore');
$("#dep_it_table").on('click', '.remove', function() {
$(this).closest('tr').remove();
updateTable();
});
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"></script>


<table class="table table-sm" id="dep_it_table">
<thead>
<tr>
<th style="width:16.67%">Name</th>
<th style="width:16.67%">Outstanding</th>
<th style="width:16.67%">Received</th>
<th style="width:16.67%">Paid</th>
<th style="width:16.67%">Sub Total</th>
</tr>
</thead>
<tbody>

<tr>
<td>Name 1</td>
<td><input type="number" name="" id="" class="form-control outstanding"></td>
<td><input type="number" name="" id="" class="form-control received"></td>
<td><input type="number" name="" id="" class="form-control paid"></td>
<td><input type="number" name="" id="" class="form-control amtOutstanding" readonly></td>
</tr>
<tr>
<td>Name 2</td>
<td><input type="number" name="" id="" class="form-control outstanding"></td>
<td><input type="number" name="" id="" class="form-control received"></td>
<td><input type="number" name="" id="" class="form-control paid"></td>
<td><input type="number" name="" id="" class="form-control amtOutstanding" readonly></td>
</tr>

<tr id="addMore">
<td><a href="javascript:void(0)" class="btn btn-primary" style="padding:0px;" onclick="addMoreDepIT()"><i class="ft-plus hidden-lg-up"></i>&nbsp;Add More</a></td>
<td></td><td></td><td></td><td></td>
</tr>

<tr>
<td>Add Total</td>
<td><input type="number" name="" id="sch26_outstanding" class="form-control total_sum" readonly></td>
<td><input type="number" name="" id="sch26_received" class="form-control total_sum" readonly></td>
<td><input type="number" name="" id="sch26_paid" class="form-control total_sum" readonly></td>
<td><input type="number" name="" id="sch26_amtOutstanding" class="form-control" readonly></td>
</tr>
</tbody>
</table>

最佳答案

首先,您需要将已删除行的所有输入设置为 0,然后重新计算每个列和行总计,然后删除该行:

$("#dep_it_table").on('click', '.remove', function() {
var row = $(this).closest('tr');
row.find(".outstanding, .received, .paid").each(function(){
$(this).val(0)
$(this).change();
});
row.remove();
});

$(document).on('input change', '.outstanding, .received, .paid', updateTable);

function updateTable() {
updateRow($(this).closest("tr"));
updateCol($(this).closest("td"), $(this));
updateTotal($(this.closest("table")));
}

function updateRow($row) {
var sum = 0,
sum2 = 0,
sum3 = 0;
$row.find('.outstanding, .received, .paid').each(function(i) {
if (!isNaN(this.value) && this.value.length != 0) {
if ($(this).hasClass('outstanding')) {
out = $(this).val();
sum += parseFloat(this.value);
} else if ($(this).hasClass('received')) {
reci = $(this).val();
sum2 += parseFloat(this.value);
} else if ($(this).hasClass('paid')) {
paid = $(this).val()
sum3 += parseFloat(this.value);
}
}
});
$row.find('.amtOutstanding').val(sum + sum2 + sum3);
}

function updateCol($col, $input) {
var index = $col.index() + 1;
var sum = 0;
$col.closest('table').find('td:nth-child(' + index + ')').find('input').each(function(i) {
if (!isNaN(this.value) && this.value.length != 0 && !$(this).attr('id')) {
sum += parseFloat(this.value);
}
});

if ($input.hasClass('outstanding')) {
$('#sch26_outstanding').val(sum.toFixed(2));
} else if ($input.hasClass('received')) {
$('#sch26_received').val(sum.toFixed(2));
} else if ($input.hasClass('paid')) {
$('#sch26_paid').val(sum.toFixed(2));
} else if ($input.hasClass('amtOutstanding')) {
$('#sch26_amtOutstanding').val(sum.toFixed(2));
}

}

function updateSchedule26() {
var sum = 0,
sum2 = 0,
sum3 = 0,
out, reci, paid;
$('.outstanding, .received, .paid').each(function(i) {
if (!isNaN(this.value) && this.value.length != 0) {
if ($(this).hasClass('outstanding')) {
sum += parseFloat(this.value);
} else if ($(this).hasClass('received')) {
sum2 += parseFloat(this.value);
} else if ($(this).hasClass('paid')) {
sum3 += parseFloat(this.value);
}
}
});
var total = (parseInt(out) + parseInt(reci)) + parseInt(paid);
$(".amtOutstanding").val(parseFloat(total).toFixed(2));
$('#sch26_outstanding').val(sum.toFixed(2));
$('#sch26_received').val(sum2.toFixed(2));
$('#sch26_paid').val(sum3.toFixed(2));
}

function updateTotal($table) {
var sum = 0;
$table.find('.amtOutstanding').each(function(i) {
if (!isNaN(this.value) && this.value.length != 0) {
sum += parseFloat(this.value);
}
});
$('#sch26_amtOutstanding').val(sum.toFixed(2))
}

function addMoreDepIT() {
var new_raw = $(
'<tr>'+
'<td><a href="javascript:void(0);" class="remove">Remove</td>'+
'<td><input type="number" min="0" name="" id="" class="form-control outstanding"></td>'+
'<td><input type="number" min="0" name="" id="" class="form-control received"></td>'+
'<td><input type="number" min="0" name="" id="" class="form-control paid"></td>'+
'<td><input type="number" min="0" name="" id="" class="form-control amtOutstanding" readonly></td>'+
'</tr>'
);
new_raw.insertBefore('#addMore');
$("#dep_it_table").on('click', '.remove', function() {
var row = $(this).closest('tr');
row.find(".outstanding, .received, .paid").each(function(){
$(this).val(0)
$(this).change();
});
row.remove();
});
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"></script>


<table class="table table-sm" id="dep_it_table">
<thead>
<tr>
<th style="width:16.67%">Name</th>
<th style="width:16.67%">Outstanding</th>
<th style="width:16.67%">Received</th>
<th style="width:16.67%">Paid</th>
<th style="width:16.67%">Sub Total</th>
</tr>
</thead>
<tbody>

<tr>
<td>Name 1</td>
<td><input type="number" name="" id="" class="form-control outstanding"></td>
<td><input type="number" name="" id="" class="form-control received"></td>
<td><input type="number" name="" id="" class="form-control paid"></td>
<td><input type="number" name="" id="" class="form-control amtOutstanding" readonly></td>
</tr>
<tr>
<td>Name 2</td>
<td><input type="number" name="" id="" class="form-control outstanding"></td>
<td><input type="number" name="" id="" class="form-control received"></td>
<td><input type="number" name="" id="" class="form-control paid"></td>
<td><input type="number" name="" id="" class="form-control amtOutstanding" readonly></td>
</tr>

<tr id="addMore">
<td><a href="javascript:void(0)" class="btn btn-primary" style="padding:0px;" onclick="addMoreDepIT()"><i class="ft-plus hidden-lg-up"></i>&nbsp;Add More</a></td>
<td></td><td></td><td></td><td></td>
</tr>

<tr>
<td>Add Total</td>
<td><input type="number" name="" id="sch26_outstanding" class="form-control total_sum" readonly></td>
<td><input type="number" name="" id="sch26_received" class="form-control total_sum" readonly></td>
<td><input type="number" name="" id="sch26_paid" class="form-control total_sum" readonly></td>
<td><input type="number" name="" id="sch26_amtOutstanding" class="form-control" readonly></td>
</tr>
</tbody>
</table>

关于javascript - 使用 Jquery 删除新扩展的行时自动计算表总计,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52699773/

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