Numbering dynamic table rows

前端 未结 3 962
失恋的感觉
失恋的感觉 2021-01-28 18:50

I\'m working on making a dynamic HTML table using jQuery. In a table, my user has two interactions:

  1. Append a row
  2. Remove a specific row

The

3条回答
  •  暖寄归人
    2021-01-28 19:40

    I have written a jquery plugin which does exactly this, and you shouldnt need to "number" the rows per-se. All you need to do when deleting a row is to pass the index of the row being deleted.

    eg, if you have a delete button in a row:

    The jQuery might look like

    $('.delete').click(function(){
     var index = $(this).parents('tr').index();
     // tell your plugin to delete row "index"
    });
    

    The method from my plugin which does this looks something like:

    removeRow: function (index) {
            return this.each(function () {
                var $this = $(this);
                var $tbody = $('tbody', $this);
                var $tr = $('tr', $tbody).eq(index);
                $this.trigger('rowRemoved', [$tr]);
                $tr.remove();
            });
        }
    

提交回复
热议问题