11

我正在使用 Twitter Bootstrap 为 DataTables 网格中的每一行创建一个带有 DropDown 菜单的按钮,但是 DataTables 中的数据容器正在使用“溢出:隐藏”是什么使 DropDown 被剪切。

我不能只切换到“溢出:自动”,因为这会导致出现不必要的垂直滚动条。

这是一个JSFiddle

HTML

<table cellpadding="0" cellspacing="0" border="0" class="pretty" id="example"></table>

JS

/* API method to get paging information */
$.fn.dataTableExt.oApi.fnPagingInfo = function (oSettings) {
    return {
        "iStart": oSettings._iDisplayStart,
            "iEnd": oSettings.fnDisplayEnd(),
            "iLength": oSettings._iDisplayLength,
            "iTotal": oSettings.fnRecordsTotal(),
            "iFilteredTotal": oSettings.fnRecordsDisplay(),
            "iPage": oSettings._iDisplayLength === -1 ? 0 : Math.ceil(oSettings._iDisplayStart / oSettings._iDisplayLength),
            "iTotalPages": oSettings._iDisplayLength === -1 ? 0 : Math.ceil(oSettings.fnRecordsDisplay() / oSettings._iDisplayLength)
    };
}

/* Bootstrap style pagination control */
$.extend($.fn.dataTableExt.oPagination, {
    "bootstrap": {
        "fnInit": function (oSettings, nPaging, fnDraw) {
            var oLang = oSettings.oLanguage.oPaginate;
            var fnClickHandler = function (e) {
                e.preventDefault();
                if (oSettings.oApi._fnPageChange(oSettings, e.data.action)) {
                    fnDraw(oSettings);
                }
            };

            $(nPaging).addClass('pagination').append(
                '<ul>' +
                '<li class="prev disabled"><a href="#">&larr; ' + oLang.sPrevious + '</a></li>' +
                '<li class="next disabled"><a href="#">' + oLang.sNext + ' &rarr; </a></li>' +
                '</ul>');
            var els = $('a', nPaging);
            $(els[0]).bind('click.DT', {
                action: "previous"
            }, fnClickHandler);
            $(els[1]).bind('click.DT', {
                action: "next"
            }, fnClickHandler);
        },

            "fnUpdate": function (oSettings, fnDraw) {
            var iListLength = 5;
            var oPaging = oSettings.oInstance.fnPagingInfo();
            var an = oSettings.aanFeatures.p;
            var i, j, sClass, iStart, iEnd, iHalf = Math.floor(iListLength / 2);

            if (oPaging.iTotalPages < iListLength) {
                iStart = 1;
                iEnd = oPaging.iTotalPages;
            } else if (oPaging.iPage <= iHalf) {
                iStart = 1;
                iEnd = iListLength;
            } else if (oPaging.iPage >= (oPaging.iTotalPages - iHalf)) {
                iStart = oPaging.iTotalPages - iListLength + 1;
                iEnd = oPaging.iTotalPages;
            } else {
                iStart = oPaging.iPage - iHalf + 1;
                iEnd = iStart + iListLength - 1;
            }

            for (i = 0, iLen = an.length; i < iLen; i++) {
                // Remove the middle elements
                $('li:gt(0)', an[i]).filter(':not(:last)').remove();

                // Add the new list items and their event handlers
                for (j = iStart; j <= iEnd; j++) {
                    sClass = (j == oPaging.iPage + 1) ? 'class="active"' : '';
                    $('<li ' + sClass + '><a href="#">' + j + '</a></li>')
                        .insertBefore($('li:last', an[i])[0])
                        .bind('click', function (e) {
                        e.preventDefault();
                        oSettings._iDisplayStart = (parseInt($('a', this).text(), 10) - 1) * oPaging.iLength;
                        fnDraw(oSettings);
                    });
                }

                // Add / remove disabled classes from the static elements
                if (oPaging.iPage === 0) {
                    $('li:first', an[i]).addClass('disabled');
                } else {
                    $('li:first', an[i]).removeClass('disabled');
                }

                if (oPaging.iPage === oPaging.iTotalPages - 1 || oPaging.iTotalPages === 0) {
                    $('li:last', an[i]).addClass('disabled');
                } else {
                    $('li:last', an[i]).removeClass('disabled');
                }
            }
        }
    }
});

