“display”选项在typeahead.js中不起作用。

huangapple go评论54阅读模式
英文:

`display` option in typeahead.js does not work

问题

这是您要翻译的内容:

"我已经做了好几个小时了,我确信我犯了一个愚蠢的错误。但我束手无策...

这是我已经检查过的内容:

文档:

Stackoverflow:

示例/谷歌/等等
...

这是我的设置。我有一个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:

Stackoverflow:

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

我已更新我的问题,排除了多余的信息,并将我的答案提供如下。

问题在于,当我在typeaheaddisplay函数中查看或记录数据时,或者在页面本身上看到它时,它会显示嵌套数据。感到困惑吗?我也是...

以下是我认为我的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

[
  {
    &quot;_id&quot;: &quot;64401c91bc76fdfca320c118&quot;,
    &quot;text&quot;: &quot;pneumothorax&quot;,
    &quot;__v&quot;: 0
  },
  {
    &quot;_id&quot;: &quot;64403bb708645fb7abbe2db3&quot;,
    &quot;text&quot;: &quot;pneumothorax&quot;,
    &quot;__v&quot;: 0
  },
  {
    &quot;_id&quot;: &quot;64403bdf08645fb7abbe2dbf&quot;,
    &quot;text&quot;: &quot;hemopneumothorax&quot;,
    &quot;__v&quot;: 0
  }
]

Here is what is was ACTUALLY returning.

{ 
answers:{
[
  {
    &quot;_id&quot;: &quot;64401c91bc76fdfca320c118&quot;,
    &quot;text&quot;: &quot;pneumothorax&quot;,
    &quot;__v&quot;: 0
  },
  {
    &quot;_id&quot;: &quot;64403bb708645fb7abbe2db3&quot;,
    &quot;text&quot;: &quot;pneumothorax&quot;,
    &quot;__v&quot;: 0
  },
  {
    &quot;_id&quot;: &quot;64403bdf08645fb7abbe2dbf&quot;,
    &quot;text&quot;: &quot;hemopneumothorax&quot;,
    &quot;__v&quot;: 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(&#39;text&#39;),
        queryTokenizer: Bloodhound.tokenizers.whitespace,
        remote:{
            url: &#39;http://localhost:3000/api/v1/answers/search?text=%QUERY&#39;,
            wildcard: &#39;%QUERY&#39;,
            transform: (response) =&gt; {
                return $.map(response.answers, function(object) {
                  return { text: object.text };
                });
              }
        }
      });

      $(&#39;#new-answer&#39;).typeahead({
        hint: true,
        highlight: true,
        autoselect: true,
        minLength: 2
      },
      {
        name: &#39;typeahead-answers&#39;,
        source: answer_suggestions,
        display: &#39;text&#39;,
        templates: {
            notFound: &#39;&lt;div&gt;Not Found&lt;/div&gt;&#39;,
            pending: &#39;&lt;div&gt;Loading...&lt;/div&gt;&#39;,
            suggestion:  function(data) {
                return `&lt;div&gt;${data.text}&lt;/div&gt;`
            },
        },
        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.

huangapple
  • 本文由 发表于 2023年4月20日 05:03:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/76058790.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定