【问题标题】:Twitter's typeahead example / dynamic queryTwitter 的 typeahead 示例/动态查询
【发布时间】:2013-10-02 01:12:58
【问题描述】:

我正在寻找twitter's typeahead 的示例。

我想通过函数获取数据,我应该使用远程吗? 从服务器返回的数据将是一个 Json(而不仅仅是一个数组)。 从我只想显示字段的那些中,例如name.

选择正确的name 后,我想运行另一个功能,例如提醒该对象的iddescription(字段)。可能是typeahead:autocompleted 事件绑定器?

更新/示例:

Dajaxice.async.get_closest_events() 波纹管将 3 个参数发送到内部服务器(lat、lng、query)。查询是用户写入的值。它返回一个数组 (venuesNames),该数组将显示在下拉列表中。

$( "#locationQueryInput" ).typeahead({
    remote:{
        replace: function (query ) {
            //alert($("locationQueryInput").val());
            Dajaxice.async.get_closest_events( 
                (function(data){                                            
                    venuesNames = []
                    venuesDetails = []
                    for (var i = 0; i < data.fVenues.length; i++) {
                        venuesNames.push(data.fVenues[i].name)
                        venuesDetails[ data.fVenues[i].name ] = {
                                                'id' : data.fVenues[i].id,
                                                'lat' : data.fVenues[i].lat,
                                                'lng' : data.fVenues[i].lng,
                                                'address' :data.fVenues[i].address,
                                                'city' :data.fVenues[i].city,
                        }
                    }
                    return venuesNames
                }), 
                {'lat' : new_marker_latlng.lat, 'lng' : new_marker_latlng.lng, 'query' : query }  
            );
        }


    }

}).bind('typeahead:selected', function(obj,datum){
// do stuff upon completion
...
}

【问题讨论】:

    标签: javascript jquery twitter typeahead typeahead.js


    【解决方案1】:

    这是一个使用 json 响应的好例子:How to render JSON response using latest typeahead.js library

    然后要绑定事件,您可以将上述建议与以下内容结合起来:

    $('.selector').typeahead({
        // base this part on example link given above...
    }).bind('typeahead:autocompleted', function (obj, datum) {
        console.log(datum);  // datum will contain the value that was autocompleted
    });
    

    更新 1: 关于查询参数,您的函数调用编写不正确。根据文档:

    replace – 带有签名的函数 replace(url, uriEncodedQuery) 可用于覆盖请求 URL。预计返回一个 有效的网址。如果设置,则不会对 url 执行通配符替换。

    所以你需要传递 URL 作为第一个参数,查询字符串作为第二个参数:

    remote:{
        replace: function (url, query ) {
        }
    }
    

    【讨论】:

    • 问题,如何获取查询输入值? $('input').val() 由于 html 布局而无法正常工作。
    • 我已经更新了我的答案,以展示如何在 'typeahead:autocomplete' 事件处理程序中获取值。请注意,当有人明确单击下拉列表中的项目时,您可能还希望捕获 'typeahead:selected' 事件。
    • 感谢您的努力,我的意思是如何在服务器请求中获取查询。我用到目前为止的示例代码更新了我的问题。
    猜你喜欢
    • 2012-03-03
    • 2015-03-22
    • 2014-05-23
    • 1970-01-01
    • 1970-01-01
    • 2017-09-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多