Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Typeahead.js TwitterTypeahead显示所有可用值,而不仅仅是匹配值_Typeahead.js - Fatal编程技术网

Typeahead.js TwitterTypeahead显示所有可用值,而不仅仅是匹配值

Typeahead.js TwitterTypeahead显示所有可用值,而不仅仅是匹配值,typeahead.js,Typeahead.js,我已经尝试在web应用程序中应用twitters typeahead插件。我使用typeahead插件初始化了许多typeahead输入字段,这似乎是可行的。插件被激活了。但是,Id似乎与我正在编写的查询字符串及其选择显示的值不匹配。例如,如果我选择写'Nuu',那么它还将显示前3个字母完全不同的值 我的html如下所示: $(selector).each(function(){ var jsonUrl = $(this).attr('data-json'); $(this).typ

我已经尝试在web应用程序中应用twitters typeahead插件。我使用typeahead插件初始化了许多typeahead输入字段,这似乎是可行的。插件被激活了。但是,Id似乎与我正在编写的查询字符串及其选择显示的值不匹配。例如,如果我选择写'Nuu',那么它还将显示前3个字母完全不同的值

我的html如下所示:

$(selector).each(function(){
  var jsonUrl = $(this).attr('data-json');
    $(this).typeahead({
      name : 'berths',
      remote : {
      url : jsonUrl,
      filter : myfilter
    }
  });
});


myfilter = function(parsedResponse) {
  var datums = $.map(parsedResponse, function(berth) {
    var datum = {
      name : berth.name,
      value : berth.name + (berth.alias ? " (" + berth.alias + ")" : ""),
      tokens : [ berth.name, berth.alias ],
      latitude : berth.latitude,
      longitude : berth.longitude
    };

    return datum;
  });

  return datums;
};

我的JavaScript如下所示:

$(selector).each(function(){
  var jsonUrl = $(this).attr('data-json');
    $(this).typeahead({
      name : 'berths',
      remote : {
      url : jsonUrl,
      filter : myfilter
    }
  });
});


myfilter = function(parsedResponse) {
  var datums = $.map(parsedResponse, function(berth) {
    var datum = {
      name : berth.name,
      value : berth.name + (berth.alias ? " (" + berth.alias + ")" : ""),
      tokens : [ berth.name, berth.alias ],
      latitude : berth.latitude,
      longitude : berth.longitude
    };

    return datum;
  });

  return datums;
};

我做错了什么,有什么线索吗?

您是否缺少
jsonUrl
上的查询数据?TypeAhead需要URL中的替换字符串替换为字段中键入的数据,因此URL应如下所示:

/my/data/source/?text=%QUERY
其中,
%查询
替换为输入字段内容


请注意,
%QUERY
是默认值;你应该可以使用任何你想要的字符串,但我还不能让它工作。

虽然typeahead.js会过滤给定查询的本地和预取数据,但它不会过滤远程数据。typeahead.js的工作假设是,对于远程数据,过滤是在服务器端完成的

对于您的用例来说,使用预取可能是一个更好的选择:

$(selector).typeahead({
  name : 'berths',
  prefetch : {
    url : jsonUrl,
    filter : myfilter
  }
});

谢谢我以前尝试过使用预回迁,但由于另一个错误而从未执行过预回迁。这让我有点困惑。然而,我现在使用预回迁和远程的结合,这满足了我的需要。