简体   繁体   English

jQuery自动完成与外部JSON源

[英]jquery autocomplete with external json source

I have an autocomplete function that was working with a local json source. 我有一个与本地json源一起使用的自动完成功能。 Given that it's 16k lines of code, I want to move this to an external json file. 鉴于这是16k行代码,我想将其移至外部json文件。 However I can't seem to get it working with the external source file. 但是我似乎无法使其与外部源文件一起使用。 Can anyone point me in the right direction? 谁能指出我正确的方向? At the moment this code does not work, but also does not return any errors to the console. 目前,此代码不起作用,但也不会向控制台返回任何错误。

Autocomplete script 自动完成脚本

$(function() {
  $.ajax({
    url: "javascripts/airports.json",
    dataType: "json",
    success: function(request, response) {
             var data = $.grep(suggestion, function(value) {
               return value.city.substring(0, request.term.length).toLowerCase() == request.term.toLowerCase();
             });
  $('#autocomplete').autocomplete({
  minLength: 1,
  source: data,
  focus: function(event, ui) {
              $('#autocomplete').val(ui.item.city,ui.item.country);
              return false;
    },
  select: function(event, ui) {
    $('#autocomplete').val(ui.item.name);
      return false;
      }
      }).data( "ui-autocomplete" )._renderItem = function( ul, item ) {
        return $( "<li></li>" )
          .data( "ui-autocomplete-item", item )
            .append( "<a>" + item.city + "," + item.country + "</a>" )
              .appendTo( ul );
          };
        }
    });
  });

External data structure 外部数据结构

var suggestion =
  [
  {"id":"1","name":"Goroka","city":"Goroka","country":"Papua New Guinea","iata":"GKA","icao":"AYGA","latitude":"-6.081689","longitude":"145.391881","altitude":"5282","timezone":"10","dst":"U","tz":"Pacific/Port_Moresby"}
  ,
  {"id":"2","name":"Madang","city":"Madang","country":"Papua New Guinea","iata":"MAG","icao":"AYMD","latitude":"-5.207083","longitude":"145.7887","altitude":"20","timezone":"10","dst":"U","tz":"Pacific/Port_Moresby"}
  ,
  {"id":"3","name":"Mount Hagen","city":"Mount Hagen","country":"Papua New Guinea","iata":"HGU","icao":"AYMH","latitude":"-5.826789","longitude":"144.295861","altitude":"5388","timezone":"10","dst":"U","tz":"Pacific/Port_Moresby"}
  ,
  {"id":"4","name":"Nadzab","city":"Nadzab","country":"Papua New Guinea","iata":"LAE","icao":"AYNZ","latitude":"-6.569828","longitude":"146.726242","altitude":"239","timezone":"10","dst":"U","tz":"Pacific/Port_Moresby"}
  ,
  {"id":"5","name":"Port Moresby Jacksons Intl","city":"Port Moresby","country":"Papua New Guinea","iata":"POM","icao":"AYPY","latitude":"-9.443383","longitude":"147.22005","altitude":"146","timezone":"10","dst":"U","tz":"Pacific/Port_Moresby"}
]

Your response should be a JSON object (array) where each item is an object with id , label and value keys. 您的响应应该是一个JSON对象(数组),其中每个项目都是具有idlabelvalue键的对象。

The items in your json files doesn't have the label and value keys, so the autocomplete can't really show them. json文件中的项目没有labelvalue键,因此自动完成功能无法真正显示它们。

Best solution - change the content of the file/response to follow the id/label/value structure. 最佳解决方案-更改文件/响应的内容以遵循id/label/value结构。

If you can't do this - you can use the _renderItem function to create your own items in the autocomplete based on the content of the json file: 如果您不能执行此操作,则可以使用_renderItem函数根据json文件的内容在自动完成功能中创建自己的项目:

$('#autocomplete').autocomplete({
    ...
    _renderItem: function( ul, item ) {
        return $( "<li>" )
            .append( item.name )
            .appendTo( ul );
    }
    ...
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM