0

I am working on autocomplete component from jquery UI. Though my auto complete working fine but If I type suggested letter "I" it was showing all the list which is available from json data where i need only the relevant letter for example if I type letter I "India", Indonesia etc,. And I need to show only max 10 values in the list. Here I have updated the latest code I am trying to do the slice in the autocomplete and i am trying to getting the same value in the next text box.

Here is the current jquery code

$("#ipt_Countryres").autocomplete({
 source: function( request, response ) {
      var regex = new RegExp(request.term, 'i');
      //var filteredArray = filteredArray.slice(0,10);
    $.ajax({
        url: "json/countries.json",
        dataType: "json",
        data: {term: request.term},
        success: function(data) {
            response($.map(data, function(item) {
                if(regex.test(item.label)){
                    return {
                        id: item.id,
                        label: item.label,  
                        value: item.value
                    };
                }
            }));
        },
        minlength:2,
        select: function (event, ui) {
           $("#get_country").val(ui.item.label);               
       }
    });
}

});

Here is the HTML Code

<input type="text" id="ipt_Countryres" name="ipt_Countryres" class="ipt_Field"/>
<input type="text" class="ipt_Field" id="get_country" name="get_country"/ disabled>

Here is my sample JSON data

[  
   {  
    "value":"Afghanistan",
     "label":"Afghanistan",
     "id":"AF"
   },
   {  
      "value":"Åland Islands ",
     "label":"Åland Islands",
     "id":"AX"
   },
   {  
     "value":"Albania ",
     "label":"Albania",
     "id":"AL"
   },
  {  
     "value":"Algeria ",
     "label":"Algeria",
     "id":"DZ"
  },
  {  
    "value":"American Samoa ",
    "label":"American Samoa",
    "id":"AS"
  },
  {  
     "value":"AndorrA ",
    "label":"AndorrA",
    "id":"AD"
  },
  {  
     "value":"Angola ",
     "label":"Angola",
     "id":"AO"
  },
  {  
     "value":"Anguilla ",
     "label":"Anguilla",
     "id":"AI"
  },
  {  
     "value":"Antarctica ",
     "label":"Antarctica",
     "id":"AQ"
  },
  {  
     "value":"Antigua and Barbuda ",
    "label":"Antigua and Barbuda",
     "id":"AG"

}]

Kindly please help me.

Thank in advance

Mahadevan

