r3340 committed - autocomplete: improved select example to always show all options when ...

r3340 committed - autocomplete: improved select example to always show all options when ...


Revision: 3340
Author: joern.zaefferer
Date: Wed Sep 30 14:39:46 2009
Log: autocomplete: improved select example to always show all options when
using the button
http://code.google.com/p/jquery-ui/source/detail?r=3340
Modified:
/branches/dev/tests/visual/autocomplete/select.html
=======================================
--- /branches/dev/tests/visual/autocomplete/select.html    Tue Sep 29 15:29:20
2009
+++ /branches/dev/tests/visual/autocomplete/select.html    Wed Sep 30 14:39:46
2009
@@ -20,12 +20,15 @@
                var select = this.element.hide();
                var input = $("<input/>").addClass("ui-widget ui-widget-content
ui-corner-left").insertAfter(select).autocomplete({
                    source: function(request) {
+                        var matcher = new RegExp(request.term, "i");
                        return select.children("option").map(function() {
-                            return {
-                                id: $(this).val(),
-                                label: $(this).text().replace(new RegExp("(?![^&;]+;)(?!<[^<>]*)("
+ request.term.replace(/([\^\$\(\)\[\]\{\}\*\.\+\?\|\\])/gi, "\\$1")
+ ")(?![^<>]*>)(?![^&;]+;)", "gi"), "<strong>$1</strong>"),
-                                result: $(this).text()
-                            };
+                            var text = $(this).text();
+                            if (!request.term || matcher.test(text))
+                                return {
+                                    id: $(this).val(),
+                                    label: text.replace(new RegExp("(?![^&;]+;)(?!<[^<>]*)(" +
request.term.replace(/([\^\$\(\)\[\]\{\}\*\.\+\?\|\\])/gi, "\\$1")
+ ")(?![^<>]*>)(?![^&;]+;)", "gi"), "<strong>$1</strong>"),
+                                    result: text
+                                };
                        });
                    },
                    delay: 0,
@@ -51,7 +54,10 @@
                // TODO remove/replace once button is finished
                .addClass("ui-corner-right ui-button-icon").css("margin-left", -1)
                .click(function() {
-                    input.autocomplete("search");
+                    // need to temporarily remove value for search to display all options
+                    // find a better solution to avoid the flashing
+                    var val = input.val();
+                    input.val("").autocomplete("search").val(val);
                    input.focus();
                });
            }