由于滚动,响应表内的 Bootstrap 按钮下拉不可见

IT技术 javascript jquery css twitter-bootstrap overflow
2021-03-03 13:26:55

当响应和滚动活动时,表格内的下拉按钮出现问题,因为下拉列表由于overflow: auto;属性不可见我该如何解决这个问题,以便在折叠时显示按钮的下拉选项?我可以使用一些 jQuery,但是在左右滚动出现问题后,我决定找到另一个解决方案。我附上了一张照片以便更好地理解。在此处输入图片说明

这是一个小的js小提琴:

6个回答

我自己解决了这个问题,并将答案放在范围内以帮助其他有相同问题的用户:我们在引导程序中有一个事件,我们可以使用该事件进行设置,overflow: inherit但是如果您的父级没有 css 属性,这将起作用容器。

$('.table-responsive').on('show.bs.dropdown', function () {
     $('.table-responsive').css( "overflow", "inherit" );
});

$('.table-responsive').on('hide.bs.dropdown', function () {
     $('.table-responsive').css( "overflow", "auto" );
})

这是小提琴

info: 在这个小提琴示例中工作很奇怪,我不确定为什么但在我的项目中工作得很好。

在动态加载表的情况下: $('body') .on('show.bs.dropdown', '.table-responsive', function () { $(this).css("overflow", "visible"); }) .on('hide.bs.dropdown', '.table-responsive', function () { $(this).css("overflow", "auto"); });
2021-04-24 13:26:55
这仅适用于在该行的第一个单元格上有下拉列表的表格。
2021-05-05 13:26:55
但是点击后,数据超出了屏幕的最小尺寸。所以这不起作用.!!!!
2021-05-06 13:26:55
如果您可以在打开下拉菜单时阻止表格向左滚动,这将起作用。但是,如果您在屏幕右侧有一个下拉菜单,则在打开时表格会向左倾斜。.table-responsive在尝试编辑它们时,远离右侧的下拉菜单(被屏蔽)完全隐藏。
2021-05-21 13:26:55

CSS唯一解决办法是允许y轴到溢出。

http://www.bootply.com/YvePJTDzI0

.table-responsive {
  overflow-y: visible !important;
}

编辑

另一个仅 CSS 的解决方案是根据视口宽度响应地应用溢出:

@media (max-width: 767px) {
    .table-responsive .dropdown-menu {
        position: static !important;
    }
}
@media (min-width: 768px) {
    .table-responsive {
        overflow: inherit;
    }
}

https://www.codeply.com/go/D3XBvspns4

即使您删除了自定义 css,此解决方案也有效。也许这是一个html的东西。我仍在寻找仅 css 的解决方案。
2021-04-21 13:26:55
对我有用,除了按钮在页面底部......然后下拉菜单出现在窗口之外......
2021-05-02 13:26:55
@ZimSystem 仍然做同样的事情,这从根本上不是解决方案。
2021-05-13 13:26:55
不行。我想这是因为我正在使用数据表
2021-05-20 13:26:55

作为参考,现在是 2018 年,我使用的是 BS4.1

尝试添加data-boundary="viewport"到切换下拉列表的按钮(带有 class 的按钮dropdown-toggle)。请参阅https://getbootstrap.com/docs/4.1/components/dropdowns/#options

我确认这也是我想出的一个可行的解决方案。
2021-04-21 13:26:55
我不能说早期版本,但对于 Bootstrap 4,这确实是最好的解决方案。它很简单,它不牺牲响应能力,它使用框架中已经提供的功能,而不是试图用额外的样式或脚本来破解它。
2021-05-21 13:26:55

我采取了不同的方法,我已经将元素与父元素分离,并通过 jQuery 将其设置为绝对位置

工作 JS 小提琴:http : //jsfiddle.net/s270Lyrd/

在此处输入图片说明

我正在使用的 JS 解决方案。

//fix menu overflow under the responsive table 
// hide menu on click... (This is a must because when we open a menu )
$(document).click(function (event) {
    //hide all our dropdowns
    $('.dropdown-menu[data-parent]').hide();

});
$(document).on('click', '.table-responsive [data-toggle="dropdown"]', function () {
    // if the button is inside a modal
    if ($('body').hasClass('modal-open')) {
        throw new Error("This solution is not working inside a responsive table inside a modal, you need to find out a way to calculate the modal Z-index and add it to the element")
        return true;
    }

    $buttonGroup = $(this).parent();
    if (!$buttonGroup.attr('data-attachedUl')) {
        var ts = +new Date;
        $ul = $(this).siblings('ul');
        $ul.attr('data-parent', ts);
        $buttonGroup.attr('data-attachedUl', ts);
        $(window).resize(function () {
            $ul.css('display', 'none').data('top');
        });
    } else {
        $ul = $('[data-parent=' + $buttonGroup.attr('data-attachedUl') + ']');
    }
    if (!$buttonGroup.hasClass('open')) {
        $ul.css('display', 'none');
        return;
    }
    dropDownFixPosition($(this).parent(), $ul);
    function dropDownFixPosition(button, dropdown) {
        var dropDownTop = button.offset().top + button.outerHeight();
        dropdown.css('top', dropDownTop + "px");
        dropdown.css('left', button.offset().left + "px");
        dropdown.css('position', "absolute");

        dropdown.css('width', dropdown.width());
        dropdown.css('heigt', dropdown.height());
        dropdown.css('display', 'block');
        dropdown.appendTo('body');
    }
});
在使用 jquery 数据表(ajax 渲染)时,这个答案对我来说是完美的解决方案。我相应地进行了调整,效果很好。
2021-04-30 13:26:55
非常感谢这个解决方案。
2021-05-20 13:26:55

这个解决方案对我很有用:

@media (max-width: 767px) {
    .table-responsive .dropdown-menu {
        position: static !important;
    }
}
@media (min-width: 768px) {
    .table-responsive {
        overflow: visible;
    }
}

更多细节:https : //github.com/twbs/bootstrap/issues/15374