英文:
`display` option in typeahead.js does not work
问题
这是您要翻译的内容:
"我已经做了好几个小时了,我确信我犯了一个愚蠢的错误。但我束手无策...
这是我已经检查过的内容:
文档:
- https://github.com/corejavascript/typeahead.js/blob/master/doc/jquery_typeahead.md#usage
- https://github.com/twitter/typeahead.js/blob/master/doc/bloodhound.md#options
Stackoverflow:
- https://stackoverflow.com/questions/30757076/typeahead-js-doesnt-display-the-dropdown-using-remote-data
- https://stackoverflow.com/questions/30370496/typeahead-js-bloodhound-display-just-one-result
- https://stackoverflow.com/questions/18768401/typeahead-js-displaying-all-prefetched-datums
示例/谷歌/等等
...
这是我的设置。我有一个MongoDB,为它创建了一个带有Mongoose/Node.js的API搜索路径。我已经验证了搜索API的正常工作。
HTML(pug)
.answer-container
form(method="POST" enctype="multipart/form-data")#answer-form.form
label(for="new-answer") Answer:
input(required minlength="1" maxlength="80" type="text" name="new-answer" id="new-answer" placeholder="Answer Text" autocomplete="off").new-answer-input
br
input(type="submit" name="submit" disabled autocomplete="off" value="Submit")#answer-submit-button
.answer-messages
我已验证pug模板按预期工作。
JS(typeahead和bloodhound)
var answer_suggestions = new Bloodhound({
datumTokenizer: Bloodhound.tokenizers.obj.whitespace("text"),
queryTokenizer: Bloodhound.tokenizers.whitespace,
remote:{
url: 'http://localhost:3000/api/v1/answers/search?text=%QUERY',
wildcard: '%QUERY'
}
});
$('#new-answer').typeahead({
hint: true,
highlight: true,
autoselect: true,
minLength: 2
},
{
name: 'answers',
source: answer_suggestions,
display: 'text',
limit: 10
});
问题
如果我删除display
选项,我可以看到来自API的结果,并且一切正常工作。如果我包括它,我将得不到结果。
结果
原始结果
[
{
"_id": "64401c91bc76fdfca320c118",
"text": "pneumothorax",
"__v": 0
},
{
"_id": "64403bb708645fb7abbe2db3",
"text": "pneumothorax",
"__v": 0
},
{
"_id": "64403bdf08645fb7abbe2dbf",
"text": "hemopneumothorax",
"__v": 0
}
]
我已尝试将数据重构为JSON对象,尝试在display
中使用函数,但似乎没有什么效果。
我只想要结果列表简单显示答案文本。
英文:
I have been at this for hours, and I am sure I am making a dumb mistake. But I am at a loss...
Here is what I have checked:
docs:
- https://github.com/corejavascript/typeahead.js/blob/master/doc/jquery_typeahead.md#usage
- https://github.com/twitter/typeahead.js/blob/master/doc/bloodhound.md#options
Stackoverflow:
- https://stackoverflow.com/questions/30757076/typeahead-js-doesnt-display-the-dropdown-using-remote-data
- https://stackoverflow.com/questions/30370496/typeahead-js-bloodhound-display-just-one-result
- https://stackoverflow.com/questions/18768401/typeahead-js-displaying-all-prefetched-datums
Examples/ google / etc
...
Here is my setup. I have a mongoDB which I have created an API search path for with mongoose / Node.js. I have verified that the search API works correctly.
HTML (pug)
.answer-container
form(method="POST" enctype="multipart/form-data")#answer-form.form
label(for="new-answer") Answer:
input(required minlength="1" maxlength="80" type="text" name="new-answer" id="new-answer" placeholder="Answer Text" autocomplete="off").new-answer-input
br
input(type="submit" name="submit" disabled autocomplete="off" value="Submit")#answer-submit-button
.answer-messages
I have verified that the pug template is working as expected.
JS (typeahead and bloodhound)
var answer_suggestions = new Bloodhound({
datumTokenizer: Bloodhound.tokenizers.obj.whitespace("text"),
queryTokenizer: Bloodhound.tokenizers.whitespace,
remote:{
url: 'http://localhost:3000/api/v1/answers/search?text=%QUERY',
wildcard: '%QUERY'
}
});
$('#new-answer').typeahead({
hint: true,
highlight: true,
autoselect: true,
minLength: 2
},
{
name: 'answers',
source: answer_suggestions,
display: 'text',
limit: 10
});
PROBLEM
If I remove the display
option, I can SEE the results are coming in from the API and everything is working. If I include it, I get no results.
RESULTS
Raw results
[
{
"_id": "64401c91bc76fdfca320c118",
"text": "pneumothorax",
"__v": 0
},
{
"_id": "64403bb708645fb7abbe2db3",
"text": "pneumothorax",
"__v": 0
},
{
"_id": "64403bdf08645fb7abbe2dbf",
"text": "hemopneumothorax",
"__v": 0
}
]
I have tried refactoring the data as a json object, I have tried using a function in display
but nothing seems to work.
All I want is the results list to simply show the answer text.
答案1
得分: 0
我已更新我的问题,排除了多余的信息,并将我的答案提供如下。
问题在于,当我在typeahead
的display
函数中查看或记录数据时,或者在页面本身上看到它时,它会显示嵌套数据。感到困惑吗?我也是...
以下是我认为我的API返回给bloodhound
的内容
[
{
"_id": "64401c91bc76fdfca320c118",
"text": "pneumothorax",
"__v": 0
},
{
"_id": "64403bb708645fb7abbe2db3",
"text": "pneumothorax",
"__v": 0
},
{
"_id": "64403bdf08645fb7abbe2dbf",
"text": "hemopneumothorax",
"__v": 0
}
]
以下是实际返回的内容。
{
answers: [
{
"_id": "64401c91bc76fdfca320c118",
"text": "pneumothorax",
"__v": 0
},
{
"_id": "64403bb708645fb7abbe2db3",
"text": "pneumothorax",
"__v": 0
},
{
"_id": "64403bdf08645fb7abbe2dbf",
"text": "hemopneumothorax",
"__v": 0
}
]
}
我通过在bloodhound
的可选transform
函数中记录数据来发现了这一点。这个额外的对象是问题的根本原因。由于bloodhound
在传递给typeahead
之前将其剥离,所以我从未看到它。
以下是可行的代码:
var answer_suggestions = new Bloodhound({
datumTokenizer: Bloodhound.tokenizers.whitespace('text'),
queryTokenizer: Bloodhound.tokenizers.whitespace,
remote:{
url: 'http://localhost:3000/api/v1/answers/search?text=%QUERY',
wildcard: '%QUERY',
transform: (response) => {
return $.map(response.answers, function(object) {
return { text: object.text };
});
}
}
});
$('#new-answer').typeahead({
hint: true,
highlight: true,
autoselect: true,
minLength: 2
},
{
name: 'typeahead-answers',
source: answer_suggestions,
display: 'text',
templates: {
notFound: '<div>Not Found</div>',
pending: '<div>Loading...</div>',
suggestion: function(data) {
return `<div>${data.text}</div>`
},
},
limit: 10
});
您可以看到,我在bloodhound
中添加了一个自定义的transform
。我还在typeahead
中添加了一个自定义模板,但这并不是必需的。
我希望这对某人有所帮助。在我看来,文档和示例都不足够。数据记录是您的朋友。
英文:
I have updated my question to exclude extraneous info and I will provide my answer below.
The issue was that when I was seeing or logging my data in the typeahead
display
function or on the page itself it was showing me the NESTED data. Confused? so was I...
Here is what I thought my API was returning to bloodhound
[
{
"_id": "64401c91bc76fdfca320c118",
"text": "pneumothorax",
"__v": 0
},
{
"_id": "64403bb708645fb7abbe2db3",
"text": "pneumothorax",
"__v": 0
},
{
"_id": "64403bdf08645fb7abbe2dbf",
"text": "hemopneumothorax",
"__v": 0
}
]
Here is what is was ACTUALLY returning.
{
answers:{
[
{
"_id": "64401c91bc76fdfca320c118",
"text": "pneumothorax",
"__v": 0
},
{
"_id": "64403bb708645fb7abbe2db3",
"text": "pneumothorax",
"__v": 0
},
{
"_id": "64403bdf08645fb7abbe2dbf",
"text": "hemopneumothorax",
"__v": 0
}
]
}
I found this by logging the data in the bloodhound
optional transform
function. This extra object was my culprit. Since bloodhound stripped it before it got to typeahead, I never saw it.
HERE is the working code:
var answer_suggestions = new Bloodhound({
datumTokenizer: Bloodhound.tokenizers.whitespace('text'),
queryTokenizer: Bloodhound.tokenizers.whitespace,
remote:{
url: 'http://localhost:3000/api/v1/answers/search?text=%QUERY',
wildcard: '%QUERY',
transform: (response) => {
return $.map(response.answers, function(object) {
return { text: object.text };
});
}
}
});
$('#new-answer').typeahead({
hint: true,
highlight: true,
autoselect: true,
minLength: 2
},
{
name: 'typeahead-answers',
source: answer_suggestions,
display: 'text',
templates: {
notFound: '<div>Not Found</div>',
pending: '<div>Loading...</div>',
suggestion: function(data) {
return `<div>${data.text}</div>`
},
},
limit: 10
});
You can see I added a custom transform
in bloodhound
. I also added a custom template in typeahead
but this was not required.
I hope this helps someone. Not enough docs or examples, IMO.
Data logging is your friend.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论