-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Ellipsis for long text in small width selects #351
Comments
Pretty easy with some CSS: .bootstrap-select .dropdown-menu {
max-width: 200px;
}
.bootstrap-select .dropdown-menu span.text {
display: block;
overflow: hidden;
text-overflow: ellipsis;
} I added a cool animation feature on mouseover to scroll the text too: var $select = $('select');
// Init selectpicker.
$select.selectpicker();
// Loop through all of the option links in the dropdown menu.
$select.data('selectpicker').$menu.find('li a').each(function () {
var $link = $(this)
, $text = $link.find('span.text')
;
// Bind to mouseenter.
$link.on('mouseenter', function () {
// Clone the text & append it to the body so we can measure its natural width.
var $clone = $text.clone().appendTo('body')
, diff = ($clone.width() - $text.width())
;
// Remove our dummy clone.
$clone.remove();
// If the text content is wider than the menu, animate the `text-indent`.
if (diff > 0) {
$text.stop(true).delay(250).animate({textIndent: '-' + diff + 'px'});
}
});
// On mouseleave, animate the `text-indent` back to `0`.
$link.on('mouseleave', function () {
$text.stop(true).delay(250).animate({textIndent: 0});
});
}); The appending of the text to the body works in my use-case, but if the font family or size differs between the body and the dropdown, the width measurement may be off... |
Thanks. Very helpful. With the mouse events, where/what is $menu ($select.data('selectpicker').$menu.find('li a').each(function () {)? From: Jesse Street [mailto:notifications@github.com] Pretty easy with some CSS: .bootstrap-select .dropdown-menu { I added a cool animation feature on mouseover to scroll the text too: var $select = $('select'); // Init selectpicker. // Loop through all of the option links in the dropdown menu. // Bind to mouseenter.
}); // On mouseleave, animate the The appending of the text to the body works in my use-case, but if the font family or size differs between the body and the dropdown, the width measurement may be off... — |
Actually, the more I look at this, I don’t want to place the ellipsis on the options but only on the selected (the one visible in the select box after selection). Ideally, the max width should apply to the select box and not the options. The options should display in full width to their text in the dropdown, but since the select box itself may be a smaller width than its options, the ellipsis is applied. Would it be simply: .bootstrap-select > .filter-option { max-width: 200px; overflow: hidden; text-overflow: ellipsis; } My use-case is I’d like to add a simple jQuery modifier to find perhaps a class such as “bootstrap-select-ellipsis” and/or perhaps a “data-maxwidth” attribute which would apply this on-the-fly to only those select boxes rather than applying to all. From: Jesse Street [mailto:notifications@github.com] Pretty easy with some CSS: .bootstrap-select .dropdown-menu { I added a cool animation feature on mouseover to scroll the text too: var $select = $('select'); // Init selectpicker. // Loop through all of the option links in the dropdown menu. // Bind to mouseenter.
}); // On mouseleave, animate the The appending of the text to the body works in my use-case, but if the font family or size differs between the body and the dropdown, the width measurement may be off... — |
The ellipsis can be taken care of with just .bootstrap-select.btn-group .btn .filter-option {
text-overflow: ellipsis;
} This will only show the ellipsis if the text is cut off, but if you wanted to apply the ellipsis only on certain selects (for whatever reason), just do .bootstrap-select.btn-group.ellipsis .btn .filter-option {
text-overflow: ellipsis;
} Then add the |
When a long option string is selected for a select of a smaller width, rather than cutting of the text, provide an ellipsis (clean, nice readability). Additional nice-to-have would be the actual text displayable (title attr?) on mouse over.
The text was updated successfully, but these errors were encountered: