简体   繁体   English

删除 jQuery 中的父行而不是子行

[英]Delete the parent row not the child row in jQuery

在此处输入图像描述

This is the result of a datatable on a specific screen size.这是特定屏幕尺寸上的数据表的结果。 If all the columns are displayed, there is no problem deleting the row using如果显示所有列,则使用删除行没有问题

$(this).closest('tr').remove();

But when screen size became smaller, this will be the result.但是当屏幕尺寸变小时,就会出现这样的结果。 I cannot use the above code anymore.我不能再使用上面的代码了。 Now if I hit the button with the class deleteRow , I want to delete the tr with the class parent and its child (though the child row is not inside the parent row), not just the closest row.现在,如果我用 class deleteRow按下按钮,我想用 class级及其子级删除tr (尽管子行不在父行内),而不仅仅是最近的行。 There could be many rows that have class parent so I just want to delete the row associates to the childs' row.可能有很多行具有 class级,所以我只想删除与子行关联的行。

I want to delete the tr with the class parent , not just the closest row.我想用 class parent 删除 tr ,而不仅仅是最近的行。

Then include that extra info .parent in the selector like so:然后在选择器中包含额外的信息.parent ,如下所示:

$(this).closest('tr.parent').remove();

Try尝试

$(this).closest('tr.child')

You can have a look at the samples I've put together here, I've tried to split into many cases that you can reference and re-apply to your project.您可以查看我在这里整理的示例,我尝试将其拆分为许多案例,您可以参考并重新应用到您的项目中。

Let me know which's suitable for you and I can explain it clearer.让我知道哪个适合您,我可以更清楚地解释它。

 $(document).ready(function() { $('.delete-single-parent').each(function(index) { var $this = $(this); $this.click(function() { // $(this) = the element button clicked // Try row 1,3,5 $(this).closest('tr.parent').remove(); // Try row 2,4,6. Assuming "parent" is always a sibling of current `child` $(this).closest('tr.child').prev().remove(); }) }); // This queries to the top (table), and find all parent rows to remove // Will remove row 1,3,5 only $('.delete-all-parents').click(function() { $(this).closest('table').find('tr.parent').remove(); }) })
 table { border: 1px solid grey; } table tr:nth-child(odd) { background-color: #f3f3f3; } table tr td { padding: 10px; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <table> <tr class="parent"> <td> <div>1</div> <button class="delete-single-parent">Delete Direct parent</button> <button class="delete-all-parents">Delete row 1,3,5</button> </td> </tr> <tr class="child"> <td> <div>2</div> <button class="delete-single-parent">Delete Parent (row 1)</button> <button class="delete-all-parents">Delete row 1,3,5</button> </td> </tr> <tr class="parent"> <td> <div>3</div> <button class="delete-single-parent">Delete Direct parent</button> <button class="delete-all-parents">Delete row 1,3,5</button> </td> </tr> <tr class="child"> <td> <div>4</div> <button class="delete-single-parent">Delete Parent (row 3)</button> <button class="delete-all-parents">Delete row 1,3,5</button> </td> </tr> <tr class="parent"> <td> <div>5</div> <button class="delete-single-parent">Delete Direct parent</button> <button class="delete-all-parents">Delete row 1,3,5</button> </td> </tr> <tr class="child"> <td> <div>6</div> <button class="delete-single-parent">Delete Parent (row 5)</button> <button class="delete-all-parents">Delete row 1,3,5</button> </td> </tr> </table>

Just fixed my own issue.刚刚解决了我自己的问题。 I added attribute to the parent row upon page loading.我在页面加载时向父行添加了属性。

<tr data_id = {{ $item->id }}>

this will make the tr unique so that I can call this later to delete the parent as well as the child row.这将使tr独一无二,以便我稍后可以调用它来删除父行和子行。

to delete the child where the button is placed删除放置按钮的孩子

$(this).closest('tr.child').remove();

and added code to delete the parent row through attribute as selector.并添加代码以通过属性作为选择器删除父行。

$('[data_id="'+ row_id +'"]').remove();

so upon ajax success, I will call these two.所以在 ajax 成功后,我将调用这两个。

$tr.find('td').fadeOut(1000,function(){ 
    $(this).closest('tr.child').remove();
    $('[data_id="'+ row_id +'"]').remove(); 
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM