Build table using partial view with ajax requests

I need to dynamically create (insert) a new table row every time the user clicks a button (using ajax). My partial view has the following structure:

    <tr>
         <td><td>
         <td><td>
         ...
    </tr>

      

When I need to insert a new line at the end, I can do something like this:

    element_table.innerHTML += ajax_data

      

But what should I do when I need to put it between other lines? I can only return [td] elements and wrap them in a [tr] client-generated element (tr.innerHTML = ajax_data). But I don't think that's a good idea. Any ideas? Are there any common practices?

+3


source to share


1 answer


The easiest way is to use jQuery with an Ajax response. It can be as easy as

$('#table').append(response) 

      

add a line . It is also possible to insert a new line at a specific index :



$('#my_table > tbody > tr').eq(index).after(response);

      

Note that the index is 0-based.

+2


source







All Articles