0

i have country related data stored as json. i want to show country name from that json array. i use jquery autocomplete. this is my code which i tried but not working. give me some hint that where is the mistake.

<input type="text" class="form-control" id="plugins" name="plugins" />

var myData =[
    {"name": "Afghanistan"},
    {"name": "Åland Islands"},
    {"name": "Albania"},
    {"name": "Algeria"},
    {"name": "American Samoa"},
    {"name": "Bahamas"},
    {"name": "Bahrain"},
    {"name": "Bangladesh"},
    {"name": "Barbados"},
    {"name": "Belarus"},
    {"name": "Belgium"},
    {"name": "Belize"},
    {"name": "Benin"},
    {"name": "Cambodia"},
    {"name": "Cameroon"},
    {"name": "Canada"},
    {"name": "Cape Verde"},
    {"name": "Cayman Islands"},
    {"name": "Viet Nam"},
    {"name": "Virgin Islands, British"},
    {"name": "Virgin Islands, U.S."},
    {"name": "Wallis and Futuna"},
    {"name": "Western Sahara"},
    {"name": "Yemen"},
    {"name": "Zambia"},
    {"name": "Zimbabwe"}
]

$(function() {
  $("#plugins").autocomplete({
    source: function(req, resp) {
      var results = [];
      $.each(myData, function(k, v) {
        // Make a pass for names
        if (v.name.indexOf(req.term) != -1) {
          results.push(v);
          return;
        }
      });
      resp(results);
    }
  });
});

here is jsfiddle version https://jsfiddle.net/urtkxo46/2/

this is working version of jsfiddle https://jsfiddle.net/durga598/urtkxo46/5/

5
  • Do you need to use the function variant? If the list of options is fixed (as countries is) then the easiest solution is to update your list so it's simply an array of strings, then use source:myData rather than the function. the autocomplete UI will take care of the rest Commented Dec 6, 2017 at 10:15
  • can u please tell me which line i need to change to get the code working. Commented Dec 6, 2017 at 10:16
  • that said, I think the actual source of your error is that results.push(v) should be results.push(v.name) Commented Dec 6, 2017 at 10:16
  • @GPW thanks a lot your hint solve my problem jsfiddle.net/urtkxo46/3 Commented Dec 6, 2017 at 10:25
  • i use if (v.name.indexOf(req.term) != -1) it search any character but i want to search input char start with. how to do it with js? Commented Dec 6, 2017 at 10:26

2 Answers 2

1

In your result array you need to push results.push(v.name); to match case insensitive use toLowerCase() , so it will match all the values properly. You need to use source an array or string or a function returns object with key: label and value .

/*var myData = [{
  label: "xxx",
  value: "9997515744"
}, {
  label: "abc",
  value: "9619054073"
}, {
  label: "asd",
  value: "9323135708"
}];*/

var myData =[
	{"name": "Afghanistan"},
	{"name": "Åland Islands"},
	{"name": "Albania"},
	{"name": "Algeria"},
	{"name": "American Samoa"},
	{"name": "Bahamas"},
	{"name": "Bahrain"},
	{"name": "Bangladesh"},
	{"name": "Barbados"},
	{"name": "Belarus"},
	{"name": "Belgium"},
	{"name": "Belize"},
	{"name": "Benin"},
	{"name": "Cambodia"},
	{"name": "Cameroon"},
	{"name": "Canada"},
	{"name": "Cape Verde"},
	{"name": "Cayman Islands"},
	{"name": "Viet Nam"},
	{"name": "Virgin Islands, British"},
	{"name": "Virgin Islands, U.S."},
	{"name": "Wallis and Futuna"},
	{"name": "Western Sahara"},
	{"name": "Yemen"},
	{"name": "Zambia"},
	{"name": "Zimbabwe"}
]

$(function() {
  $("#plugins").autocomplete({
    source: function(req, resp) {
      var results = [];
      $.each(myData, function(k, v) {
        if (v.name.toLowerCase().startsWith(req.term.toLowerCase())) {
          results.push(v.name);
          return;
        }
      });
      //console.log(results)
      resp(results);
    }
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.js"></script>
<link href="https://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css" rel="stylesheet"/>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<input type="text" class="form-control" id="plugins" name="plugins" />

Sign up to request clarification or add additional context in comments.

8 Comments

Code only answers can get downvoted... you should add an explanation
i use if (v.name.indexOf(req.term) != -1) it search any character but i want to search input char start with. how to do it with js?
@Thomas That's why I am converting to lowercase. Check the snippet.
@Thomas do you need input character at first position only?
suppose when i type b then country name start with C is coming which i do not want. i test this if (v.name.toLowerCase().indexOf(req.term.toLowerCase()) != -1) { but did not work. still when i search with B then country name start with C is coming which is wrong.
|
0

According to the docs: [http://api.jqueryui.com/autocomplete/]

The result of the response function needs to be an array of strings, or an array of objects with label and value properties. You're returning an array of objects with a "name" property.

results.push(v) should be results.push(v.name) and then it'll be an array of strings so should work.

2 Comments

i use if (v.name.indexOf(req.term) != -1) it search any character but i want to search input char start with. how to do it with js?
can't you just use v.name.indexOf(req.term) === 0) ? that'll only be true if it matches at the start

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.