Tasneem Fathima
Tasneem Fathima

Reputation: 159

auto increment the serial number when row increases and automatically readjust the numbering order when a row gets deleted in jquery

I want the serial no: to increase whenever a row is inserted dynamically. and when any row is deleted whether in the beginning or in the end or in-between, the numbers should rearrange themselves in proper order. I'm a fresher and could not get a logic on how to do it. Can anyone pls guide me on the same. Thanks in advance for the support.

I have given my sample coding below. On clicking the "(+)" button, the row increases automatically. when that is happening, i want the serial no also to increment automatically. And when any row is deleted, (even in middle of the table), then the numbers should automatically adjust themselves in proper order. Pls guide me on the same.

<html>
<head>
<script type="text/javascript" src="jquery-1.7.2.min.js"></script>
<script type="text/javascript">
$(document).ready( function() {
id=1;
var serial =1;

$('#butVal').click(function(){

        var master = $(this).parents("#table-2");
        id++;
        var sample = master.find('.tabRow').clone();
        sample.attr("id", id + "item"); 
        master.find('.tabRow').removeClass('tabRow');
        master.find('tbody').append(sample);
        //alert(master);
        //alert(sample);
        //alert(sample.html());
      //alert(master.html());





var rowLen =  $('#table-2 > tbody >tr').length;

//alert(rowLen);
if(rowLen>9)
{
alert("no of row is reached 10");
}


   }

);

$('table#table-2 button.remove').live('click', function(){


 if($("table#table-2 tbody tr").length > 1)
  {
     if($("table#table-2 tbody tr").index($(this).parents('tr')) ==$("table#table-2 tbody tr").length -1)
     {
       $(this).parents('tr').prev().addClass("tabRow");
     }
     $(this).parents('tr').remove();
  }
 else
 {
    alert("you can.t remove this record");
 }



} );


});


//jquery ends here

</script>

<style type="text/css">
.total
{
border:1px solid black;
width:90%;
height:1250px;
margin-left:auto;
margin-right:auto;
}
.add
{
width:100%; 
}
.add select,input
{
width:100%;
}
</style>
</head> 
<body>

<div class="total">

<table id="table-2" class="add" border ="1">

<thead>

<tr><th class="small"> S.No</th><th> Product Status </th> <th> Stock Status</th> <th class="sizing"> Description</th> <th> Quantity </th> <th> Price </th> <th>  Total </th >
<th> <button id="butVal"> + </button></th></tr> 
</thead>

<tbody>
<tr class="tabRow" id="1item">
<td class="sno">  </td>
<td><select><option> New </option><option> Old </option></select> </td>
<td><select><option> In Stock </option><option> Out Of Stock </option></select> </td>
<td> <input type="text" name="desc"/>  </td>
<td> <input type="text" name="qty"/> </td>
<td> <input type="text" name="price"/> </td>
<td> <input type="text" name="total"/> </td>
<td><button class="remove">Remove</button></td>
</tr>
</tbody>

<tfoot>
<tr>

<td>&nbsp;  </td>
<td>&nbsp;  </td>
<td>&nbsp;  </td>
<td>&nbsp;  </td>
<td>&nbsp;  </td>
<td> Grand Total </td>
<td>&nbsp;  </td>
<td>  <button id="proceed" onClick="payment();"> Proceed </button>   </td>


</tr>
</tfoot>

</table>

</div>
</body>






</html>

Upvotes: 0

Views: 10992

Answers (1)

Dom
Dom

Reputation: 40481

I made some changes to your code at jsfiddle.net. The link is: http://jsfiddle.net/2nzgG/30/. All my comments can be found in the javascript section. I hope this is what you are looking for. Let me know if you need anything else.

EDIT:

I also added the code below, but the comments will be found in the link...

$(document).ready( function() {

$('#butVal').click(function(){
   var rowLen =  $('tr.tabRow').length;

    if(rowLen>9)
    {
          alert("no of row is reached 10");
    }
    else
    {
   $("tr.tabRow:first").clone(true).appendTo("#table-2>tbody");  
    $(".tabRow:last").children("td").children("input").each(function(index, element){
        $(element).val("");
    });
    }
    });


$(document).on("click", "button.remove", function(){

 if($(this).parents("tr").siblings("tr.tabRow").length > 0)
  {
     $(this).closest("tr.tabRow").remove();        
  }
 else
 {
    alert("you can.t remove this record");
 }
});


//FOR Serial Number- use ON 
    $(document).on("click", ".add, .remove", function(){

       $("td.sno").each(function(index,element){                 
           $(element).text(index + 1); 
        });
    });
});

Upvotes: 5

Related Questions