marsweb
marsweb

Reputation: 187

How to change row number when add\delete rows

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.net/MJGV2/6/.

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

Upvotes: 1

Views: 3086

Answers (3)

Beauvais
Beauvais

Reputation: 2279

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.

Upvotes: 7

dreamweiver
dreamweiver

Reputation: 6002

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.net/MJGV2/14/

Happy Coding :)

Upvotes: 0

Boris Parfenenkov
Boris Parfenenkov

Reputation: 3279

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

Upvotes: 0

Related Questions