Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery autocomplete auto fill field with 1st value and highligh added part

im using jqueryui autocomplete plugin with following code

$(this).autocomplete({
                source: function(request, response) {
                    $.ajax({ url: 'clinic/auto',
                    data: { 'term': this.term,'name': this.element.attr('complete')},
                    dataType: "json",
                    type: "POST",
                    success: function(data){
                        response(data);
                    }
                });
            },
            minLength: 2
        });

This display a list of all results in a dropdown.

my question is how do i get it to autocomplete work for u and highlight the added part for easier use?

do i have to code it ? or there is an already existing option for that? if i hv to do it manual how can it be done ? example pic: enter image description here

Solutions so far:

I have found this link and thisto be very usefull (monkey-patching jquery-autocomplete) to edit styling ,yet still not what i want..

like image 772
Zalaboza Avatar asked Dec 27 '12 23:12

Zalaboza


Video Answer


2 Answers

Unfortunately, there is not an existing option for it. Fortunately, there's a pretty straightforward way to do it using the events supplied with JQuery Autocomplete. Check out this JSFiddle: http://jsfiddle.net/RyTuV/133/

As you'll notice, the relavant code you want to add uses the JQuery Autocomplete Open event:

Triggered when the suggestion menu is opened or updated.

Using this event, you can add the text of the first item in the list to what is already typed into the input filed, and then highlight from after the input text to the end of the added text:

$(this).autocomplete({
    source: function(request, response) {
                $.ajax({ url: 'clinic/auto',
                data: { 'term': this.term,'name': this.element.attr('complete')},
                dataType: "json",
                type: "POST",
                success: function(data){
                    response(data);
                }
            });
    },
    minLength: 2,
    open: function( event, ui ) {
        var firstElement = $(this).data("autocomplete").menu.element[0].children[0]
           , inpt = $('#autocomplete')
           , original = inpt.val()
           , firstElementText = $(firstElement).text();

        /*
           here we want to make sure that we're not matching something that doesn't start
           with what was typed in 
        */
        if(firstElementText.toLowerCase().indexOf(original.toLowerCase()) === 0){
            inpt.val(firstElementText);//change the input to the first match

            inpt[0].selectionStart = original.length; //highlight from end of input
            inpt[0].selectionEnd = firstElementText.length;//highlight to the end
        }
    }
});

Using this as a template, you could loop though the items in the menu to find the first match that starts with the input text, and use that to fill in and highlight, instead of only using the first item.

like image 91
Rick Hanlon II Avatar answered Oct 06 '22 00:10

Rick Hanlon II


You'll need to code it yourself or see if there is a different autocomplete plugin that does this.

To code it yourself you will need to reference the response event to get the first matching result text and place it in the input box. To manipulate the selected text see this post: Selecting Part of String inside an Input Box with jQuery

like image 30
ryan Avatar answered Oct 05 '22 22:10

ryan