2009-10-20 104 views
20

即時通訊在json中得到響應,但是這不會解析json響應。我做錯了什麼?我could'nt找到DOC任何http://docs.jquery.com/Plugins/AutocompletejQuery響應jQuery自動完成

$("#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; 
    } 
}); 

回答

33
$("#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; 
    } 
}); 

檢查dataType和解析選項。

+0

btw這是正確的答案在karim79的幫助下 – Basit 2009-10-20 02:25:36

+0

嗨basit,我有同樣的問題,你做了什麼,因爲我得到一個data.split不是一個函數 – Angela 2010-02-24 22:37:15

+0

我試圖按照這個答案,它doesn沒有工作。 formatItem函數參數未定義。 – 2010-07-07 22:10:41

6

我想你只需要在dataType選項扔,我記得準備好,你可以在autocompleter使用任何$.ajax的選擇:

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

與即時得到data.split不是一個函數 的jQuery/jquery.autocomplete.js 11號線誤差 – Basit 2009-10-20 01:51:35

+0

BTW我沒有在任何地方使用這個函數..你可以在上面看到我的代碼 – Basit 2009-10-20 01:52:07

+0

@basit - 因爲現在你正在處理一個對象,'split'是'Array'類型的函數。 – karim79 2009-10-20 01:54:18

1

嘗試宣告$(document).ready(..)

防爆範圍之外的選項:

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); 

    }); 
});