jQuery jquery自动完成与json响应

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1592048/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 11:47:44  来源:igfitidea点击:

jquery autocomplete with json response

jqueryjsonautocompleteresponse

提问by Basit

im getting response in json, but this wont parse the json response. what m i doing wrong? i could'nt find anything on doc http://docs.jquery.com/Plugins/Autocomplete

我在 json 中得到响应,但这不会解析 json 响应。我做错了什么?我在文档http://docs.jquery.com/Plugins/Autocomplete上找不到任何内容

$("#users-allowed").autocomplete("/people/following.json", {
  width: 320,
  //max: 4,
  highlight: false,
  scroll: true,
  scrollHeight: 300,
  formatItem: function(response, i, max) {
    console.log(response);
    console.log(response['items']);
    console.log(response.items);
    return i + "/" + max + ": \"" + response.status_code + "\" [" + response.status_description + "]";

    //return "<img src='images/" + value + "'/> " + value.split(".")[0];
  },
  formatResult: function(response) {
    //return value.split(".")[0];
    return response.status_description;
  }
});

回答by Basit

$("#users-allowed").autocomplete("/people/following.json", {
  width: 320,
  dataType: 'json',
  highlight: false,
  scroll: true,
  scrollHeight: 300,
  parse: function(data) {
    var array = new Array();
    for(var i=0;i<data.items.length;i++) {
      array[array.length] = { data: data.items[i], value: data.items[i], result: data.items[i].username };
    }
    return array;
  },
  formatItem: function(row) {               
    var name = '';
    if (row.first_name && row.last_name)
      name = '('+row.first_name+', '+row.last_name+')';
    else if (row.first_name)
      name = '('+row.first_name+')';
    else if (row.last_name)
      name = '('+row.last_name+')';

    return row.username+' '+name;
  }
});

check dataType and parse option.

检查数据类型和解析选项。

回答by karim79

I think you just need to throw in a dataTypeoption, I remember readying that you can use any of $.ajax's options in the autocompleter:

我认为您只需要输入一个dataType选项,我记得准备好您可以$.ajax在自动完成程序中使用's 的任何选项:

$("#users-allowed").autocomplete("/people/following.json", {
    dataType: "json",
    ...

回答by Anderson Murakami

Try declaring the options outside the scope of $(document).ready(..)

尝试声明超出范围的选项 $(document).ready(..)

Ex:

前任:

var acCbo = {
        minChars: 1,
        delay:500,
        max: 100,
        width: 400,
        dataType: 'json', // this parameter is currently unused
        extraParams: {
            format: 'json', //pass the required context to the Zend Controller,
            filtro: 'id_procsianv,id_atividade',
            chave: function(){
                return $('#id_procsianv').val()+','+$('#id_atividade').val();
            }
        },
        queryParam: "descricao",
        parse: function(data) {
            if (data['qtde']>0){
                data = data['Cbo'];
                var parsed = [];
                for (var i = 0; i < data.length; i++) {
                    parsed[parsed.length] = {
                        data: data[i],
                        value: data[i].id_cbo,
                        result: $('<textarea/>').html(data[i].no_cbo).val()
                    };
                }
                return parsed;
            }else{
                $('#id_cbo').val('');
                return [];
            }
        },
        formatItem: function(item) {
            return item.no_cbo+ ' (' +item.id_cbo+ ')';
        }
    };

    $(document).ready(function(){

    $('#cbo').autocomplete('/cbos/index',acCbo)
    .result(function(e,data){
        $('#id_cbo').val(data.id_cbo);

    });
});