Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Patch: Drop down suggestions (part 2) #44

Closed
PrinceCuberdon opened this issue Feb 10, 2013 · 4 comments
Closed

Patch: Drop down suggestions (part 2) #44

PrinceCuberdon opened this issue Feb 10, 2013 · 4 comments

Comments

@PrinceCuberdon
Copy link

I use your library for my web site and I needed a way to display the whole suggestions because I pre fill the auto completion.

The patch don't alter the rest of the library.

to use this function use $("myelement").autocomplete('display');

Best regards and thanks for you work (the bug is corrected)

The patch (don't want to fork the project):

diff --git a/src/jquery.autocomplete.js b/src/jquery.autocomplete.js
index 245b3b1..ae9271d 100644
--- a/src/jquery.autocomplete.js
+++ b/src/jquery.autocomplete.js
@@ -610,6 +610,35 @@
             }

             return currentValue.substr(0, currentValue.length - parts[parts.length - 1].length) + value;
+        },
+
+        display: function() {
+            var that = this,
+                className = that.classes.suggestion,
+                suggestionSelector = '.' + that.classes.suggestion,
+                container = $(that.suggestionsContainer),
+                html = '';
+                
+            that.suggestions = [];
+            
+            $.each(that.options.lookup, function(i, suggestion) {
+                that.suggestions.push(suggestion.value);
+                html += '<div class="' + className + '" data-index="' + i + '">' + suggestion.value + '</div>';
+            });
+            
+            container.html(html).show();
+            that.visible = true;
+
+            // reinstall handler
+            container.on('click', suggestionSelector, function () {
+                that.el.val(that.suggestions[$(this).data('index')]);
+            });
+
+            // Select first value by default:
+            if (that.options.autoSelectFirst) {
+                that.selectedIndex = 0;
+                container.children().first().addClass(classSelected);
+            }
         }
     };
@tkirda
Copy link
Member

tkirda commented Feb 11, 2013

So it would act like a dropdown?

@PrinceCuberdon
Copy link
Author

On 12/02/2013 01:04, Tomas Kirda wrote:

So it would act like a dropdown?


Reply to this email directly or view it on GitHub
#44 (comment).

Yes

BR

Prince Cuberdon

@riccardolardi
Copy link

Hi, I can't seem to be able to use this, I've applied the patch correctly but it still acts the same as before, and I can't just call $('#input').autocomplete('display'); as I have to pass the array with possible values and other options, how do I call this correctly? I'm currently doing this:

    $('input.search-filter').autocomplete({
        lookup: testBrands,
        onSelect: function(suggestion) {
            alert('You selected: ' + suggestion.value + ', ' + suggestion.data);
        }
    });

@riccardolardi
Copy link

Wouldn't this be a lot easier or am I missing something? Just call it from onFocus and when the user deletes the value inside the input field

    showAllSuggestions: function() {
        var that = this,
            allSuggestions;

        allSuggestions = that.getSuggestionsLocal(that.getQuery(''));

        that.suggestions = allSuggestions.suggestions;

        that.suggest();

    }

@tkirda tkirda closed this as completed Nov 23, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants