How to add row only once when found with multiple duplicate values.Here it adds rows according to the number of accountNumber == accountNumberInTable found.I add a new record with say NS-01 and when i add another entry with the same record NS-01 it asks for one confirmation. Again if i add the record with the same value of NS-01, it asks the confirmation twice since there are two rows with the same record.
$('#AddNewRowButton').click(function () {
debugger;
var index = $('#CollectionTable tbody tr').length;
var imageButton = "<button type='button' style='background-color:transparent; margin-top:-4px;' class='btn btn-flat' id='DeleteImageButton' onmouseover=this.style.cursor='pointer'><i style='font-size:11px;' class='glyphicon glyphicon-trash'></i></button>";
var statementReference = $('#StatementReferenceTextBox').val();
var accountNumberId = $("#AccountNumberIdHiddenField").val();
var accountNumber = $('#AccountNumberTextBox').val();
var customerId = $('#CustomerIdTextBox').val();
var name = $('#CustomerNameTextBox').val();
var unit = $('#UnitTextBox').val();
var collectorName = $('#CollectorsSelect :selected').text();;
var productName = $('#ProductTextBox').val();
var amount = parseFloat($('#AmountTextBox').val());
if (isNaN(amount)) {
amount = 0;
}
var amountTextBox = "<input type='text' id='TableAmountTextBox'" + "value=" + amount + " style='text-align:right;' />";
if (amount == '' || accountNumber == '' || customerId == '') {
$('#DialogDiv').empty();
$('#DialogDiv').append('Cannot add empty values');
$('#DialogDiv').slideDown(200);
return;
}
var newRow = "<tr><td>" + imageButton + "</td><td>" + (index + 1) + "</td><td>" + statementReference +
"</td><td style='display:none;'>" + accountNumberId + "</td><td>" + accountNumber + "</td><td>" + customerId + "</td><td>" + name + "</td><td>" + unit + "</td><td>" + collectorName + "</td><td>" + productName + "</td><td>" + amountTextBox + "</td></tr>";
var alreadyExists = false;
var i = collectionSheetDetails.length;
$('#CollectionTable > tbody > tr').each(function () {
var accountNumberInTable = $(this).find('td:eq(4)').text();
if (accountNumber == accountNumberInTable) {
var r = confirm('There is already an entry with the same account number. Proceed anyway?');
if (r == true) {
alreadyExists = true;
addNewRow(newRow);
calculateTotal();
clearReadOnlyFields();
if ($('#AccountNumberTextBox').prop('disabled') == false) {
$('#AccountNumberTextBox').focus();
}
enableDisableCollector();
} else {
alreadyExists = true;
if ($('#AccountNumberTextBox').prop('disabled') == false) {
$('#AccountNumberTextBox').focus();
clearReadOnlyFields();
$('#AccountNumberTextBox').val('');
};
}
}
});
if (!alreadyExists) {
addNewRow(newRow);
calculateTotal();
clearReadOnlyFields();
if ($('#AccountNumberTextBox').prop('disabled') == false) {
$('#AccountNumberTextBox').focus();
}
enableDisableCollector();
}
if ($('#NumberTextBox').prop('disabled') == false) {
$('#NumberTextBox').focus();
}
if ($('#AccountNumberTextBox').prop('disabled') != true) {
$('#AccountNumberTextBox').focus();
}
var colDiv = document.getElementById("CollectionSheetTable");
console.log(colDiv);
colDiv.scrollTop = colDiv.scrollHeight;
//$("#CollectionTable").animate({ scrollTop: $(document).height() }, "slow");
return false;
});
How do i make it ask for confirmation only once irrespective of the number of duplicate records.Help please.
eachloop. Also, no need for thisif ($('#CollectionTable > tbody > tr').length > 0)inside your loop, if that condition is not true, it won't even go into the loop{ "message": "ReferenceError: $ is not defined", "filename": "https://stacksnippets.net/js", "lineno": 13, "colno": 9 }. Well I think it's the fact that you have not included your jQuery file in the snippet, hence"ReferenceError: $ is not defined", check that out