Mr.M
  • 1,472
  • 3
  • 29
  • 76
  • You may have to look at following SO: [http://stackoverflow.com/questions/3148195/jquery-ui-autocomplete-use-startswith][1] [1]: http://stackoverflow.com/questions/3148195/jquery-ui-autocomplete-use-startswith – vijayP Aug 24 '15 at 08:10

4 Answers4

1

Try to add below code for filtering the values with start with regex,

$.ui.autocomplete.filter = function (array, term) {
   var matcher = new RegExp("^" + $.ui.autocomplete.escapeRegex(term), "i");
   return $.grep(array, function (value) {
     return matcher.test(value.label || value.value || value);
   });
};

See more jqueryui-autocomplete-filter-words-starting-with-term

Rohan Kumar
  • 40,431
  • 11
  • 76
  • 106
  • Hi @rohan i don't want only stat letter with i that is for an example but for example if i type any other letter i it has to show the relevant data and one small request how to get the label value eample india in the first text box (ipt_Countryres) though i am getting the value in the next text box but in the first text box i am getting code example IN but i don't want IN I want INDIA kindly please help me – Mr.M Aug 24 '15 at 08:16
  • hi @rohan the maximum values was not working kindly please help me – Mr.M Aug 24 '15 at 10:37
1

Try this:

var orignalArray = [  
  {  
    "id":"Afghanistan",
    "label":"Afghanistan",
     "value":"AF"
  },
  {  
  "id":"Åland Islands ",
  "label":"Åland Islands",
  "value":"AX"
   },
 {  
    "id":"Albania ",
    "label":"Albania",
    "value":"AL"
 }]
$("#ipt_Countryres").autocomplete({
   minLength:1,
    source: function(request, response) {
    var filteredArray = $.map(orignalArray, function(item) {
        if( item.id.startsWith(request.term)){
            return item;
        }
        else{
            return null;
        }
    });
  
  filteredArray = filteredArray.slice(0,10);
  
    response(filteredArray);
},
   select: function(event, ui) {
     event.preventDefault();
     // Prevent value from being put in the input:
     $('#ipt_Countryres').val(ui.item.label);
     $('#get_country').val(ui.item.label);
     // Set the next input's value to the "value" of the item.
   },
  
  focus: function(event, ui) {
        event.preventDefault();
         $('#ipt_Countryres').val(ui.item.label);
    }
  
});
<link href="http://api.jqueryui.com/jquery-wp-content/themes/jquery/css/base.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.8.24/jquery-ui.min.js"></script>


<input type="text" id="ipt_Countryres" name="ipt_Countryres" class="ipt_Field"/>
<input type="text" class="ipt_Field" id="get_country" name="get_country"/ disabled>
vijayP
  • 11,432
  • 5
  • 25
  • 40
  • hi @vijayP I cannot able to see the output kindly help me – Mr.M Aug 24 '15 at 08:17
  • hi @vijayP I have updated the code but still not working please help me – Mr.M Aug 24 '15 at 08:27
  • hi @ vijay is that possible the above code will work for json data if yes how to do that please help me – Mr.M Aug 24 '15 at 08:36
  • sorry vijay to disturb you lot but in your code inside source your have function i am bit confused :) – Mr.M Aug 24 '15 at 08:39
  • Hello Mahadevan, yes..its true. We can have JS function as our `source:`. Please check http://api.jqueryui.com/autocomplete/#option-source. Above code can be used with JSON being fetched from server. Just add `$.ajax` call from within this `source:` function and get your `var filteredArray` populated from your server response. Ultimate aim is to call `response(filteredArray);` so that we can see newer values in auto complete suggestion. – vijayP Aug 24 '15 at 08:46
  • Hello vijay the maximum number it was not working in the jquery autocomplete kindly please help me – Mr.M Aug 24 '15 at 10:37
  • JQuery UI autocomplete don't have any predefined configuration for setting max number suggestions to be displayed to end user. However you can use javascript `slice` function to achieve your goal. In our code `filteredArray = filteredArray.slice(0,10);` will gives you back first 10 result from filteredArray. Hope this will help you. – vijayP Aug 24 '15 at 10:42
1

You can reduce the minLength for getting more results:

$("#ipt_Countryres").autocomplete({
    source: country_list,
    minLength: 3,
    max:10,
    select: function (event, ui) {
        // Prevent value from being put in the input:
        $('#ipt_Countryres').val(ui.item.label);
        $('#get_country').val(ui.item.label);
        // Set the next input's value to the "value" of the item.
    }
});

Please find below the running code :

JS Fiddle : http://jsfiddle.net/vafus4qb/

Nikhil Maheshwari
  • 2,198
  • 18
  • 25
  • Hi @Nikhil I want something like both the text box should get the label and not the value Kindl please help me – Mr.M Aug 24 '15 at 08:34
0

Thank you so much for your suggestion.

Here is my final output which i get right now.

$("#ipt_Countryres").autocomplete({
highlightClass: "bold",
 source: function( request, response ) {

      var regex = new RegExp(request.term, 'i');

      //var filteredArray = filteredArray.slice(0,10);
    $.ajax({
        url: "json/countries.json",
        dataType: "json",
        data: {term: request.term},
        success: function(data) {
            response($.map(data, function(item) {
                if(regex.test(item.label)){
                    return {
                        id: item.id,
                        label: item.label,  
                        value: item.value
                    };
                }
            }));
        }  
    });
},
minlength:3,
select: function (event, ui) {
    $("#get_country").val(ui.item.label);
}

});

Thanks & Regards Mahadevan

Mr.M
  • 1,472
  • 3
  • 29
  • 76