如何使用jquery为表中的序列号列提供连续编号

时间:2012-04-05 10:15:10

标签: javascript jquery html

我有一张桌子。看我的表结构

<table width="100%" border="1">
  <tr>
    <td width="2%"><h2># SL NO</h2></td>
    <td width="70%"><h2>Name</h2></td>
    <td width="15%"><h2>Edit</h2></td>
    <td width="13%"><h2>Delete</h2></td>
  </tr>


  <tr id="id0">
    <td >1</td>
    <td>XXXXXXX</td>
        <td><a href="#"><img src="../images/icon_edit.gif" alt="Edit" /></a></td>
    <td width="13%"><a href="#"><img src="../images/delete_07.png" alt="Delete"  onclick="fnDeleteState(0)" /></a></td>
  </tr>
  <tr id="id1">
    <td>2</td>
     <td>XXXXXXX</td>
    <td><a href="#"><img src="../images/icon_edit.gif" alt="Edit" /></a></td>
    <td%"><a href="#"><img src="../images/delete_07.png" alt="Delete"  onclick="fnDeleteState(1)" /></a></td>
  </tr>

... .... ..       

当按下删除按钮时,我需要隐藏该行,我已使用此行从表中删除特定行

$("#id" + i).remove();

工作正常。但序列号...我显示错误。即当我删除第二行时,第三行中的序列号仍为3我需要显示此s 2,依此类推......

我有任何方法可以在jquery中执行此操作 提前致谢

3 个答案:

答案 0 :(得分:3)

function fnDeleteState(i) {
    $("#id" + i).remove();
    $("tr").each(function (index) { // traverse through all the rows
        if(index != 0) { // if the row is not the heading one
            $(this).find("td:first").html(index + ""); // set the index number in the first 'td' of the row
        }
    });
}

答案 1 :(得分:1)

如果我理解正确,您需要更新每行的第一个单元格,以便显示该行的等级。

您可以通过为每个内容行添加一个类来实现它,例如“contentrow”(以区别于标题行),然后编写如下函数:

function refreshRowIDs(){
    $(".contentrow").each(function(index){
        $(this).children("td:first").html(index);
    });
}

答案 2 :(得分:0)

您需要重新排序序列号。删除后。你可以使用这样的东西:

 function reorder(){
   var i = 1;
   $('#tableid tr').each(function() {
        $(this).find("td:first").text(i);
        i++;
    });
  }