3

I want to set Jquery UI plugin to search only from the beginning of the string. I find some simillar questions but no usable answer. I have this code:

$.ajax({
        url: "{!$basePath}/mesta.xml",
        dataType: "xml",
        success: function( xmlResponse ) {
            var data = $( "city", xmlResponse ).map(function() {
                return {
                    value: $( "name", this ).text(),
                    id: $( "name", this ).text()
                };
            }).get();
            $( ".autocomplete" ).autocomplete({
                source: data,
                minLength: 0
            });
        }
    });

Thank you.

Darkry
  • 590
  • 2
  • 12
  • 26

1 Answers1

4

Just apply what is in the link @Samich posted to your source option:

source: function(req, response) { 
    var re = $.ui.autocomplete.escapeRegex(req.term); 
    var matcher = new RegExp( "^" + re, "i" ); 
    response($.grep( data, function(item){ 
        return matcher.test(item.value); }) ); 
 },

And here is a fiddle demo: http://jsfiddle.net/jensbits/PekQZ/

jk.
  • 14,365
  • 4
  • 43
  • 58