views:

91

answers:

1

Hello,

I wanted to know how do I go about adding rows after a particular row in my datatable. I'm using the jQuery datatables plug-in for this purpose.

I can pass the table row Index. that I need to insert the row to my javascript function, as follows:

function addNewContact(rCount){ .. }

And my HTML code is:
table id="exampleTbl"
tr
td..../td (data of first column)
td..../td (data of second column)
...
td
input type="button" id="addNewContact<%=rCount %>" name="addNewContact_Details" value="+" onclick="javascript:addNewContact(<%=rCount %>);"
/td
/tr
/table

For the newly added row, I want the first 3 columns as blank, and other columns(5) to contain text boxes.

+3  A: 

Assuming your table looks like this:

<table id="exampleTbl">  
    <tr>
        <td>....</td>
        <td>....</td>
        ...  
        <td>
           <button type="button" class="addRow" />+</button>
       </td>  
    </tr>
</table>

You can add a click handler to the buttons that adds a row like this:

$(function() {
    // HTML template of a row
    var html = '<tr><td></td>...<td><button type="button" class="addRow">+</button></td></tr>';

    $('#exampleTbl').delegate('button.addRow', 'click', function() {
        var row = $(this).closest('tr'); // get the parent row of the clicked button
        $(html).insertAfter(row); // insert content
    });
});

Some notes:

  • The code is executed once the DOM is loaded, due to $(function(){...}).
  • The click event is caught by the table. This ensures that also buttons of the newly inserted rows work.
  • Don't mix styles. If you use jQuery, don't attach click handlers via the onclick attribute in the HTML. It makes your code hard to maintain and you are more flexible doing it the jQuery way.

Hope that helps, if you have questions about this, please comment on this post.

Felix Kling
Hey Felix,Thanks for your reply.I tried that same code that you sent me, but doesn't quite work.I tried to add some alert statements inside the function for executing the onclick event, as follows:$(function() { // HTML of new row var html = '<tr><td> </td> <td> </td> <td> </td> <td> 1 </td> <td> 2 </td> </tr>'; $('#contactInfo').delegate("button.addRow", "click" , function() { alert("button clicked"); var row = $(this).closest(tr); // get parent row of clicked button $(html).insertAfter(row); //add content });});But this doesn't quite work.
Pritish
@Pritish: I have some syntax and quotation errors in my code, I am sorry. I updated it and you can also see a live example here: http://jsfiddle.net/dxPzF/
Felix Kling
Hey Felix,Thanks for your quick reply. One thing I wanted to know, if my table is inside a <div> tag,(say with id exampleDiv) would the above javascript still work?As I can see your code working perfectly, but when I try the same, doesn't quite fire the function() for the on-click event on button.
Pritish
Yes it should work (if you use `#exampleDiv` of course). The events just *bubble up* to the element, where the handler is attached and are processed there. More information can be found [here](http://www.quirksmode.org/js/events_order.html). jQuery just makes use of this. If you attach an event handler via `delegate()` to an element, it just listens for events that bubble up and check the original target.
Felix Kling