locked
Add row only once when found with multiple duplicate values javascript RRS feed

  • Question

  • User411771660 posted

    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.

    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 () {
            if ($('#CollectionTable > tbody > tr').length > 0) {
                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('');
    
                        };
                    }
                }
            }

    How do i make it ask for confirmation only once irrespective of the number of duplicate records.Help please.

    Tuesday, March 13, 2018 12:07 PM

Answers

  • User-707554951 posted

    Hi tarun02

    You could have an flag variable outside your each function.

    Working code as below:

     <style>
            table {
                font-family: arial, sans-serif;
                border-collapse: collapse;
                width: 100%;
            }
    
            td, th {
                border: 1px solid #dddddd;
                text-align: left;
                padding: 8px;
            }
    
            tr:nth-child(even) {
                background-color: #dddddd;
            }
        </style>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    
        <script>
            $(document).ready(function () {
                var accountNumber = 4;
               // 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;
                var flag = true;
                $('#CollectionTable > tbody > tr').each(function () {
                    if ($('#CollectionTable > tbody > tr').length > 0) {
                        var accountNumberInTable = $(this).find('td:eq(3)').text();
                        if (accountNumber == accountNumberInTable) {
                            if (flag == true) {
                                var r = confirm('There is already an entry with the same account number. Proceed anyway?');                        
                                if (r == true) {
                                    flag = false;
                                    //alreadyExists = true;
                                    // addNewRow(newRow);
                                    //calculateTotal();
                                    //clearReadOnlyFields();
                                    //if ($('#AccountNumberTextBox').prop('disabled') == false) {
                                    //    $('#AccountNumberTextBox').focus();
                                    //}
                                    //enableDisableCollector();
    
                                }
                                else {
                                    flag = false;
                                    //alreadyExists = true;
                                    //if ($('#AccountNumberTextBox').prop('disabled') == false) {
                                    //    $('#AccountNumberTextBox').focus();
                                    //    clearReadOnlyFields();
                                    //    $('#AccountNumberTextBox').val('');
    
                                    //};
                                }
                            }
                           
                        }
                    }
                })
            });
        </script>
      <table id="CollectionTable">
                    <thead>
                        <tr>
                            <th>Company</th>
                            <th>Contact</th>
                            <th>Country</th>
                        </tr>
                    </thead>
                    <tr>
                        <td>Alfreds Futterkiste</td>
                        <td>Maria Anders</td>
                        <td>Germany</td>
                        <td>5</td>
                    </tr>
                    <tr>
                        <td>Centro comercial Moctezuma</td>
                        <td>Francisco Chang</td>
                        <td>Mexico</td>
                        <td>4</td>
                    </tr>
                    <tr>
                        <td>Ernst Handel</td>
                        <td>Roland Mendel</td>
                        <td>Austria</td>
                        <td>5</td>
                    </tr>
                    <tr>
                        <td>Island Trading</td>
                        <td>Helen Bennett</td>
                        <td>UK</td>
                        <td>6</td>
                    </tr>
                    <tr>
                        <td>Laughing Bacchus Winecellars</td>
                        <td>Yoshi Tannamuri</td>
                        <td>Canada</td>
                        <td>4</td>
                    </tr>
                    <tr>
                        <td>Magazzini Alimentari Riuniti</td>
                        <td>Giovanni Rovelli</td>
                        <td>Italy</td>
                        <td>4</td>
                    </tr>
    
                </table>

    Output:

    Best regards

    Cathy

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, March 14, 2018 7:27 AM
  • User1644485212 posted

    Add 

    return false;

    in your else block, so it will break your each loop.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, March 20, 2018 10:56 AM

All replies

  • User-707554951 posted

    Hi tarun02

    You could have an flag variable outside your each function.

    Working code as below:

     <style>
            table {
                font-family: arial, sans-serif;
                border-collapse: collapse;
                width: 100%;
            }
    
            td, th {
                border: 1px solid #dddddd;
                text-align: left;
                padding: 8px;
            }
    
            tr:nth-child(even) {
                background-color: #dddddd;
            }
        </style>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    
        <script>
            $(document).ready(function () {
                var accountNumber = 4;
               // 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;
                var flag = true;
                $('#CollectionTable > tbody > tr').each(function () {
                    if ($('#CollectionTable > tbody > tr').length > 0) {
                        var accountNumberInTable = $(this).find('td:eq(3)').text();
                        if (accountNumber == accountNumberInTable) {
                            if (flag == true) {
                                var r = confirm('There is already an entry with the same account number. Proceed anyway?');                        
                                if (r == true) {
                                    flag = false;
                                    //alreadyExists = true;
                                    // addNewRow(newRow);
                                    //calculateTotal();
                                    //clearReadOnlyFields();
                                    //if ($('#AccountNumberTextBox').prop('disabled') == false) {
                                    //    $('#AccountNumberTextBox').focus();
                                    //}
                                    //enableDisableCollector();
    
                                }
                                else {
                                    flag = false;
                                    //alreadyExists = true;
                                    //if ($('#AccountNumberTextBox').prop('disabled') == false) {
                                    //    $('#AccountNumberTextBox').focus();
                                    //    clearReadOnlyFields();
                                    //    $('#AccountNumberTextBox').val('');
    
                                    //};
                                }
                            }
                           
                        }
                    }
                })
            });
        </script>
      <table id="CollectionTable">
                    <thead>
                        <tr>
                            <th>Company</th>
                            <th>Contact</th>
                            <th>Country</th>
                        </tr>
                    </thead>
                    <tr>
                        <td>Alfreds Futterkiste</td>
                        <td>Maria Anders</td>
                        <td>Germany</td>
                        <td>5</td>
                    </tr>
                    <tr>
                        <td>Centro comercial Moctezuma</td>
                        <td>Francisco Chang</td>
                        <td>Mexico</td>
                        <td>4</td>
                    </tr>
                    <tr>
                        <td>Ernst Handel</td>
                        <td>Roland Mendel</td>
                        <td>Austria</td>
                        <td>5</td>
                    </tr>
                    <tr>
                        <td>Island Trading</td>
                        <td>Helen Bennett</td>
                        <td>UK</td>
                        <td>6</td>
                    </tr>
                    <tr>
                        <td>Laughing Bacchus Winecellars</td>
                        <td>Yoshi Tannamuri</td>
                        <td>Canada</td>
                        <td>4</td>
                    </tr>
                    <tr>
                        <td>Magazzini Alimentari Riuniti</td>
                        <td>Giovanni Rovelli</td>
                        <td>Italy</td>
                        <td>4</td>
                    </tr>
    
                </table>

    Output:

    Best regards

    Cathy

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, March 14, 2018 7:27 AM
  • User1644485212 posted

    Add 

    return false;

    in your else block, so it will break your each loop.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, March 20, 2018 10:56 AM