JQuery UI Autocomplete - "This source is not a function"

0

I am trying to use the function autocomplete of JQuery UI to suggest words previously stored in localStorage .

The problem is that it does not suggest them but if you save them and in turn when I test in console it returns the array of objects.

This is the method that stores the data entered into localStorage.

guardarSugerencias:function(cityName)
{       
    app.ciudades.ciudades.push({nombre:cityName});  
    localStorage.setItem("ciudades",JSON.stringify(app.ciudades));
},

and he who is in charge of obtaining them.

obtenerSugerencias:function()
{
    var results = JSON.parse(localStorage.getItem("ciudades"));
    var search = $("#ciudad");
    $("#ciudad").on('focus',function()
    {
        search.autocomplete({
        source: results
    });     

    }); 
},

When I call the function obtenerSugerencias in the document.ready it gives me that error that says:

  

this source is not a function

    
asked by Ccccccccc 13.03.2017 в 18:43
source

1 answer

1

Problem:

  • The source option, when a array is indicated, you expect it to have one of the following formats:
  
  • A array of strings . Example ["Choice1", "Choice2"]
  •   
  • A% co_of% of objects with the array and label properties. Example: value
  •   

And actually you're passing an object to it: [{ label: "Choice1", value: "value1" }, ... ]

Solution:

Since you do not have more than the name of the city, it would be best to spend a {ciudades: [{nombre: ""},...]} .

Example:

obtenerSugerencias:function()
{
  var results = [];
  var cities = JSON.parse(localStorage.getItem("ciudades"));
  // Convertimos a un array de strings
  $.each(cities.ciudades, function(idx, city) {
    results.push(city.nombre);
  });

  var search = $("#ciudad");
  search.on('focus',function(){
    search.autocomplete({
    source: results //<-- pasamos un array de strings
  });     

  }); 
},
    
answered by 13.03.2017 в 19:23