简体   繁体   English

Twitter Typeahead:未定义

[英]Twitter Typeahead: undefined

I have a .json file with data and I want to use Twitter's typeahead for the locations of railway stations in the .json file. 我有一个带有数据的.json文件,并且我想使用Twitter的typeahead作为.json文件中火车站的位置。 (See my jsFiddle) When I use the current code, I run into errors. (请参阅我的jsFiddle)当我使用当前代码时,我遇到了错误。 On display, when I start typing, the results from typeahead is "undefined" for every record. 在显示中,当我开始键入内容时,每条记录的预输入结果都是“未定义”。

The problem is in the source method: 问题出在源方法中:

source: function(query, process){
                $.getJSON("../data/stations.json", function(data){
                    $.each(data.Stations.Station, function(key,val){
                        if(val.Land == "NL"){
                            stationsnamen.push(val['Namen'].Lang.toString());
                            stationsdata.push(new Array(val.Lat, val.Lon, val['Namen'].Lang.toString()));
                        }
                    });
                });
                process(stationsnamen);
            }

I can't seem to find why it is returning "undefined", as process(array) in the first-to-last line of code above gets the same data, compared to having a separate method. 我似乎找不到它为什么返回“未定义”的原因,因为与使用单独的方法相比,上述代码的第一至最后一行中的process(array)获取相同的数据。

Current code 当前代码

Example JSON JSON范例

Suggestions in typeAhead should be an object rather than plain strings. typeAhead中的建议应该是一个对象,而不是纯字符串。

You have setup typeAhead to display 'value' property of object 您已经设置typeAhead来显示对象的“值”属性

displayKey: 'value'

The easiest way to solve this problem is to wrap source strings into JavaScript objects. 解决此问题的最简单方法是将源字符串包装到JavaScript对象中。

Like this: 像这样:

stationsnamen.push({value: val['Namen'].Lang.toString() });

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

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