更改 Twitter Typeahead 返回的内容
Change what Twitter Typeahead returns
是否可以更改 Twitter 的 Typeahead 返回的内容?
以便用户可以在文本字段中搜索名称,但它返回与所选结果对应的 ID?
我找不到任何关于这个的东西,我什至搜索了源代码。
提前致谢
$(document).ready(function(){
var modules = getBloodhound('modules');
$('.typeahead').typeahead({
hint: true,
highlight: true,
minLength: 1
},
{
name: 'id',
displayKey: 'name',
source: modules
});
});
var getBloodhound = function(name){
return new Bloodhound({
datumTokenizer: Bloodhound.tokenizers.whitespace,
queryTokenizer: Bloodhound.tokenizers.whitespace,
remote: {
url: '/json/' + name + '/%QUERY',
wildcard: '%QUERY'
}
});
}
编辑,更新
如果正确解释 Question,则返回的数组包含具有name
和id
属性的对象。要求是将元素input
处显示的hint
设置为返回对象的id
,而不是name
,这将在建议结果中呈现?
可以使用templates
、suggestion
函数来设置 .tt-hint
input
,这将显示hint
到id
的值;设置.tt-hint
placeholder
属性,该属性在选项suggestion
显示对象参数的hint
到id
。
使用 typeahead:render
、input
事件来设置 .tt-hint
的属性css
left
或者如果 .typeahead
处没有value
,则placeholder
为空字符串
var getBloodhound = function(name){
return new Bloodhound({
datumTokenizer: Bloodhound.tokenizers.whitespace("value"),
queryTokenizer: Bloodhound.tokenizers.whitespace,
remote: {
url: '/json/' + name + '/%QUERY',
wildcard: '%QUERY'
}
});
}
var modules = getBloodhound('modules').ttAdapter();
$(".typeahead").typeahead({
hint: true,
minLength: 1,
highlight: true
}, {
name: "id",
display: "value",
source: modules,
templates: {
suggestion: function(data) {
return "<li>" + data.team + "</li>"
}
}
})
.on("typeahead:render", function(e, data) {
console.log(e, data);
$(".tt-hint").attr("placeholder", data.id)
.css("left", e.target.value.length * 10)
})
.on("input", function() {
if (this.value === "" || /^'s+$/.test(this.value)) {
$(".tt-hint").attr("placeholder", "")
}
})
var nflTeams = new Bloodhound({
datumTokenizer: Bloodhound.tokenizers.obj.whitespace("team"),
queryTokenizer: Bloodhound.tokenizers.whitespace,
local: [{
"team": "San Francisco 49ers",
"id": "49ers"
}, {
"team": "Chicago Bears",
"id": "Bears"
}, {
"team": "Cincinnati Bengals",
"id": "Bengals"
}, {
"team": "Buffalo Bills",
"id": "Bills"
}, {
"team": "Denver Broncos",
"id": "Broncos"
}, {
"team": "Cleveland Browns",
"id": "Browns"
}, {
"team": "Tampa Bay Buccaneers",
"id": "Buccaneers"
}, {
"team": "Arizona Cardinals",
"id": "Cardinals"
}, {
"team": "San Diego Chargers",
"id": "Chargers"
}, {
"team": "Kansas City Chiefs",
"id": "Chiefs"
}, {
"team": "Indianapolis Colts",
"id": "Colts"
}, {
"team": "Dallas Cowboys",
"id": "Cowboys"
}, {
"team": "Miami Dolphins",
"id": "Dolphins"
}, {
"team": "Philadelphia Eagles",
"id": "Eagles"
}, {
"team": "Atlanta Falcons",
"id": "Falcons"
}, {
"team": "New York Giants",
"id": "Giants"
}, {
"team": "Jacksonville Jaguars",
"id": "Jaguars"
}, {
"team": "New York Jets",
"id": "Jets"
}, {
"team": "Detroit Lions",
"id": "Lions"
}, {
"team": "Green Bay Packers",
"id": "Packers"
}, {
"team": "Carolina Panthers",
"id": "Panthers"
}, {
"team": "New England Patriots",
"id": "Patriots"
}, {
"team": "Oakland Raiders",
"id": "Raiders"
}, {
"team": "St.Louis Rams",
"id": "Rams"
}, {
"team": "Baltimore Ravens",
"id": "Ravens"
}, {
"team": "Washington Redskins",
"id": "Redskins"
}, {
"team": "New Orlean Saints",
"id": "Saints"
}, {
"team": "Seattle Seahawks",
"id": "Seahawks"
}, {
"team": "Pittsburgh Steelers",
"id": "Steelers"
}, {
"team": "Houston Texans",
"id": "Texans"
}, {
"team": "Tennesse Titans",
"id": "Titans"
}, {
"team": "Minnesota Vikings",
"id": "Vikings"
}]
})
var adapter = nflTeams.ttAdapter();
$("#default-suggestions .typeahead").typeahead({
hint: true,
minLength: 1,
highlight: true
}, {
name: "nfl-teams",
display: "value",
source: adapter,
templates: {
suggestion: function(data) {
return "<li>" + data.team + "</li>"
}
}
})
.on("typeahead:render", function(e, data) {
console.log(e, data);
$(".tt-hint").attr("placeholder", data.id)
.css("left", "calc(" + e.target.value.length * 7 + "px)")
})
.on("input", function() {
if (this.value === "" || /^'s+$/.test(this.value)) {
$(".tt-hint").attr("placeholder", "")
}
})
<script src="http://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script src="http://twitter.github.io/typeahead.js/releases/latest/typeahead.bundle.js"></script>
<div id="default-suggestions">
<input class="typeahead" type="text" placeholder="NFL Teams">
</div>
相关文章:
- 节点导出返回一个空对象
- ES6构造函数返回基类的实例
- 监视函数从服务返回不起作用,但作用域函数起作用
- 控制台返回var不是't定义,但它是
- 从函数返回角度承诺
- Javascript返回值只在循环中返回一次
- 从控制器返回后Ajax启动事件激发
- 在Twitter上用ie9中的空白src访问iframe的contentWindow
- CKFinder 3为所选文件返回错误的URL
- 更改 Twitter Typeahead 返回的内容
- Date.getTime()返回IE11中ISO/Twitter API日期的NaN
- 如果名称包含数字,Twitter 搜索 API 不会返回推文
- Twitter typeahead 0.9.3 远程返回 URL 和 JSON 对象
- Twitter Typeahead 0.9.3 返回 json 对象
- “follow”键在Get friends/list API Twitter中总是返回false
- 为什么twitter搜索不返回结果?
- Twitter API不返回图像
- Twitter更新在QML中运行时返回401 Unauthorized,但第一次只能在桌面Java上工作
- 统计twitter搜索API返回的页面数
- Twitter API 仍然since_id返回每个结果