Skip to content

Instantly share code, notes, and snippets.

@wellwind
Created June 12, 2015 02:50
Show Gist options
  • Select an option

  • Save wellwind/8ca3a4d1568f18619574 to your computer and use it in GitHub Desktop.

Select an option

Save wellwind/8ca3a4d1568f18619574 to your computer and use it in GitHub Desktop.
$(document).ready(function(){
$('.dropdown-menu').parent().on('show.bs.dropdown', function () {
var parentResponsiveTable = $(this).parents('.table-responsive');
var parentTarget = $(parentResponsiveTable).first().hasClass('table-responsive') ? $(parentResponsiveTable) : $(window);
var parentTop = $(parentResponsiveTable).first().hasClass('table-responsive') ? $(parentResponsiveTable).offset().top : 0;
var parentLeft = $(parentResponsiveTable).first().hasClass('table-responsive') ? $(parentResponsiveTable).offset().left : 0;
var dropdownMenu = $(this).children('.dropdown-menu').first();
if (!$(this).hasClass('dropdown') && !$(this).hasClass('dropup')) {
$(this).addClass('dropdown');
}
$(this).attr('olddrop', $(this).hasClass('dropup') ? 'dropup' : 'dropdown');
$(this).children('.dropdown-menu').each(function () {
$(this).attr('olddrop-pull', $(this).hasClass('dropdown-menu-right') ? 'dropdown-menu-right' : '');
});
if ($(this).hasClass('dropdown')) {
if ($(this).offset().top + $(this).height() + $(dropdownMenu).height() + 10 >= parentTop + $(parentTarget).height()) {
$(this).removeClass('dropdown');
$(this).addClass('dropup');
}
} else if ($(this).hasClass('dropup')) {
if ($(this).offset().top - $(dropdownMenu).height() - 10 <= parentTop) {
$(this).removeClass('dropup');
$(this).addClass('dropdown');
}
}
if ($(this).offset().left + $(dropdownMenu).width() >= parentLeft + $(parentTarget).width()) {
$(this).children('.dropdown-menu').addClass('dropdown-menu-right');
}
});
$('.dropdown-menu').parent().on('hide.bs.dropdown', function () {
if ($(this).attr('olddrop') != '') {
$(this).removeClass('dropup dropdown');
$(this).addClass($(this).attr('olddrop'));
$(this).attr('olddrop', '');
}
$(this).children('.dropdown-menu').each(function () {
$(this).removeClass('dropdown-menu-right');
$(this).addClass($(this).attr('olddrop-pull'));
});
});
});
@budnieswski
Copy link

To avoid DataTable overflow, add this codes:
In show trigger:
$(this).closest('.table-scrollable').css('overflow', 'visible');

In hide trigger:
$(this).closest('.table-scrollable').css('overflow', '');

Can be put wherever you want, inside triggers!

Example:

$('.dropdown-menu').parent().on('show.bs.dropdown', function () {
    $(this).closest('.table-scrollable').css('overflow', 'visible');
    [...]

$('.dropdown-menu').parent().on('hide.bs.dropdown', function () {
    $(this).closest('.table-scrollable').css('overflow', '');
    [...]

Before:

corrected-dropdown-01

After:

corrected-dropdown-02

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment