1

Using Twitter Boostrap TypeAhead along with MVC4 - is there any way I can change the javascript for typeahead, to highlight ANY word in the search box, as opposed to exactly what is in the search box eg. if I type Outlook, it highlights Outlook in the 4 items in the drop down:

Typeahead ss1

However, if I type "outlook access" (which appears in the 4th item above) - it IS returned in the JSON from my controller, however, TypeAhead does not display it as "Outlook access" does not appear as one string in the entries:

Typeahead ss2

I suspect the answer is here (typeahead JS) but not certain:

, highlighter: function (item) {
  var query = this.query.replace(/[\-\[\]{}()*+?.,\\\^$|#\s]/g, '\\$&')
  return item.replace(new RegExp('(' + query + ')', 'ig'), function ($1, match) {
    return '<strong>' + match + '</strong>'
  })
}

, render: function (items) {
  var that = this

  items = $(items).map(function (i, item) {
    i = $(that.options.item).attr('data-value', item)
    i.find('a').html(that.highlighter(item))
    return i[0]
  })

  items.first().addClass('active')
  this.$menu.html(items)
  return this
}

Thanks for any help,

Mark

4

1 回答 1

1

您希望matcher通过将其添加到您的 typeahead 选项来覆盖以返回所有结果:

matcher: function(item) { return true; }

默认情况下,它会进行基本的字符串匹配以过滤掉它们,但是由于您的控制器已经返回了您想要的项目,因此可以将它们全部显示出来。

这将显示项目,尽管荧光笔不会突出显示任何内容,因为找不到“outlook access”。您可以以类似的方式通过选项覆盖该highlighter方法。如果控制器以特定顺序返回结果,您可能还希望重写该sorter方法以按原样返回项目。

于 2013-04-30T14:34:44.367 回答