tags:

views:

28

answers:

1

I have a repeater which contains a table. I am giving the user the ability to add a new row in the repeater by adding a new row to the table.

Can anyone give me ideas? As I am very new to jQuery, can someone give sample code?

+1  A: 

If you look at the jQuery manipulation methods these are the methods for modifying the DOM in jQuery.

I would suggest for what you are doing something along the lines of:

var tableToUpdate = $('#yourTableId'); // select the table
var rowToAdd = $('<tr></tr>'); // this will create a table row element
rowToAdd.append('<td>some content for this cell</td>'); // add the columns to your new row
tableToUpdate.append(rowToAdd); // append the row to the end of the table

This will insert a new row at the end of the table. If your table has a tbody (you will have to modify your initial selector to '#yourTableId tbody'.

To insert the new row in different positions within the table look though the other manipulation methods - after, before, prepend etc.

Hope this helps, if you are able to be a little more specific about the situation, I can probably give you a more concrete example.

Giles
You have the right idea, but your code will generate something like '<tr></tr><td>some content for this cell</td>'. The '<tr></tr>' needs to be split up as bookends.
Joel Etherton
Hi Joel, Thanks for the comment, but the code does work - it is quite a cool feature of jQuery that I was surprised when I first saw it.If you do $('some html here') it actually converts the string into the appropriate DOM structure. So in my answer above it actually creates a tr element. Then in the next line where you append to it - you are appending to the element, rather than the string.Have just double checked the code in a demo project - and works out of the box. Happy to pass on my test page to you if you want.
Giles