简体   繁体   English

Bootstrap-3-Typeahead获得正确的JSON密钥

[英]Bootstrap-3-Typeahead getting right json key

I am useing this plugin for an ajax auto complete feature https://github.com/bassjobsen/Bootstrap-3-Typeahead the bootstrap-3 type. 我正在将此插件用于ajax自动完成功能https://github.com/bassjobsen/Bootstrap-3-Type,而不是bootstrap-3类型。 The code below is working but I do not know why it works. 下面的代码正常工作,但我不知道为什么工作。 Specifically how the the process and response parameter work. 特别是过程和响应参数的工作方式。

$(document).ready(function() {
    $('#typeahead-input').typeahead({
        autoSelect: true,
        minLength: 1,
        delay: 400,
        source: function (query, process) {
            $.ajax({
                url: '/api/location',
                data: {sstr: query},
                dataType: 'json'
            })
                .done(function(response) {

                    // console.log(response)
                    return process(response);
                });
        }
    });
});

my json looks like this 我的json看起来像这样

[
    {
        "id": "123", 
        "name": "Frederiksted", 
        "state": "VI", 
        "zip_code": "840"
    }
]

What if i wanted to autocomplete to populated based on on the zip_code field how would i do it? 如果我想根据zip_code字段自动完成填充,该怎么办? I have tried doing "response.zipcode" but it comes out as undefined 我试着做“ response.zipcode”,但结果未定义

First, response.zipcode will be undefined because response is a Array not a Object. 首先,response.zipcode将是未定义的,因为response是一个数组而不是对象。 You access zipcode by response[0].zip_code ( And also note that your property name is not 'zipcode' it is 'zip_code' ). 您可以通过response [0] .zip_code访问邮政编码(并且还要注意,您的属性名称不是'zipcode',而是'zip_code')。

Second, documentation of the "source" property says: The data source to query against. 其次,“源”属性的文档说:要查询的数据源。 May be an array of strings, an array of JSON object with a name property or a function. 可以是字符串数组,具有name属性或函数的JSON对象数组。

So, what you give to the "process" method most probably should be a array of strings or array of JSON objects where each JSON object has a "name" property. 因此,您提供给“ process”方法的内容很可能应该是字符串数组或JSON对象数组,其中每个JSON对象都具有“ name”属性。 If your response is correct and returns an array of objects like you say, then it means your objects each have a 'name' property,so that property is displayed. 如果您的回答正确,并且返回了您所说的对象数组,则意味着您的对象每个都有一个“名称”属性,因此将显示该属性。 If you want to display something else, you need to create a new String array from the response: 如果要显示其他内容,则需要根据响应创建一个新的String数组:

So I would try this: 所以我会尝试这样的:

 .done(function(response) {
     // get the response and create a new array of Strings
     var names = $.map (response, function(item) {
          return item.name + '-' + item.zip_code;
     });
     // console.log(response)
     return process(names);
    });

or another way: 或另一种方式:

.done(function(response) {
 // get the response and change the 'name' of each object
 $.each (response, function() {
       this.name = this.name + '-' + this.zip_code;
 });
 // console.log(response)
 return process(response);
});

I think you have issue with json format: 我认为您在使用json格式时遇到问题:

[ "id": "123", "name": "Frederiksted", "state": "VI", "zip_code": "840" ] [“ id”:“ 123”,“ name”:“ Frederiksted”,“ state”:“ VI”,“ zip_code”:“ 840”]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM