1

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.

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

0

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 :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.