简体   繁体   English

删除行后如何对表序列号进行重新排序

[英]How to reorder the table serial no after deleting the row

I have a table with few buttons in each row. 我有一张桌子,每行只有几个按钮。 I have to remove the table row when I click the delete button of each row. 单击每行的删除按钮时,必须删除表行。 My problem here is when I delete the row the SN is not arranged in order. 我的问题是,当我删除未按顺序排列的SN行时。 For Example: if there are 5 rows and once I remove the first row, it will order as 2, 3, 4, 5 instead of 1, 2, 3, 4. 例如:如果有5行,并且一旦我删除了第一行,它将按2、3、4、5(而不是1、2、3、4)顺序排列。

<table>
<thead>
    <tr>
        <th>S.N.</th>
        <th>Name</th>
        <th>Address</th>
        <th>Modified Date</th>
        <th>Action</th>
    </tr>
</thead>
<tbody>
    <?php 
    $sn = 1;
    while($result= mysql_fetch_row($res))
    {

        ?>
        <tr id=<?= $result[0];?>>
            <td><? echo $sn++; ?></td>
            <td><? echo $result[1] ?></td>
            <td><? echo $result[2] ?></td>
            <td><? echo $result[3] ?></td>
            <td>
                <a href="#" id="delete">Delete</a>
                <a href="#" id="edit">Edit</a>
            </td>

        </tr>

        <?php } ?>
    </tbody>
</table>

$('#delete').on('click', function(e) {
    e.preventDefault();
    var id = $(this).closest('tr').attr('id');
    $(this).closest('tr').remove();
});

First of all you should not have same ID. 首先,您不应具有相同的ID。 IDs must be unique. ID必须是唯一的。 you can rather use class and then class selector to target those elements: 您可以先使用class然后再使用class选择器来定位这些元素:

$('.delete').on('click......

For reordering, you can use index of element in .attr callback function to set id based on index: 对于重新排序,可以在.attr回调函数中使用element的索引来基于索引设置id:

$('table tbody tr').attr('id',function(i,o){
  return i++ ;
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM