【问题标题】:typeahead / filter / JSON parse?预输入/过滤/JSON解析?
【发布时间】:2016-01-06 04:54:43
【问题描述】:

尝试在我的预输入代码上“解析/读取”外部 .json 文件,但 .json 文件(我无法修改)看起来像:

{"**cms_countries**":
  [{"**cms_country**": 
[{"**countrydisplayname**":"Afghanistan"}
 ,{"countrydisplayname":"Albania"} ,{"countrydisplayname":"Algeria"}
 ... ... ... ,{"countrydisplayname":"Zimbabwe"} ] } ,{"TotalRecords": 
 [ {"TotalRecords":"246"} ] } ] }

所以,我认为我的问题是知道如何解析/读取/同化/集成/采用这个 .json 文件,具有 cms_countries , cms_country , 然后,我的 countrydisplayname 字段就可以了。 (你看到这里的树了吗?)

这是我的代码:

$(document).ready(function() {

        var searchablePlaces    = new Bloodhound({
        datumTokenizer                  : Bloodhound.tokenizers.obj.whitespace("countrydisplayname"),
        queryTokenizer                  : Bloodhound.tokenizers.whitespace,
        prefetch                        : 'countries.json',
        remote                          : {
            url                             : 'countries/%QUERY.json',
            wildcard                        : '%QUERY',
            filter                          : function(response) { return response.cms_country; }
            }, 
        limit                           : 10
        });

searchablePlaces.initialize(); 

        $('#remote .typeahead').typeahead(
        {
        hint            : true,
        highlight       : true,
        minLength       : 2
        },
        {
        name            : 'countrydisplayname',
        displayKey      : "countrydisplayname",
        source          : searchablePlaces.ttAdapter()
        })
});

当然,它不起作用:

关于如何组织我的过滤器的任何提示...?或者如何克服我的nested .json 包装器....

【问题讨论】:

    标签: json typeahead bloodhound


    【解决方案1】:

    好的,我的代码现在可以运行了:

    $(window).load(function(){   
        var movies = new Bloodhound({
            limit: 10,
            datumTokenizer: function (d) {
                return Bloodhound.tokenizers.whitespace(d.value);
            },
            queryTokenizer: Bloodhound.tokenizers.whitespace,
            prefetch: { 
                url: 'countries.json',
                filter: function (movies) {  
                    return $.map(movies.cms_countries[0].cms_country, function (paises) {  
                        return {        
                            value: paises.countrydisplayname 
                        };
                    });
                }
            }
        });
    
        // Initialize the Bloodhound suggestion engine
        movies.initialize(); 
    
        // Instantiate the Typeahead UI
        $('.typeahead').typeahead(
            {
                hint: true,
                highlight: true,
                minLength: 1
            }, 
            {
            //displayKey: 'value',
            displayKey: function (toto) {
                      return toto.value;
                  },
            source: movies.ttAdapter()
        });
    }); 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-03-14
      • 2022-01-12
      • 2023-01-20
      • 2014-12-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多