it:ad:jquery:ui:autocomplete_combobox

IT:AD:JScript:Libs:JQuery:UI:AutoComplete:ComboBox

$.widget(
            "ui.combobox",
            {
                //options are the arguments
                //set by invoking widget, passing it a map
                //that merges in values.
                //eg: $("#mySelect").combobox({url="..."});
                options: {
                    url: "",
                    httpMethod: "GET",
                    datatype: "json",
                    maxVisibleItems: 8,
                    buttonTitle: "Show Options",
                    moreText: "More: refine search...",
                    showValueAsTitle: false
                },

                _create: function () {
                    var input,
                        self = this,
                    //hide the combo, as we'll be showing
                    //a text input instead:
                        select = this.element.hide(),
                    //Get the selected item at load time.
                        selected = select.children(":selected"),
                    //Get it's value if you can:
                        value = selected.val() ? selected.text() : "",

                        wrapper = this.wrapper = $("<span>")
                            .addClass("ui-combobox")
                            .insertAfter(select);

                    var x = select.data('url');
                    if (typeof (x) != "undefined") {
                        self.options.url = x;
                        if (!self.options.url) {
                            self.options.url = "['url' not set.]";
                        }
                    }
                    x = select.data('dataType')
                    if (typeof (x) != "undefined") {
                        self.options.dataType = x;
                        if (!self.options.dataType) {
                            self.options.dataType = 'json';
                        }
                    }
                    x = select.data('httpMethod');
                    if (typeof (x) != "undefined") {
                        self.options.httpMethod = x;
                        if (!self.options.httpMethod) {
                            self.options.httpMethod = 'GET';
                        }
                    }
                    x = select.data('maxVisibleItems');
                    if (typeof (x) != "undefined") {
                        self.options.maxVisibleItems = parseInt(x);
                        if (self.options.maxVisibleItems == NaN || self.options.maxVisibleItems < 5) {
                            self.options.maxVisibleItems = 5;
                        }
                    }
                    x = select.data('buttonText');
                    if (typeof (x) != "undefined") {
                        self.options.buttonText = x;
                        if (!self.options.buttonText) {
                            self.options.buttonText = 'Show Options';
                        }
                    }
                    x = select.data('moreText');
                    if (typeof (x) != "undefined") {
                        self.options.moreText = x;
                        if (!self.options.moreText) {
                            self.options.moreText = 'More: refine search...';
                        }
                    }

                    x = select.data('showValueAsTitle');
                    if (typeof (x) != "undefined") {
                        self.options.showValueAsTitle = (x == "true");
                    }

                    //Create an text input
                    input = $("<input>")
                        .appendTo(wrapper)
                        .val(value)
                        .addClass("ui-state-default ui-combobox-input")
                    //make it autocomplete
                        .autocomplete({
                            delay: 0,
                            minLength: 0,
                            //define it's source:
                            source: function (request, response) {
                                $.ajax({
                                    url: self.options.url,
                                    dataType: self.options.datatype,
                                    data: {
                                        //what we are sending off:
                                        searchTerm: request.term
                                    },
                                    type: self.options.httpMethod,
                                    success: function (data) {
                                        //a bit wasteful, but
                                        //clear out the previous queries
                                        select.empty();

                                        $.each(data, function (key, val) {
                                            //If it comes back as an array rather than a dictionary:
                                            if (typeof (val) == "object") {
                                                key = val.key;
                                                val = val.val;
                                            }
                                            select.append($("<option/>")
                                                .attr("value", key)
                                                .text(val));
                                        });


                                        //Now that we have a new set of options
                                        //we use that as the local datastore to
                                        //search through.

                                        //define a regex filter to find the input within the select options
                                        var matcher = new RegExp($.ui.autocomplete.escapeRegex(request.term), "i");
                                        //for each select option, get the text(not value), 
                                        //test it, 
                                        //and if matched 
                                        //return response
                                        var o = select.children("option").map(function () {
                                            var text = $(this).text();
                                            if (this.value && (!request.term || matcher.test(text)))
                                                return {
                                                                                                  //Mke bold characters
                                                    label: text.replace(
                                                        new RegExp(
                                                            "(?![^&;]+;)(?!<[^<>]*)(" +
                                                                $.ui.autocomplete.escapeRegex(request.term) +
                                                                    ")(?![^<>]*>)(?![^&;]+;)", "gi"
                                                        ), "<strong>$1</strong>"),
                                                    value: text,
                                                    option: this
                                                };
                                        })
                                        //If there are too many items, truncate them or else
                                        //unwieldy.
                                        if (o.length > self.options.maxVisibleItems) {
                                            o = o.slice(0, self.options.maxVisibleItems - 1);
                                            //no way of marking the option as disabled.
                                            //And note that we are making objects, not li's or options:
                                            o.push({ label: self.options.moreText, value: "", option: null });
                                        }
                                        response(o);
                                    }
                                });
                            },
                            select: function (event, ui) {
                                if (!ui.item.option) {
                                    //if we are the last one (ie our 'more' text)
                                    //it has no option, so clear and get out.
                                                                     $(this).val("");
                                    select.val("");
                                                                     input.data("autocomplete").term = "";
                                } else {
                                    //otherwise set it:
                                    ui.item.option.selected =true;
                                    self._trigger("selected", event, {
                                        item: ui.item.option
                                    });
                                }
                            },
                            change: function (event, ui) {
                                if (!ui.item) {
                                    var matcher = new RegExp("^" + $.ui.autocomplete.escapeRegex($(this).val()) + "$", "i"),
                                        valid = false;
                                    select.children("option").each(function () {
                                        if ($(this).text().match(matcher)) {
                                            this.selected = valid = true;
                                            return false;
                                        }
                                    });
                                    if (!valid) {
                                        // remove invalid value, as it didn't match anything
                                        $(this).val("");
                                        select.val("");
                                        input.data("autocomplete").term = "";
                                        return false;
                                    }
                                }
                            }
                        })//input

                        .addClass("ui-widget ui-widget-content ui-corner-left")
                                              //hack to remove image that gives illusion of gray background
                                              .css('background', "white");

                    //render the drop down items
                    input.data("autocomplete")._renderItem = function (ul, item) {
                        var $item = $("<a></a>")
                            .html(item.label);
                        if ((self.options.showValueAsTitle) && (item.option)) {
                            $item.attr("title", item.option.value);
                        }

                        return $("<li></li>")
                            .data("item.autocomplete", item)
                            .append($item)
                            .appendTo(ul);
                    };

                    //Make a link button for the drop down button
                    //and append to end of wrapper (ie to right of input

                    $("<a>")
                        .attr("tabIndex", -1)
                        .attr("title", self.options.buttonTitle)
                        .appendTo(wrapper)
                        .button({
                            icons: {
                                primary: "ui-icon-triangle-1-s"
                            },
                            text: false
                        })
                        .removeClass("ui-corner-all")
                        .addClass("ui-corner-right ui-combobox-toggle")
                    //give a click handler that will show all current
                    //items without causing a fetch
                        .click(function () {
                            // close if already visible
                            if (input.autocomplete("widget").is(":visible")) {
                                input.autocomplete("close");
                                return;
                            }

                            // work around a bug (likely same cause as #5265)
                            $(this).blur();

                            // pass empty string as value to search for, displaying all results
                            input.autocomplete("search", "");
                            input.focus();
                        });
                },

                destroy: function () {
                    this.wrapper.remove();
                    this.element.show();
                    $.Widget.prototype.destroy.call(this);
                }
            });



