[英]Angular' Bootstrap drop-down button size
I have some requirements that force me to style angular bootstrap drop-down to be like this : 我有一些要求,迫使我的风格的角度引导下拉要像这样 :
Everything works fine except that I need to make the button's text responsive. 一切正常,除了我需要使按钮的文本响应。 so what ever user pick in drop-down list should be shown in button. 因此,用户在下拉列表中选择的内容都应显示在按钮中。
As you can see I have small triangle on right upper side of drop-down list and when you choose some option with shorter width it will cause problem. 如您所见,我在下拉列表的右上角有一个小三角形,当您选择宽度较小的某个选项时,将会引起问题。 like below 像下面
My question i,s is there any way I can fix this issue? 我的问题是,有什么办法可以解决这个问题? for example by setting with of button equal to maxim length I have in drop-down list? 例如通过设置与等于我在下拉列表中的最大长度的按钮的? or any other solution? 或其他解决方案?
you can find all the code here: 您可以在此处找到所有代码:
http://plnkr.co/edit/zmDUjqdtPsql9GJjva4T?p=info http://plnkr.co/edit/zmDUjqdtPsql9GJjva4T?p=info
angular.module('ui.bootstrap.demo').directive('uibDropdownTemplate', ['$log', function ($log) {
return {
restrict: 'EA',
replace: true,
scope: {
defaultText: '@',
options: '=',
selectedOption: '='
},
templateUrl: 'drop-down.html',
controller: function() {
},
link: function (scope, element, attrs) {
if (scope.selectedOption === undefined) {
scope.selectedOption = scope.defaultText;
}
scope.selectedChange = function(option,$event){
if(scope.selectedOption === option) {
scope.selectedOption = scope.defaultText
$($event.target).removeClass("option-selected");
}
else
{
scope.selectedOption = option;
$("ul.dropdown-menu>li>a").removeClass("option-selected");
$($event.target).addClass("option-selected");
}
};
}
};
}]);
Without fiddling with fonts and em
and such, I could only come up with a quick-and-dirty solution in terms of setting the width. 如果不摆弄字体和em
类的东西,我只能在设置宽度方面想出一种快速而又肮脏的解决方案。 The general concept, however, is to determine the max length of your options and then use ng-class
to manually set the width on your preCaretText
to the desired width. 但是,一般概念是确定选项的最大长度,然后使用ng-class
手动将preCaretText
上的宽度设置为所需的宽度。 It my example, I set it to the max length x 7 px, which seems pretty close on my Chrome browser. 在我的示例中,我将其设置为最大长度x 7像素,这在我的Chrome浏览器上似乎非常接近。
http://plnkr.co/edit/vK3DxLM7mNuKG0w2Q8jv?p=preview http://plnkr.co/edit/vK3DxLM7mNuKG0w2Q8jv?p=preview
drop-down.html 下拉down.html
<div class="preCaretText" ng-style="{'width': maxOptionLength * 7+ 'px'} ">{{selectedOption}}</div>
example.js example.js
angular.module('ui.bootstrap.demo').directive('uibDropdownTemplate', ['$log', function ($log) {
return {
restrict: 'EA',
replace: true,
scope: {
defaultText: '@',
options: '=',
selectedOption: '='
},
templateUrl: 'drop-down.html',
controller: function() {
},
link: function (scope, element, attrs) {
var maxOptionLength = Math.max.apply(Math, scope.options.map(function (el) { return el.length }));
scope.maxOptionLength = maxOptionLength;
if (scope.selectedOption === undefined) {
scope.selectedOption = scope.defaultText;
}
scope.selectedChange = function(option,$event){
if(scope.selectedOption === option) {
scope.selectedOption = scope.defaultText
$($event.target).removeClass("option-selected");
}
else
{
scope.selectedOption = option;
$("ul.dropdown-menu>li>a").removeClass("option-selected");
$($event.target).addClass("option-selected");
}
};
}
};
}]);
Is using a btn-block
on both your button and dropdown not an option? 在按钮和下拉菜单上都使用btn-block
不是一种选择吗? That way they'll both take the size of their parent element: 这样,它们都将采用其父元素的大小:
<div>
<div class="btn-group btn-block" uib-dropdown >
<button type="button" class="btn drop btn-block" uib-dropdown-toggle>
<div class="preCaretDiv">
<div class="preCaretText">{{selectedOption}} </div>
<div class="caretCircle">
<span class="caret"></span>
</div>
</div>
</button>
<ul class="uib-dropdown-menu btn-block" role="menu" aria-labelledby="button-template-url">
<li role="menuitem" ng-repeat="option in options" ng-click="selectedChange(option,$event)"><a href="">{{option}}</a></li>
</ul>
</div>
</div>
http://plnkr.co/edit/8IiSWese3fVtnG29MmFK?p=preview http://plnkr.co/edit/8IiSWese3fVtnG29MmFK?p=preview
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.