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

Bur*_*ler 73 javascript css jquery overflow twitter-bootstrap

当响应和滚动活动时,表格内的下拉按钮出现问题,因为overflow: auto;属性导致下拉列表不可见.我如何修复它以便在折叠时显示按钮的下拉选项?我可以使用一些jQuery,但在我左右滚动问题后,我决定找到另一个解决方案.我附上照片以便更好地理解.在此输入图像描述

这是一个小小的小提琴:

Bur*_*ler 55

我解决了这个问题,我把答案放在范围内,以帮助其他有相同问题的用户:我们在bootstrap中有一个事件,我们可以使用该事件进行设置,overflow: inherited但如果您的父容器上没有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" );
})
Run Code Online (Sandbox Code Playgroud)

这就是小提琴

info: 在这个小提琴例子工作奇怪,我不知道为什么,但在我的项目工作正常.

  • 在动态加载表的情况下:`$('body').on('show.bs.dropdown','.table-responsive',function(){$(this).css("overflow","visible" );}} .on('hide.bs.dropdown','.table-responsive',function(){$(this).css("overflow","auto");});` (5认同)
  • 这仅适用于在行的第一个单元格上有下拉列表的表. (3认同)
  • 但是单击后,数据超出了屏幕的最小大小。所以这是行不通的。 (2认同)
  • 如果您可以在打开下拉列表时阻止表格向左滚动,那么这将起作用。但事实上,如果屏幕右侧有一个下拉列表,则打开时表格会向左猛拉。尝试编辑时,位于右侧的下拉菜单(由“.table-responsive”类屏蔽)完全隐藏。 (2认同)

Zim*_*Zim 37

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

http://www.bootply.com/YvePJTDzI0

.table-responsive {
  overflow-y: visible !important;
}
Run Code Online (Sandbox Code Playgroud)

编辑

另一个CSS唯一的解决方案是响应性地应用基于视口宽度的溢出:

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

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


Rya*_*ier 20

Bootstrap 5 解决方案

这对我来说最有效:

.table-responsive .dropdown,
.table-responsive .btn-group,
.table-responsive .btn-group-vertical {
    position: static;
}
Run Code Online (Sandbox Code Playgroud)


Waz*_*ime 15

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

工作JS fidle: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');
    }
});
Run Code Online (Sandbox Code Playgroud)

  • 这个答案对我来说是使用 jquery datatable(ajax render) 时的完美解决方案。我进行了相应的调整,效果很好。 (2认同)

pha*_*ong 13

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

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

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


Ben*_*Ben 13

作为参考,它是2018年,我正在使用BS4.1

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

  • 我确认这是我提出的一个有效的解决方案. (3认同)
  • 我不能说早期版本,但是对于Bootstrap 4,这确实是最好的解决方案。很简单,它不会牺牲响应能力,它使用框架中已经提供的功能,而不是尝试使用其他样式或脚本来破坏它。 (3认同)
  • 将 `position-static` 添加到下拉/按钮容器,就像这样 `<div class="btn-groupposition-static"><button type="butt` 在 5.2 中对我有用。 (3认同)
  • 我认为这是正确的答案。不幸的是,这在 Bootstrap 5 中被破坏了。即使您将其更改为使用更新的 `data-bs-boundary="viewport"`。 (2认同)

Hal*_*şil 13

定义此属性。祝你好运!

data-toggle="dropdown" data-boundary="window"
Run Code Online (Sandbox Code Playgroud)

  • Bootstrap 4 唯一的解决方案 (3认同)
  • 不支持 BS5 (3认同)

Leo*_*iro 10

我有一个只使用CSS的解决方案,只需使用位置相对于表内的下拉列表 - 响应:

@media (max-width: 767px) {
  .table-responsive .dropdown-menu {
    position: relative; /* Sometimes needs !important */
  }
}
Run Code Online (Sandbox Code Playgroud)

https://codepen.io/leocaseiro/full/rKxmpz/

  • 这个“小提琴不起作用”。它“隐藏”桌子下面的下拉菜单。 (2认同)

小智 9

我的2¢快速全球修复:

// drop down in responsive table

(function () {
  $('.table-responsive').on('shown.bs.dropdown', function (e) {
    var $table = $(this),
        $menu = $(e.target).find('.dropdown-menu'),
        tableOffsetHeight = $table.offset().top + $table.height(),
        menuOffsetHeight = $menu.offset().top + $menu.outerHeight(true);

    if (menuOffsetHeight > tableOffsetHeight)
      $table.css("padding-bottom", menuOffsetHeight - tableOffsetHeight);
  });

  $('.table-responsive').on('hide.bs.dropdown', function () {
    $(this).css("padding-bottom", 0);
  })
})();
Run Code Online (Sandbox Code Playgroud)

