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)?

Share Improve this question edited Feb 23, 2015 at 15:33 user1032531 asked Feb 23, 2015 at 15:10 user1032531user1032531 26.4k75 gold badges245 silver badges416 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 4

Simple 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