//Initialize all combos on the page:
$(function () {
    $('.combobox').each(function () {
        //var url = $(this).data("url");
        $(this).combobox();
    });
});

In your layout:

<script>

            //Initialize all combos on the page:
            $(function () {
                $('select.combobox').each(function () {
                    //var url = $(this).data("url");
                    $(this).combobox();
                });
            });
</script>

Html:

<callout icon="true" type="class="ui-widget">
    <label>Your preferred programming language: </label>
    <select id="A" class="combobox" data-url="GetUserNameAndFQN">
    <!-- preload data if need be
        <option value="">Select one...</option>
        <option value="ActionScript">ActionScript</option>
        <option value="AppleScript">AppleScript</option>
        ...
        <option value="Scheme">Scheme</option>
        -->
    </select">
</callout>
I tangled with the AutoComplete one before in an ASP.NET app. It's a bit more complicated as ASP.NET XORs all the values as they go out -- and when you post back a value retrieved via Ajax, containg a value that was not in the XOR, it throws an exception.

>If I remember correctly one has to turn off page validation -- but the answer is somewhere in GTM2's code.

  • /home/skysigal/public_html/data/pages/it/ad/jquery/ui/autocomplete_combobox.txt
  • Last modified: 2023/11/04 01:46
  • by 127.0.0.1