2017-03-16 41 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(我认为这是值得的预输入的版本)。

相关问题