javascript - HTML table "Add Row" or "Remove Row" button column - Stack Overflow

I need a way to add or remove a row below another row in my html table.I'm using the dom, and ca

I need a way to add or remove a row below another row in my html table. I'm using the dom, and can add and remove rows easily enough, but the problem es up when I try to have a button column that adds and removes rows dynamically.

For example:

    |  Item Type  |     Item     |  Add/Remove Item
====================================================
    |   Fruit >   |     Apple    |   [ Add new ]
----------------------------------------------------
    |             |     Mango    |   [ Remove ]
----------------------------------------------------
    |             |     Pear     |   [ Remove ]
----------------------------------------------------

The [Add new] and [Remove] entries in the right column are buttons. Clicking a [Remove] button would delete the row (remove the pear or mango row). Clicking the [Add new] button would add a new Item (a new fruit).

If I specified the html for the button and added a new row based on an onClick, I can't think of a way to keep track of which row the button clicked from to add the new one. The best way I can think of is to have an array that keeps track of all the item types and what row they are on, then pass the id or name of the item into the method from onClick(), but this seems error-prone and messy.

To solve this, I'm willing to consider any solution (jquery plugin, javascript code, anything). Can someone point me to the best way to acplish this?

I need a way to add or remove a row below another row in my html table. I'm using the dom, and can add and remove rows easily enough, but the problem es up when I try to have a button column that adds and removes rows dynamically.

For example:

    |  Item Type  |     Item     |  Add/Remove Item
====================================================
    |   Fruit >   |     Apple    |   [ Add new ]
----------------------------------------------------
    |             |     Mango    |   [ Remove ]
----------------------------------------------------
    |             |     Pear     |   [ Remove ]
----------------------------------------------------

The [Add new] and [Remove] entries in the right column are buttons. Clicking a [Remove] button would delete the row (remove the pear or mango row). Clicking the [Add new] button would add a new Item (a new fruit).

If I specified the html for the button and added a new row based on an onClick, I can't think of a way to keep track of which row the button clicked from to add the new one. The best way I can think of is to have an array that keeps track of all the item types and what row they are on, then pass the id or name of the item into the method from onClick(), but this seems error-prone and messy.

To solve this, I'm willing to consider any solution (jquery plugin, javascript code, anything). Can someone point me to the best way to acplish this?

Share Improve this question asked Feb 16, 2012 at 0:56 John LeeheyJohn Leehey 22.2k8 gold badges62 silver badges90 bronze badges 1
  • There's a load of techniques for this, JQuery has a couple of good table plugins; but it might be better to show your work so far. Set up a fiddle here might help: jsfiddle – Russ Clarke Commented Feb 16, 2012 at 0:58
Add a ment  | 

1 Answer 1

Reset to default 3

Like this? I do not know where you are going to take values 'Mango' and 'Pear'.. http://jsfiddle/vPatQ/

$('.AddNew').click(function(){
   var row = $(this).closest('tr').clone();
   row.find('input').val('');
   $(this).closest('tr').after(row);
   $('input[type="button"]', row).removeClass('AddNew')
                                 .addClass('RemoveRow').val('Remove item');
});

$('table').on('click', '.RemoveRow', function(){
  $(this).closest('tr').remove();
});

for html code

<table>
    <tr><td>Item Type</td><td>Item</td><td>Add/Remove Item</td></tr>
    <tr><td><input type='text' value='Fruit >'></td>
        <td><input type='text' value='Apple'></td>
        <td><input type='button' class='AddNew' value='Add new item'></td></tr>
</table>

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744328000a4568746.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信