如何改变行号时,添加删除行

How to change row number when adddelete rows

本文关键字:添加 删除行 何改变 改变      更新时间:2023-09-26

我在表中集成了添加行和删除行功能。如果我加上第二行,行号也是1。我需要根据每次添加和删除(如1、2等)来更改number of each row

我使用了这个小提琴http://jsfiddle.net/MJGV2/6/中的代码。

如何实现这一点?任何帮助都会很感激。谢谢。

你可以添加:

var renum = 1;
$("tr td strong").each(function() {
    $(this).text(renum);
    renum++;
});

查看这个demo

你的代码中有很多错误-例如所有的id都需要是唯一的,你应该修复这个

例如,可以添加call recalculate函数,如下所示:

function recalculate() {
    var i = 1;
    $(".numberRow").each(function() {
        $(this).find("strong").text(i++);     
    });
}

其中numberRow为td上的css类,其中存储行数。

我将它添加到您的jsfiddle示例

你的代码中缺少了很多东西。我已经调好了

JS代码:

$("input[type='button'].AddRow").on('click',
function () {
 if ($(this).val() == 'Delete') {
    trSet = $(this).closest('tr').nextAll();
    currIndex = $(this).closest('tr').find('td span').html();
    $(this).closest('tr').remove();
    trSet.each(function () {
        $(this).find('td span').html(currIndex);
        currIndex++;
    });
    count = currIndex - 1;
 } else {
    var $tr = $(this).closest('tr').clone(true);
    var $input = $tr.find('input.startdatum');
    ++count;
    $tr.find('td span').html(count);
    $(this).val('Delete');
    var id = 'datepicker' + count;
    $input.attr('id', id).data('index', count);
    console.log(count);
    $tr.appendTo($(this).closest('table'));
    setdatepicker();
 }
});

现场演示:

http://jsfiddle.net/MJGV2/14/

Happy Coding:)