I had issues using bootstrap dropdowns in <table>
, it was cut and behaved weirdly. I've found a way to move the dropdown menu out of the table with this example, but I'm not sure how to handle multiple dropdowns, as it is used per each table row:
<table>
@foreach(...){
<tr>
<td>
<div class="dropdown">
<button data-bs-toggle="dropdown" data-bs-auto-close="true" data-bs-offset="0,2" aria-expanded="false">See review</button>
<div class="dropdown-menu" role="menu">/*some menu items*/</div>
</div>
</td>
</tr>
}
</table>
Here is the JS:
var dropdownMenu:any;
$('.dropdown').on('show.bs.dropdown', function (e:any) {
dropdownMenu = $(this).find('.dropdown-menu');
$('body').append(dropdownMenu.detach());
dropdownMenu.css('display', 'block');
dropdownMenu.position({
'my': 'right top',
'at': 'right bottom',
'of': $(e.relatedTarget)
});
});
$(".dropdown").on('hide.bs.dropdown', function (e: any) {
if (dropdownMenu != undefined) {
$(e.currentTarget).append(dropdownMenu.detach());
dropdownMenu.hide();
}
});
It works fine if you open each dropdown and click outside, but if you open one dropdown and then click on another right after (like click See review in one row and then click See review in another row right after), it closes all the dropdowns and the previously opened one's DOM remains in body.