Click here to Skip to main content
15,884,473 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am using Input Picker plugin and I am stuck at some points, here is my HTML code:
<table class="table table-bordered table-hover table-striped table-sm" id="myTable">            <thead><th></th><th>#</th><th>test1</th><th>test2</th><th>test3</th><th>test4</th><th>test5</th></thead>            <tbody>
            <tr>
      <td>
          <a href="#" class="btn btn-sm btn-danger delete-row">delete</a>
      </td>
                <td>
        1
                </td>
      <td>
        <div class="form-group">

          <input class="form-control form-control-sm" id="items" name="items">

        </div>
      </td>
                <td width="150">
                    <div class="form-group">
          <select class="form-select form-select-sm" id="uom" name="uom">
          </select>
                    </div>

</td>
                    </tr>
                </tbody>
            </table>
<a href="#" class="btn btn-sm btn-primary add-row">add</a>


and here is my JavaScript/JQuery code:

<script>
   var cloned = $('#myTable tr:last').clone();
   $(".add-row").click(function() {
     $("td:eq(1)", cloned).html($("tbody tr").length + 1);
       cloned.clone().appendTo('#myTable');
       createInputPicker();
   });

   function renumberTable(table){
     var count = 1;
     $("tbody tr", table).each(function(i, row){
       $("td:eq(1)", row).html(count++);
     });
   }

   $('#myTable').on('click', ".delete-row", function() {
       $(this).closest('tr').remove();
       renumberTable($("#myTable"));
   });

   $(document).ready(function () {
     createInputPicker();
     });


 function createInputPicker(){
        $('input#items').inputpicker({
         data:[
             {value:"2",text:"test1", description: "hello"},
             {value:"3",text:"test2", description: "simple"},
             {value:"5",text:"test3", description: "This is the description of the text 2."}
         ],
         fields:['value','text','description'],
         fieldText : 'text',
         headShow: true,
         filterOpen: true,
         autoOpen: true
      });

 }
 </script>

The job of this code
JavaScript
$("td:eq(1)", cloned).html($("tbody tr").length + 1);<pre>
is to print/display the number of the rows in the table and it is working fine if I am not choosing an option from input picker, but when I choose an option from the input picker the 2nd row number count will be 5 not 2. How to fix this issue?

What I have tried:

I think this code
$("td:eq(1)", cloned).html($("tbody tr").length + 1);
is conflicting with the plugin... I am not sure...
Posted
Updated 14-Feb-22 11:13am
v4

1 solution

The query $("tbody tr") returns every row from every table in the document.

If your plugin creates another table anywhere in your document, then its rows will be included.

Assuming you want to count the rows in a specific table, the limit your query to that specific table:
JavaScript
$("#myTable tbody tr")
 
Share this answer
 
Comments
FRS4002 14-Feb-22 13:38pm    
@Richard Deeming Oh my god! It fixed the issue! I literally asked a lot of people about it no one know the solution. Thank you so much! You deserve 100 stars!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900