views:

38

answers:

2

I have a textbox where i want to have an autocomplete that lets the user search through addresses. The user must be able to type in different words and the autocomplete must search through them to narrow its list.

I've been trying and reading the documentation, but nothing seems to do the trick as it always searches on the whole string instead of the words. Am i missing something?

Example:

When the user enters 'Mathias Antwerp' he must see all the addresses that contain those words. In the example it must show 1 row which is the second one.

<script>
var addresses = [
    { name: "Frederick Dereave Gentstreet 4 Gent" },
    { name: "Mathias Derian Meilaan 9 Antwerp" },
    { name: "Mathias Hors frelaan 5 Kortrijk" }
];    

$(document).ready(SetAutoComplete);

function SetAutoComplete() {

    $("#testveld").autocomplete(emails,
        {
            matchContains: "word"
        }
    );
}
</script>
<input type="text" id="testveld" style='width:300px'/>
A: 

AFAIK, you will have to to do some processing on your own to parse the string into words. You can do this using jquery or if you plan to get the addresses from server side then use some server side language.

ShiVik
i cannot force the autocomplete to show values that don't match his own logic, or can i?
MichaelD
@MichaelD - not unless you add some of your own logic into the jquery-ui code, which I wouldn't recommend.
ShiVik
+1  A: 

I altered the code of matchSubset in jquery.autocomplete.js which enables the behavior i was looking for.

function matchSubset(s, sub) {

    var arraySub=sub.split(" ");

    if (!options.matchCase) 
        s = s.toLowerCase();
    var i = s.indexOf(sub);
    if (options.matchContains == "word"){
        i = s.toLowerCase().search("\\b" + sub.toLowerCase());
    }

    //addition for split words
    if (options.matchContains == "splittedword"){
        for(itemindex=0;itemindex<arraySub.length;itemindex++){

            i = s.toLowerCase().search(arraySub[itemindex].toLowerCase());
            if(i==-1){
                break;
            }
        }
    }

    if (i == -1) return false;
    return i == 0 || options.matchContains;
};
MichaelD