Jquery 删除数据表中的行
我有一个使用datatable插件的表。我正在尝试使用以下方法动态删除其中的一行:Jquery 删除数据表中的行,jquery,database,datatable,Jquery,Database,Datatable,我有一个使用datatable插件的表。我正在尝试使用以下方法动态删除其中的一行: $(document).ready(function() { var oTable = $("table#demo-dtable-02").dataTable({"aaSorting": []}); $(".icon-remove").on('click',function(){ var anSelected = fnGetSelected( oTable );
$(document).ready(function() {
var oTable = $("table#demo-dtable-02").dataTable({"aaSorting": []});
$(".icon-remove").on('click',function(){
var anSelected = fnGetSelected( oTable );
deleted=oTable.fnDeleteRow(anSelected[0] );
});
});
我得到以下错误引用错误:未定义fnGetSelected
我试过使用(从这里:)
这当然会删除行,但不会像这样重置页脚中的文本
显示17个条目中的1到10个条目。
以这种方式删除并不是最好的解决方案
我的DOM代码如下所示:
<?PHP
$count=1;
foreach($ledgers as $row) {?>
<tr >
<td><a onClick="viewDetails(<?PHP echo $row['id'];?>)" style="cursor:pointer"><?PHP echo $row['name'];?></a></td>
<td><?PHP echo $row['email'];?></td>
<td><?PHP echo $row['contact_number'];?></td>
<td>
<i class="icon-remove" rowcount=<?PHP echo $count;?> style="cursor:pointer; margin-right:5px" title="Remove Ledger" id="removeLedgerNow"></i>
<i class="icon-edit" style="cursor:pointer" title="Edit Ledger" onclick="viewDetails(<?PHP echo $row['id'];?>)"></i>
</td>
</tr>
<?PHP $count++; } ?>
更新:
请尝试以下代码:
$('.icon-remove').on('click', function () {
// Get the position of the current data from the node
var aPos = oTable.fnGetPosition( $(this).closest('tr').get(0) );
// Delete the row
oTable.fnDeleteRow(aPos);
} );
注:
API中没有fGetSelected
。它可以是OP拥有的自定义函数,但不是问题中产生的
fngeposition
是接受td
、th
或tr
元素的
.get(0)
是将元素从jQuery对象中分离出来
更新:
请尝试以下代码:
$('.icon-remove').on('click', function () {
// Get the position of the current data from the node
var aPos = oTable.fnGetPosition( $(this).closest('tr').get(0) );
// Delete the row
oTable.fnDeleteRow(aPos);
} );
注:
API中没有fGetSelected
。它可以是OP拥有的自定义函数,但不是问题中产生的
fngeposition
是接受td
、th
或tr
元素的
.get(0)
是将元素从jQuery对象中分离出来
为什么要使用.each()
?这不是设置单击事件的方式查看我的编辑。这也不管用,看看我的答案。它应该可以工作。为什么要使用.each()
?这不是设置单击事件的方式查看我的编辑。这也不管用,看看我的答案。它应该会导致以下错误:aPos为空。您在fnGetPosition中使用了“this”,但在本例中,它是类(.icon remove)处理程序,它只是每行中的一个按钮。猜测:)为了清楚起见,我使用的.icon remove
类被设置为每个tr
中的最后一个td
。您的DOM如何?请参阅问题中的“我的编辑”尝试使用$(this).respect('tr')
或$(this).respect('td')
而不是this
,并告诉我哪个(如果有的话)工作原理如何<代码>$(this).最近('td')
。对不起,我没有测试环境。jsbin和jsfiddle都神秘地不工作。在这个错误中,aPos为null。您在fnGetPosition中使用了“this”,但在本例中,它是类(.icon remove)处理程序,它只是每行中的一个按钮。猜测:)为了清楚起见,我使用的.icon remove
类被设置为每个tr
中的最后一个td
。您的DOM如何?请参阅问题中的“我的编辑”尝试使用$(this).respect('tr')
或$(this).respect('td')
而不是this
,并告诉我哪个(如果有的话)工作原理如何<代码>$(this).最近('td')。对不起,我没有测试环境。jsbin和jsfiddle都神秘地无法工作