Bootstrap3 Typeahead - 在'Remote'中调用函数

时间:2013-10-30 22:11:28

标签: c# jquery twitter-bootstrap knockout.js twitter-bootstrap-3

我升级到Bootstrap 3.0版,我确实看到了typeahead模块不存在。我正在使用Web服务,我使用以下方法调用我的函数并填充我的数据集。但是,使用twitter typeahead.js,如何调用我的函数或如何使用旧的typeahead模块?非常感谢您的帮助。感谢。

            $("#searchVendor").typeahead({
            source: function (query) {
                    vieModel.callWebServiceFunctionList(counter1, query, isListCleared);   
       });

1 个答案:

答案 0 :(得分:0)

Typeahead.js无法直接将函数用作source。传递查询值的标准方法是使用%QUERY属性中包含remote的URL字符串:

$("#searchVendor").typeahead({
            remote: '.../data.json?name=%QUERY'   
});

但是,在您的情况下,这可能还不够。 remote也可以是一个对象,其中urlreplace函数应用于该网址。

因此,创建一个像callWebServiceFunctionList这样只返回URL而不是实际调用Web服务的函数。

$("#searchVendor").typeahead({
            remote: {
               url: '.../data.json?counter=%COUNTER&query=%QUERY&isListCleared=%ISLISTCLEARED',
               replace: function(url, query) {
                  return url.replace('%COUNTER', counter1).replace('%QUERY', query).replace('%ISLISTCLEARED', isListCleared);
               }
});
对于remote对象,

See the docs

或者,您可以只获取Bootstrap 2.x的typeahead部分的JS,虽然您可能会遇到格式化问题,it seems to work fine by itself(JSFiddle演示)。