2013-10-20 37 views
0

我對jQuery和fooTable很新穎。FooTable:將添加和刪除行合併爲一個函數

我想要做的是組合添加和刪除FooTable提供的jQuery功能,這樣當我添加一行時,它會自動刪除該表中的最後一行。

非常感謝這裏的一些幫助。謝謝!

//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); 
}); 
+0

您是否需要用新的行替換現有的行(不一定是最後一行)?我找到了解決這個問題的方法,但我認爲它仍然是[Footable]的一個未解決的問題(https://github.com/bradvin/FooTable/issues/241)。 – twigmac

回答

0

我會建議更換:

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

所以假設你的表像

<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/

+1

根據Footable文檔,他們推薦使用內置的removeRow和appendRow函數。 (http://fooplugins.com/footable/demos/add-delete-row.htm)如果你不這樣做,你需要手動觸發Footable重繪。如果你不需要擔心重繪,那麼這將是一個可能的解決方案。 – Jeff

相關問題