0

I've a table like this:

<table class="table table-striped table-bordered table-hover table-condensed" id="table">
        <tr>
            <td>rowNumber</td>
            <td>Product Name</td>
            <td>Price</td>
        </tr>
        <tr>
            <td>1</td>
            <td>item1</td>
            <td>250000</td>
        </tr>
        <tr>
            <td>2</td>
            <td>item2</td>
            <td>250000</td>
        </tr>
    </table>

I also adding new row this way (data will be adding to table when new record added):

if ($('#table').length) {
     $('#table tr:first').after("<tr>" +
                            "<td>" + ? + "</td>" +
                            "<td>" + data.Title + "</td>" +
                            "<td>" + data.Price + "</td>" +
                            "</tr>");
}

As you can see, I add new row to the first row of the table. Now I want to add new row with rowNumber 1 then all other rowNumber get update.
Any idea?

1
  • 2
    Loop through and set the html of the cells with the new value. Commented Jan 27, 2015 at 17:34

1 Answer 1

2

Once you have appended the new row, you can set the rowNumber going through all rows (except the first one):

if ($('#table').length) {
    $('#table tr:first').after("<tr>" +
        "<td></td>" +
        "<td>" + data.Title + "</td>" +
        "<td>" + data.Price + "</td>" +
        "</tr>");

    $("#table tr:not(:first-child) td:first-child").each(function(index,item){
        $(this).text(index+1);
    });
}

Fiddle

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

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.