吃茶:当显示内部的"表响应"下拉菜单中,它计算所述台的高度和其展开(具有填充)来匹配以显示菜单所需要的高度.菜单可以是任何尺寸.

在我的情况下,这不是具有'.table-responsive'类的表,它是一个包装div:

<div class="table-responsive" style="overflow:auto;">
    <table class="table table-hover table-bordered table-condensed server-sort">
Run Code Online (Sandbox Code Playgroud)

所以脚本中的$ table var实际上是一个div!(只是为了清楚......或者不是):)

注意:我将它包装在一个函数中,以便我的IDE可以折叠函数;)但它不是强制性的!

  • 这是唯一对我有用的解决方案,但我建议使用$ table.css("padding-bottom",menuOffsetHeight - tableOffsetHeight + 16); $ table.css的插入("padding-bottom",menuOffsetHeight - tableOffsetHeight); 添加默认情况下在引导程序上添加的额外填充,加上底部的小间隙. (2认同)

Cyr*_* V. 8

这已通过添加data-boundary="viewport"( Bootstrap Dropdowns Docs )在 Bootstrap v4.1 及更高版本中得到修复

但是对于早期版本(v4.0 及更低版本),我发现这个 javascript代码段非常有效。它适用于小表和滚动表:

$('.table-responsive').on('shown.bs.dropdown', function (e) {
    var t = $(this),
        m = $(e.target).find('.dropdown-menu'),
        tb = t.offset().top + t.height(),
        mb = m.offset().top + m.outerHeight(true),
        d = 20; // Space for shadow + scrollbar.
    if (t[0].scrollWidth > t.innerWidth()) {
        if (mb + d > tb) {
            t.css('padding-bottom', ((mb + d) - tb));
        }
    }
    else {
        t.css('overflow', 'visible');
    }
}).on('hidden.bs.dropdown', function () {
    $(this).css({'padding-bottom': '', 'overflow': ''});
});
Run Code Online (Sandbox Code Playgroud)


Har*_*tel 8

尝试一次。经过 1 小时的网络研究后,我找到了这个问题的最佳解决方案。

解决方案: -只需添加脚本

(function () {
    // hold onto the drop down menu                                             
    var dropdownMenu;

    // and when you show it, move it to the body                                     
    $(window).on('show.bs.dropdown', function (e) {

    // grab the menu        
    dropdownMenu = $(e.target).find('.dropdown-menu');

    // detach it and append it to the body
    $('body').append(dropdownMenu.detach());

    // grab the new offset position
    var eOffset = $(e.target).offset();

    // make sure to place it where it would normally go (this could be improved)
    dropdownMenu.css({
        'display': 'block',
            'top': eOffset.top + $(e.target).outerHeight(),
            'left': eOffset.left
       });
    });

    // and when you hide it, reattach the drop down, and hide it normally                                                   
    $(window).on('hide.bs.dropdown', function (e) {
        $(e.target).append(dropdownMenu.detach());
        dropdownMenu.hide();
    });
})();
Run Code Online (Sandbox Code Playgroud)

输出:- 解决方案


qua*_*kes 5

稍微清理了@Wazime 解决方案。作为通用解决方案非常有效。

$(document).on('shown.bs.dropdown', '.table-responsive', function (e) {
    // The .dropdown container
    var $container = $(e.target);

    // Find the actual .dropdown-menu
    var $dropdown = $container.find('.dropdown-menu');
    if ($dropdown.length) {
        // Save a reference to it, so we can find it after we've attached it to the body
        $container.data('dropdown-menu', $dropdown);
    } else {
        $dropdown = $container.data('dropdown-menu');
    }

    $dropdown.css('top', ($container.offset().top + $container.outerHeight()) + 'px');
    $dropdown.css('left', $container.offset().left + 'px');
    $dropdown.css('position', 'absolute');
    $dropdown.css('display', 'block');
    $dropdown.appendTo('body');
});

$(document).on('hide.bs.dropdown', '.table-responsive', function (e) {
    // Hide the dropdown menu bound to this button
    $(e.target).data('dropdown-menu').css('display', 'none');
});
Run Code Online (Sandbox Code Playgroud)