2012-07-07 3 views
4

기존의 id, label, value를 사용하지 않고도 JQuery 자동 완성 기능을 사용할 수 있습니까?레이블, 값, id를 사용하지 않고 JQuery 자동 완성

예를 들어, 내가 사용하지 않으 : 오히려

[ { "id": "Botaurus stellaris", "label": "Great Bittern", "value": "Great Bittern" }, 
{ "id": "Asio flammeus", "label": "Short-eared Owl", "value": "Short-eared Owl" }] 

하지만을 : 그래서

[ { "my_id": "Botaurus stellaris", "first_name": "Great Bittern", "last_name": "Great Bittern" }, 
{ "my_id": "Asio flammeus", "first_name": "Short-eared Owl", "last_name": "Short-eared Owl" }] 

같은, 일반적으로 id,value,label 무엇이 JQuery autocomplete, 나는 그것이 jQuery를 my_id, first_name, last_name을 가지고 만들 수 있습니다 같은 자동 완성 치료 id,label,value?

데이터 소스에서 오는 데이터 키를 수정하지 않고 JQuery 자동 완성에 표시하려는 경우 유용 할 수 있습니다.

답변

2

결코 마음,

찾을 수 JQueryUI의 자동 완성 예제 소스에서 답 : 변환으로 변환 할 필요에 키의 모든 유형을 반환 할 수 있습니다 여기에

$("#city").autocomplete({ 
     source: function(request, response) { 
      $.ajax({ 
       url: "http://ws.geonames.org/searchJSON", 
       dataType: "jsonp", 
       data: { 
        featureClass: "P", 
        style: "full", 
        maxRows: 12, 
        name_startsWith: request.term 
       }, 
       success: function(data) { 
        response($.map(data.geonames, function(item) { 
         return { 
          label: item.name + (item.adminName1 ? ", " + item.adminName1 : "") + ", " + item.countryName, 
          value: item.name 
         } 
        })); 
       } 
      }); 
     }, 
     minLength: 2 
    }); 

, source 함수를 할당하고, JQuery의 자동 완성은 id,value,label입니다.

일부 도움이 될 수 있습니다.

1

더 나은 해결책이 있습니다. jquery-ui 웹 사이트에서도 문서화되어 있습니다.

http://jqueryui.com/demos/autocomplete/#custom-data

$("input").autocomplete({..}) 
.data("autocomplete")._renderItem = function(ul, item) {return $("<li></li>") 
    .data("item.autocomplete", item) 
      .append("<a>" + item.label + "<br>" + item.desc + "</a>") 
      .appendTo(ul); 
    }; 
관련 문제