【问题标题】:Searching in typeahead if content matches如果内容匹配,则在输入前搜索
【发布时间】:2018-11-22 21:55:29
【问题描述】:

我有一个 typeahead.js 搜索,它搜索两个不同的类别(应用程序和联系人),每个类别都调用 json 文件的后端。我正在添加按应用和联系人 ID 进行搜索的功能,如果前三个字符分别为“OA-”或“OC-”,我希望阻止另一个触发。

searchTerms.typeahead {
  hint: true
  highlight: true
  minLength: 3
}, {
  name: 'applications'
  displayKey: 'address'
  valueKey: 'id'
  source: searchApplications.ttAdapter()
  templates:
    header: '<h4 class="tt-header">Applications:</h34>',
    suggestion: (data) ->
      if data.external_code
        '<div class="search-result-item">' + data.address + ' – ' + data.external_code + '</div>'
      else
        '<div class="search-result-item">' + data.address + ' <span class="btn btn-sm btn-id">OA-' + data.id + '</span></div>'
  limit: 10
},
name: 'contacts'
displayKey: 'name'
valueKey: 'id'
source: searchContacts.ttAdapter()
templates: header: '<h4 class="tt-header">Contacts:</h4>',
suggestion: (data) ->
  '<div class="">' + data.name + ' <span class="btn btn-sm btn-id">OC-' + data.id + '</span></div>'
limit: 10

如果前 3 个字符是“OA-”或“OC-”,如何阻止第二个运行?

【问题讨论】:

    标签: javascript coffeescript typeahead.js


    【解决方案1】:

    我终于明白了……

    该解决方案需要对 source: searchApplications.ttAdapter() 使用的 Bloodhound 进行修改。

    new Bloodhound 函数中,我使用prepare 运行了一个beforeSend 操作:

    search = new Bloodhound(
        remote:
          url: '/applications.json'
          prepare: (query, settings) ->
            settings.url = settings.url + '?query=' + query + '&search=true'
            settings.beforeSend = (e) ->
              if query.substr(0,3).toLowerCase() == 'oc-'
                e.abort()
            return settings
    )
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-05-29
      • 1970-01-01
      • 1970-01-01
      • 2022-09-25
      • 2021-04-25
      • 1970-01-01
      • 2012-07-03
      相关资源
      最近更新 更多