admin管理员组

文章数量:1426072

I have integrated add row and delete row functionality in my table. If i add row second row number is also 1. I need to change number of each row as per every add and delete like 1, 2 etc.

I used the code from this fiddle /.

How to achieve this? Any help would be appreciated. Thanks.

I have integrated add row and delete row functionality in my table. If i add row second row number is also 1. I need to change number of each row as per every add and delete like 1, 2 etc.

I used the code from this fiddle http://jsfiddle/MJGV2/6/.

How to achieve this? Any help would be appreciated. Thanks.

Share Improve this question edited Mar 29, 2014 at 12:35 Shaik Riyaz 11.5k7 gold badges55 silver badges72 bronze badges asked Mar 29, 2014 at 12:20 marswebmarsweb 1871 gold badge2 silver badges12 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 7

You can add this:

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

See this Fiddle demo.

There are a lot of errors in your code - for example all id needs to be unique and you should fix this.

For example, you can add call recalculate function, like this:

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

Where numberRow is css class on td, where store number of row.

I add this in your jsfiddle example

There were lot of things missing in your code. i have tuned everything properly

JS CODE:

$("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();
 }
});

LIVE DEMO:

http://jsfiddle/MJGV2/14/

Happy Coding :)

本文标签: javascriptHow to change row number when adddelete rowsStack Overflow