admin管理员组文章数量:1399997
shows how to add a scroll bar to a jQueryUI autoplete. It works as advertised, however, it is difficult to manage if multiple autoplete widgets are used which have different height requirements. For my case, I have various dialogs (all with an associated height) and have the autoplete widgets within the dialog.
When configuring the autoplete (i.e. $( "#tags" ).autoplete({source: availableTags, ...});
), could the scroll bar and associated height be added? Could it be done so where it takes the height setting from the parent element (i.e. the dialog)?
http://jqueryui./autoplete/#maxheight shows how to add a scroll bar to a jQueryUI autoplete. It works as advertised, however, it is difficult to manage if multiple autoplete widgets are used which have different height requirements. For my case, I have various dialogs (all with an associated height) and have the autoplete widgets within the dialog.
When configuring the autoplete (i.e. $( "#tags" ).autoplete({source: availableTags, ...});
), could the scroll bar and associated height be added? Could it be done so where it takes the height setting from the parent element (i.e. the dialog)?
4 Answers
Reset to default 4Simple way to modify the autoplete widget, add a css class to widget here is the css
.fixedHeight {
color:red;
font-size:10px;
max-height: 150px;
margin-bottom: 10px;
overflow-x: auto;
overflow-y: auto;
}
after creating autoplete in your js add the fixedHeight class like this
$( "#courses" ).autoplete({
//load autoplete options
});
$( "#courses" ).autoplete("widget").addClass("fixedHeight");
check this working sample
You have to find the class named .auto-plete
in jquery-ui.css
. There you can add min-height and max height which will fix the height for minimum height and maximum height of the autoplete box. Give it a try.
With $("#tags2").autoplete("widget").addClass('fixed-height');
you can add a height class to the autoplete widget.
If you want to set a global max-height you could override the css class .ui-autoplete
.
See the following demo for some ways to change the height but I think the best will be addClass. (You can find the fiddle with the same code here)
$(function () {
var availableTags = [
"ActionScript",
"AppleScript",
"Asp",
"BASIC",
"C",
"C++",
"Clojure",
"COBOL",
"ColdFusion",
"Erlang",
"Fortran",
"Groovy",
"Haskell",
"Java",
"JavaScript",
"Lisp",
"Perl",
"PHP",
"Python",
"Ruby",
"Scala",
"Scheme"];
$("#tags").autoplete({
source: availableTags
});
$("#tags2").autoplete({
source: availableTags
});
$("#tags3").autoplete({
source: availableTags
});
$("#tags2").autoplete("widget").addClass('fixed-height');
$("#tags3").autoplete("widget").attr('style', 'max-height: 400px; overflow-y: auto; overflow-x: hidden;')
});
.ui-autoplete {
max-height: 100px;
overflow-y: auto;
/* prevent horizontal scrollbar */
overflow-x: hidden;
}
/* IE 6 doesn't support max-height
* we use height instead, but this forces the menu to always be this tall
*/
* html .ui-autoplete {
height: 100px;
}
.fixed-height {
padding: 1px;
max-height: 200px;
overflow: auto;
}
<link href="https://cdnjs.cloudflare./ajax/libs/jqueryui/1.11.2/jquery-ui.css" rel="stylesheet"/>
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare./ajax/libs/jqueryui/1.11.2/jquery-ui.js"></script>
<div class="ui-widget">
<label for="tags">Tags max-height set with class .ui-autoplete:</label>
<input id="tags" />
</div>
<div class="ui-widget">
<label for="tags">Tags max-height set manually with class fixed-height:</label>
<input id="tags2" />
</div>
<div class="ui-widget">
<label for="tags">Tags max-height set with jQuery:</label>
<input id="tags3" />
</div>
you should be able to add the prop to the item itself when creating it. The auto plete will get the value from the css then override with the value you set. Something like
$( "#tags" ).autoplete({source: availableTags, ...});
$( "#tags" ).autoplete("widget").attr("max-height", "5px");
^ not tested will tinker with it in a few to get a good working example
本文标签: Adding scroll bar to jQuery autocomplete using JavaScriptStack Overflow
版权声明:本文标题:Adding scroll bar to jQuery autocomplete using JavaScript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744192392a2594577.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论