2017-03-16 161 views
2

我的建議引擎工作正常,我只是有一個問題,因爲當我點擊項目時它的json對象出現在input元素中。我希望只有OrgName出現在輸入值中。爲什麼Typeahead在輸入後顯示json對象點擊

<input class="form-control companySearch" type="text" value="" name="q" autocomplete="off" placeholder="Search a company"> 

var organization = new Bloodhound({ 
     remote: { 
      url: '/search/org?term=%QUERY%', 
      wildcard: '%QUERY%' 
     }, 
     datumTokenizer: Bloodhound.tokenizers.whitespace('term'), 
     queryTokenizer: Bloodhound.tokenizers.whitespace 
    }); 
$(".companySearch").typeahead({ 
     hint: true, 
     highlight: true, 
     minLength: 1, 
    }, 
     { 
      source: organization.ttAdapter(),  
      name: 'organizationsList',    
      templates: {     
       suggestion: function (data) {   
        return '<a class="list-group-item ">' + data.OrgName + ', '+ data.address.Address+ ', '+ data.address.city.City+ ', ' + data.address.city.country.Country + '</a>';     

       } 
      } 

     } 
    ); 

Example how I get

回答

2

我有一個類似的問題,並使用預輸入的display屬性解決了這個問題。

在您的例子:

$(".companySearch").typeahead({ 
     hint: true, 
     highlight: true, 
     minLength: 1, 
    }, 
     { 
      source: organization.ttAdapter(),  
      name: 'organizationsList', 
      display: 'id', // PUT IT HERE 
      templates: {     
       suggestion: function (data) {   
        return '<a class="list-group-item ">' + data.OrgName + ', '+ data.address.Address+ ', '+ data.address.city.City+ ', ' + data.address.city.country.Country + '</a>';     

       } 
      }  
     } 
    ); 

docs

display - 對於給定的建議,確定它的字符串表示。在選擇建議後設置輸入控件的值時將使用此選項。可以是關鍵字符串,也可以是將建議對象轉換爲字符串的函數。默認爲對建議進行字符串化。

如果它不工作,嘗試更換displaydisplayKey(我認爲這是值得的預輸入的版本)。