twitter bootstrap typeahead ajax示例

eme*_*ava 277 jquery jquery-autocomplete jquery-ui-autocomplete twitter-bootstrap typeahead.js

我试图找到一个twitter bootstrap typeahead元素的工作示例,该元素将进行ajax调用以填充它的下拉列表.

我有一个现有的工作jquery自动完成示例,它定义了ajax url以及如何处理回复

<script type="text/javascript">
//<![CDATA[
$(document).ready(function() {
    var options = { minChars:3, max:20 };
    $("#runnerquery").autocomplete('./index/runnerfilter/format/html',options).result(
            function(event, data, formatted)
                {
                    window.location = "./runner/index/id/"+data[1];
                }
            );
       ..
Run Code Online (Sandbox Code Playgroud)

我需要更改什么来将其转换为typeahead示例?

<script type="text/javascript">
//<![CDATA[
$(document).ready(function() {
    var options = { source:'/index/runnerfilter/format/html', items:5 };
    $("#runnerquery").typeahead(options).result(
            function(event, data, formatted)
                {
                    window.location = "./runner/index/id/"+data[1];
                }
            );
       ..
Run Code Online (Sandbox Code Playgroud)

我将等待" 添加远程源支持预先输入 "问题得到解决.

Sti*_*ael 300

编辑:在bootstrap 3中不再捆绑typeahead.退房:

从Bootstrap 2.1.0到2.3.2,你可以这样做:

$('.typeahead').typeahead({
    source: function (query, process) {
        return $.get('/typeahead', { query: query }, function (data) {
            return process(data.options);
        });
    }
});
Run Code Online (Sandbox Code Playgroud)

要使用这样的JSON数据:

{
    "options": [
        "Option 1",
        "Option 2",
        "Option 3",
        "Option 4",
        "Option 5"
    ]
}
Run Code Online (Sandbox Code Playgroud)

请注意,JSON数据必须是正确的mime类型(application/json),因此jQuery将其识别为JSON.

  • 可以2.1使用json不仅仅是一个字符串数组?我需要为用户提供一个值并使用id进行进一步处理.没有拾取自定义分叉可能吗? (9认同)
  • 与Typeahead fork一样,数据必须是字符串的JSON数组,内容类型必须是application/json. (3认同)
  • @Stijin是否有任何关于如何使用匿名函数的例子来处理显示选项的id?谢谢! (2认同)
  • 为什么使用`get`而不是`getJSON`?这似乎更合适. (2认同)

bog*_*ert 118

您可以使用支持ajax调用的BS Typeahead fork.然后你就可以写:

$('.typeahead').typeahead({
    source: function (typeahead, query) {
        return $.get('/typeahead', { query: query }, function (data) {
            return typeahead.process(data);
        });
    }
});
Run Code Online (Sandbox Code Playgroud)

  • 我得到Uncaught TypeError:无法调用未定义的方法'toLowerCase' (23认同)
  • 很好的答案!我会使用GET请求,只是为了满足REST标准. (8认同)

Tha*_*ath 71

从Bootstrap 2.1.0开始:

HTML:

<input type='text' class='ajax-typeahead' data-link='your-json-link' />
Run Code Online (Sandbox Code Playgroud)

使用Javascript:

$('.ajax-typeahead').typeahead({
    source: function(query, process) {
        return $.ajax({
            url: $(this)[0].$element[0].dataset.link,
            type: 'get',
            data: {query: query},
            dataType: 'json',
            success: function(json) {
                return typeof json.options == 'undefined' ? false : process(json.options);
            }
        });
    }
});
Run Code Online (Sandbox Code Playgroud)

现在,您可以制作统一的代码,在您的HTML代码中放置"json-request"链接.

  • 但我会改为使用`$(this)[0].$ element.data('link')`. (11认同)
  • 这需要标记为新接受的答案. (8认同)

Jon*_*eck 48

所有响应都引用BootStrap 2 typeahead,它在BootStrap 3中不再存在.

对于在这里寻找使用新的post-Bootstrap Twitter typeahead.js的AJAX示例的其他人,这是一个工作示例.语法有点不同:

$('#mytextquery').typeahead({
  hint: true,
  highlight: true,
  minLength: 1
},
{
  limit: 12,
  async: true,
  source: function (query, processSync, processAsync) {
    processSync(['This suggestion appears immediately', 'This one too']);
    return $.ajax({
      url: "/ajax/myfilter.php", 
      type: 'GET',
      data: {query: query},
      dataType: 'json',
      success: function (json) {
        // in this example, json is simply an array of strings
        return processAsync(json);
      }
    });
  }
});
Run Code Online (Sandbox Code Playgroud)

此示例使用同步(对processSync的调用)和异步建议,因此您会看到一些选项立即出现,然后添加其他选项.你可以使用其中一个.

有许多可绑定事件和一些非常强大的选项,包括使用对象而不是字符串,在这种情况下,您将使用自己的自定义显示功能将项目呈现为文本.

  • 天哪!!!我在过去的3天里搜索没有人提到这一点.到目前为止,我正在测试同步功能.多谢,伙计!! (2认同)

Pau*_*lis 24

我用ajax功能增强了原始的typeahead Bootstrap插件.非常好用:

$("#ajax-typeahead").typeahead({
     ajax: "/path/to/source"
});
Run Code Online (Sandbox Code Playgroud)

这是github repo:Ajax-Typeahead

  • 您的服务器返回一个字符串而不是JSON对象.jQuery的$ .ajax()用于进行调用,它负责解析. (3认同)

小智 5

我对jquery-ui.min.js做了一些修改:

//Line 319 ORIG:
this.menu=d("<ul></ul>").addClass("ui-autocomplete").appendTo(d(...
// NEW:
this.menu=d("<ul></ul>").addClass("ui-autocomplete").addClass("typeahead").addClass("dropdown-menu").appendTo(d(...

// Line 328 ORIG:
this.element.addClass("ui-menu ui-widget ui-widget-content ui-corner-all").attr...
// NEW:this.element.attr....

// Line 329 ORIG:
this.active=a.eq(0).children("a")
this.active.children("a")
// NEW:
this.active=a.eq(0).addClass("active").children("a")
this.active.removeClass("active").children("a")`
Run Code Online (Sandbox Code Playgroud)

并添加以下css

.dropdown-menu {
    max-width: 920px;
}
.ui-menu-item {
    cursor: pointer;        
}
Run Code Online (Sandbox Code Playgroud)

工作完美.