简体   繁体   English

JQuery 数据表中的引导弹出窗口在搜索后不起作用

[英]Bootstrap Popover in JQuery Datatable doesn't work after search

I have a HTML table:我有一个 HTML 表:

 <table id="PeopleTable" class="table table-striped table-bordered"> <thead> <tr> <th>ID</th> <th>Name</th> <th>Pop</th> </tr> </thead> <tbody> <tr><td>0</td><td>Michael</td><td><button class='popoverButton'>Popover</button></td></tr> </tbody></table>

And I want to use the DataTables plug-in to have search, order and filter functionality.我想使用 DataTables 插件来拥有搜索、排序和过滤功能。 I also want to use Bootstrap to show a popover when the button is clicked, so I've tried this:我还想使用 Bootstrap 在单击按钮时显示一个弹出窗口,所以我尝试了这个:

 var peopleTable = $('#PeopleTable').DataTable({ drawCallback: function () { $('.popoverButton').popover({ "html": true, trigger: 'manual', placement: 'left', "content": function () { return "<div>Popover content</div>"; } }).click(function (e) { $(this).popover('toggle'); e.stopPropagation(); }); } });

The problem is: when I perform a search, column sorting or any operation with the DataTable, the Popover stops working.问题是:当我对 DataTable 执行搜索、列排序或任何操作时,Popover 停止工作。

Here is the fiddle if you want to try it.如果您想尝试,这里是小提琴

Is "draw" the correct event to perform this or should I use another one? “绘制”是执行此操作的正确事件还是我应该使用另一个? Am I missing any other thing?我还缺少其他东西吗?

Updated JS fiddle link - https://jsfiddle.net/dxrjm530/4/更新了 JS 小提琴链接 - https://jsfiddle.net/dxrjm530/4/

You just need to take out your click event of button, because after sorting, it is getting called twice, due to drawcallback method of datatable.您只需要取出按钮的单击事件,因为在排序后,由于数据表的drawcallback方法,它会被调用两次。

 $('#PeopleTable').DataTable({ drawCallback: function () { $('.popoverButton').popover({ "html": true, trigger: 'manual', placement: 'left', "content": function () { return "<div>Popover content</div>"; } }) } }); $('#AddRow').on('click', function(){ var peopleTable = $('#PeopleTable').DataTable(); peopleTable.row.add( ['1', "David", "<button class='popoverButton'>Popover</button>"] ).draw(); }); $('table').on('click', function(e){ if($('.popoverButton').length>1) $('.popoverButton').popover('hide'); $(e.target).popover('toggle'); });

Possible solution: https://stackoverflow.com/a/72850074/979174可能的解决方案: https://stackoverflow.com/a/72850074/979174

 const rows = $("#tbl").dataTable().fnGetNodes(); $(rows).find('[data-toggle="popover"]').popover({ html: true, trigger: 'hover', placement: 'bottom', content: function () { return $(this).data('content'); } });

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

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