/* Table initialisation */
$(document).ready(function () {
    $('#example').dataTable({
        "aaData": [
        /* Reduced data set */ ['<div class="btn-group" style="float: left">' +
                                '<a class="btn btn-small dropdown-toggle" data-toggle="dropdown" href="#">Actions' +
                                    '<span class="caret"></span>' +
                                '</a>' +
                                '<ul class="dropdown-menu">' +
                                    '<li><a href="#">Approve</a></li>' +
                                    '<li><a href="#">View</a></li>' +
                                '</ul>' +
                            '</div>' +
                            '<a href="#" id="editButton" class="btn btn-small"><i class="icon-pencil"></i></a>' +
                            '<a href="#" class="btn btn-small deleteButton"><i class="icon-trash"></i></a>', "Internet Explorer 4.0", "Win 95+", 4, "X"],
            ['<div class="btn-group" style="float: left">' +
                                '<a class="btn btn-small dropdown-toggle" data-toggle="dropdown" href="#">Actions' +
                                    '<span class="caret"></span>' +
                                '</a>' +
                                '<ul class="dropdown-menu">' +
                                    '<li><a href="#">Approve</a></li>' +
                                    '<li><a href="#">View</a></li>' +
                                '</ul>' +
                            '</div>' +
                            '<a href="#" id="editButton" class="btn btn-small"><i class="icon-pencil"></i></a>' +
                            '<a href="#" class="btn btn-small deleteButton"><i class="icon-trash"></i></a>', "Internet Explorer 5.0", "Win 95+", 5, "C"],
            ['<div class="btn-group" style="float: left">' +
                                '<a class="btn btn-small dropdown-toggle" data-toggle="dropdown" href="#">Actions' +
                                    '<span class="caret"></span>' +
                                '</a>' +
                                '<ul class="dropdown-menu">' +
                                    '<li><a href="#">Approve</a></li>' +
                                    '<li><a href="#">View</a></li>' +
                                '</ul>' +
                            '</div>' +
                            '<a href="#" id="editButton" class="btn btn-small"><i class="icon-pencil"></i></a>' +
                            '<a href="#" class="btn btn-small deleteButton"><i class="icon-trash"></i></a>', "Internet Explorer 5.5", "Win 95+", 5.5, "A"],
            ['<div class="btn-group" style="float: left">' +
                                '<a class="btn btn-small dropdown-toggle" data-toggle="dropdown" href="#">Actions' +
                                    '<span class="caret"></span>' +
                                '</a>' +
                                '<ul class="dropdown-menu">' +
                                    '<li><a href="#">Approve</a></li>' +
                                    '<li><a href="#">View</a></li>' +
                                '</ul>' +
                            '</div>' +
                            '<a href="#" id="editButton" class="btn btn-small"><i class="icon-pencil"></i></a>' +
                            '<a href="#" class="btn btn-small deleteButton"><i class="icon-trash"></i></a>', "Internet Explorer 6.0", "Win 98+", 6, "A"],
            ['<div class="btn-group" style="float: left">' +
                                '<a class="btn btn-small dropdown-toggle" data-toggle="dropdown" href="#">Actions' +
                                    '<span class="caret"></span>' +
                                '</a>' +
                                '<ul class="dropdown-menu">' +
                                    '<li><a href="#">Approve</a></li>' +
                                    '<li><a href="#">View</a></li>' +
                                '</ul>' +
                            '</div>' +
                            '<a href="#" id="editButton" class="btn btn-small"><i class="icon-pencil"></i></a>' +
                            '<a href="#" class="btn btn-small deleteButton"><i class="icon-trash"></i></a>', "Internet Explorer 7.0", "Win XP SP2+", 7, "A"],
            ['<div class="btn-group" style="float: left">' +
                                '<a class="btn btn-small dropdown-toggle" data-toggle="dropdown" href="#">Actions' +
                                    '<span class="caret"></span>' +
                                '</a>' +
                                '<ul class="dropdown-menu">' +
                                    '<li><a href="#">Approve</a></li>' +
                                    '<li><a href="#">View</a></li>' +
                                '</ul>' +
                            '</div>' +
                            '<a href="#" id="editButton" class="btn btn-small"><i class="icon-pencil"></i></a>' +
                            '<a href="#" class="btn btn-small deleteButton"><i class="icon-trash"></i></a>', "Firefox 1.5", "Win 98+ / OSX.2+", 1.8, "A"],
            ['<div class="btn-group" style="float: left">' +
                                '<a class="btn btn-small dropdown-toggle" data-toggle="dropdown" href="#">Actions' +
                                    '<span class="caret"></span>' +
                                '</a>' +
                                '<ul class="dropdown-menu">' +
                                    '<li><a href="#">Approve</a></li>' +
                                    '<li><a href="#">View</a></li>' +
                                '</ul>' +
                            '</div>' +
                            '<a href="#" id="editButton" class="btn btn-small"><i class="icon-pencil"></i></a>' +
                            '<a href="#" class="btn btn-small deleteButton"><i class="icon-trash"></i></a>', "Firefox 2", "Win 98+ / OSX.2+", 1.8, "A"],
            ['<div class="btn-group" style="float: left">' +
                                '<a class="btn btn-small dropdown-toggle" data-toggle="dropdown" href="#">Actions' +
                                    '<span class="caret"></span>' +
                                '</a>' +
                                '<ul class="dropdown-menu">' +
                                    '<li><a href="#">Approve</a></li>' +
                                    '<li><a href="#">View</a></li>' +
                                '</ul>' +
                            '</div>' +
                            '<a href="#" id="editButton" class="btn btn-small"><i class="icon-pencil"></i></a>' +
                            '<a href="#" class="btn btn-small deleteButton"><i class="icon-trash"></i></a>', "Firefox 3", "Win 2k+ / OSX.3+", 1.9, "A"]
        ],
            "aoColumns": [{
            "sTitle": "Engine"
        }, {
            "sTitle": "Browser"
        }, {
            "sTitle": "Platform"
        }, {
            "sTitle": "Version",
            "sClass": "center"
        }, {
            "sTitle": "Grade",
            "sClass": "center"
        }],

            'sScrollX': "100%",
            'sScrollXInner': "150%",
            'bScrollCollapse': true,
            'bAutoWidth': false,
            'bDeferRender': true,
            'bLengthChange': false, "sPaginationType": "bootstrap",
            "oLanguage": {
            "sLengthMenu": "_MENU_ records per page"
        }
    });
    $('.acoes').dropdown();
});
4

