0

I am pretty new to jquery and fooTable.

What I want to do is to combine the add and delete jquery functions provided by FooTable, such that when I add a row, it automatically deletes the last row in that table.

Would greatly appreciate some help here. Thanks!

//This piece of code deletes a row
$('table').footable().on('click', '.row-delete', function(e) {
  e.preventDefault();

  //get the footable object
  var footable = $('table').data('footable');

  //get the row we are wanting to delete
  var row = $(this).parents('tr:first');

  //delete the row
  footable.removeRow(row);
});

//This piece of code adds a row
$('.add-row').click(function(e) {
  e.preventDefault();

  //get the footable object
  var footable = $('table').data('footable');

  //build up the row we are wanting to add
  var newRow = 'Some content here'

  //add it
  footable.appendRow(newRow);
});
Jason
  • 1
  • 2
  • Do you need to replace an existing row (not necessarily the last row) with a new one? I found a workaround to that problem, but I think it still is an [unsolved issue of Footable](https://github.com/bradvin/FooTable/issues/241). – twigmac Jun 23 '14 at 18:09

2 Answers2

0

This plunk should do what you're asking: http://plnkr.co/edit/PYELgBc0xOpvH4lePXKu

Jeff
  • 2,728
  • 3
  • 24
  • 41
0

i would recommend replace with :

http://api.jquery.com/replacewith/

so suppose your table to be like

<table id="foo">
<tr>
<td>hello</td>
</tr>
</table>
<script>
    $("#add-row").on('click',function(){// add-row is your event button
    $("#foo tr:last-child").replaceWith( "<tr><td>my new content</td></tr>" );
    })
</script>

http://fiddle.jshell.net/wqk87/

ProllyGeek
  • 15,517
  • 9
  • 53
  • 72
  • 1
    According to the Footable documentation, they recommend using the built-in removeRow and appendRow functions. (http://fooplugins.com/footable/demos/add-delete-row.htm) If you don't, you need to manually trigger the Footable redraw. If you don't need to worry about redrawing, than this would be a possible solution. – Jeff Jan 02 '14 at 21:47