【问题标题】:how to use json on twitter typeahead如何在 twitter typeahead 上使用 json
【发布时间】:2015-03-11 19:07:38
【问题描述】:

我有一个这样的 json:

{"num": ["test", "test group event"]}

我想用typeahead.js, 但是他们的json是这样的:

["Andorra","United Arab Emirates"]

如何在下面的示例中使用我的 json?

<script type='text/javascript'>//<![CDATA[ 
    $(window).load(function(){
        var countries = new Bloodhound({
            datumTokenizer : Bloodhound.tokenizers.obj.whitespace('name'),
            queryTokenizer : Bloodhound.tokenizers.whitespace,
            limit : 10,
            prefetch : {
                url    : 'http://127.0.0.1:8000/mysearch/',
                filter : function(list) {
                    return $.map(list, function(country){ 
                        return { 
                            name : country 
                        };
                    });
                }
            }
        });
        countries.initialize();
        $('#prefetch .typeahead').typeahead(null, {
            name : 'countries',
            displayKey : 'name',
            source : countries.ttAdapter()
        });
    });//]]>  
</script>

更新:

我的 json 来自服务器(http://rroyales.webfactional.com/mysearch/1.json),所以如果可能的话,我认为我们必须使用预取来从服务器获取 json。我不想使用 getjson 将其捕获到变量中,还是有更简单的方法将其捕获到变量中?

更新 2:

what is wrong with my json coming from haystack - 创建了一个与此相关的新问题

【问题讨论】:

    标签: javascript jquery json typeahead.js


    【解决方案1】:

    更新

    你的猎犬应该是这样的

    var dataSource = new Bloodhound({
        datumTokenizer: Bloodhound.tokenizers.obj.whitespace('item'),
        queryTokenizer: Bloodhound.tokenizers.whitespace,
        prefetch: {
            url: "http://jsbin.com/nixalofara/1.json",
            filter: function (data) {
                return $.map(data['num'], function (item) {
                    return {
                        item: item
                    };
                });
            }
        }
    });
    

    像这样提前输入

    $('.typeahead').typeahead({
        highlight: true
    }, {
        displayKey: 'item',
        source: dataSource.ttAdapter()
    });
    

    这是demo


    `var data = {"num": ["one", "two"]};` 对于上述数据,这就是你的猎犬的样子 var 猎犬 = 新的猎犬({ datumTokenizer: Bloodhound.tokenizers.obj.whitespace('value'), queryTokenizer: Bloodhound.tokenizers.whitespace, 本地:$.map(data['num'], function(item) { return { value: item }; }) }); 像这样提前输入 $('.typeahead').typeahead({ 亮点:真 }, { 来源:bloodhound.ttAdapter() }); 希望这可以帮助。

    【讨论】:

    【解决方案2】:

    由于您的数组是较大的 json 对象中的一个属性,我很确定您可以这样做:

    var myJson = {"num": ["one", "two"]}; //capture your json object in a variable
    var myArray = myJson.num; //myArray will now equal ["one", "two"]
    

    然后,在您列出的代码底部附近,您需要相应地更新 typeahead 调用。

    【讨论】:

    • 我的 json 来自服务器(127.0.0.1:8000/mysearch/json)。查看更新的问题。
    • stackoverflow.com/questions/29002963/…> - 创建了一个与此相关的新问题
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-10
    • 1970-01-01
    • 2013-08-06
    • 2013-05-18
    • 1970-01-01
    • 2013-07-13
    相关资源
    最近更新 更多