6 回答 6

8

我遇到了同样的问题!解决了在 component.css 中删除溢出属性的问题

.table-scrollable {
  width: 100%;
/*  overflow-x: auto;
  overflow-y: hidden;*/
  border: 1px solid #dddddd;
  margin: 10px 0 !important;
}

或添加到您的 custom.css(在引导后调用)

.table-scrollable { 
    overflow-x: visible; 
    overflow-y: visible; 
}
于 2014-06-03T15:48:41.150 回答
6

这对我有用:Datatable + fixed headers + bootstrap dropdown in header

.dataTables_scrollHead{
    overflow: visible !important;
}

在所有其他人之后添加到自定义CSS

于 2016-01-20T14:05:59.773 回答
5

好的,在我们发表评论之后,我意识到你想要什么:

您有一个<div>带有 的元素overflow:auto,并且您希望其中的一个元素<div><ul>菜单所在的位置)从溢出规则中“逃脱”并出现浮动,从而覆盖其祖先的溢出规则。

但恐怕这是不可能的。您可以做的最接近的事情是:

  • 使用 javascript 在 with 之外创建带有菜单<ul>菜单,然后使用绝对位置将其设置在应该去的位置,或者<div>overflow:auto
  • 激活下拉菜单后,使用 javascript 在底部自动滚动,方法是在最后一个下拉菜单中添加类似于事件侦听器的内容。

第二种选择似乎更优雅,更少“hackish”。(我个人会忽略这个问题,但如果我必须选择,我会选择第二个选项)

于 2013-08-09T04:39:42.770 回答
2

对我有用的是调整 Bootstrap 下拉菜单的位置并将菜单fixed移动到屏幕的特定部分。

它适用于移动视图,因为它是相对于窗口的。

table .dropdown-menu {
    position: fixed !important;
    top: 50% !important;
    left: 92% !important;
    transform: translate(-92%, -50%) !important;
}

这样您就可以保持scroller扩展的行为scrollX和.scrollYDataTables

结果

于 2021-05-23T23:53:00.200 回答
0

.dataTables_scrollHead 选择器的覆盖 DataTable css 规则为我工作

.dataTables_scrollHead{
    position: static !important;
}
于 2013-08-30T14:47:50.353 回答
0
    .ngx-datatable {
  display: block;
  overflow: visible !important;
}

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



 .table-responsive .dropdown-menu {
  position: static !important;
  overflow: visible !important;
  top: 50% !important;
  left: 92% !important;
  transform: translate(-92%, -0%) !important;
}
于 2022-03-01T11:49:46.207 回答