Auto selecting first item of result list: implementation

Ivo Luiz Campagnolo's Avatar

Ivo Luiz Campagnolo

18 Nov, 2010 03:51 AM

Hey guys, I did a small implementation to support auto select of first element at results list:

1 - Add this function to jquery.autoSuggest.js:

`function setFirstItemSelected() {
    if ($(":visible", results_holder).length > 0) {
        var lis = $("li", results_holder);
        var start = lis.eq(0);
        var active = $("li.active:first", results_holder);

        if (active.length > 0) {
              start = active.next();
        }
        lis.removeClass("active");
        start.addClass("active");
    }
}`

2 - Create a new propertie called selectFirstDataItem, and init it with false at jquery.autoSuggest.js, like:

resultClick: function (data) { }, resultsComplete: function () { }, selectFirstDataItem: false

3 - Go to function ProcessData, and at end of function before opts.resultsComplete.call(this); add this command:

if (opts.selectFirstDataItem) {setFirstItemSelected();}

Ok guys, is it... Now you can init new propertie called selectFirstDataItem! Like:

$("#autosearch").autoSuggest(data.items, {asHtmlID: "12", selectedItemProp: "name", selectedValuesProp: "value", searchObjProps: "name", minChars: 2, matchCase: false, selectFirstDataItem: true, keyDelay: 0});

  1. 1 Posted by Doug Daniels on 22 Mar, 2011 08:10 PM

    Doug Daniels's Avatar

    This is a very cool addition to the plugin! However, I don't seem to be able to make it work.

    I added all the code from Ivo but it still doesn't select the first element. Anyone had any luck with this?

  2. 2 Posted by Ivo Luiz Campag... on 22 Mar, 2011 08:17 PM

    Ivo Luiz Campagnolo's Avatar

    Hey Doug.

    I will get a sample to you with a custom script ok? I will get it tonight and send to you!

    Regards!

  3. 3 Posted by mekn on 24 Mar, 2011 05:43 PM

    mekn's Avatar

    please attach it here

  4. 4 Posted by hostmax on 31 Mar, 2012 08:01 AM

    hostmax's Avatar

    Worked like a charm... Thanks a lot!

  5. 5 Posted by Goksel on 12 Apr, 2012 08:51 PM

    Goksel's Avatar

    Awesome man! Thanks!

  6. 6 Posted by Felix on 10 Apr, 2013 03:09 PM

    Felix's Avatar

    But "," selected item. I don't want it.!!!! please help me!

Reply to this discussion

Internal reply

Formatting help / Preview (switch to plain text) No formatting (switch to Markdown)

Attaching KB article:

»

Attached Files

You can attach files up to 10MB

If you don't have an account yet, we need to confirm you're human and not a machine trying to post spam.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac