【问题标题】:Typeahead, Bloodhound & Selecting Data Source IssueTypeahead、Bloodhound 和选择数据源问题
【发布时间】:2018-03-22 15:32:54
【问题描述】:

我有一个这样的脚本:

<script>
        $(document).ready(function() {
            var searchType = $('select[name=searchType]').val();
            var bloodhound = new Bloodhound({
                datumTokenizer: Bloodhound.tokenizers.whitespace,
                queryTokenizer: Bloodhound.tokenizers.whitespace,
                remote: {
                    url: '/'+searchType+'/find?q=%QUERY%',
                    wildcard: '%QUERY%'
                },
            });

            $('#search').typeahead({
                hint: true,
                highlight: true,
                minLength: 3
            }, {
                name: searchType,
                source: bloodhound,
                display: function(data) {
                    return data.name  //Input value to be set when you select a suggestion. 
                },
                templates: {
                    empty: [
                        '<div class="list-group search-results-dropdown"><div class="list-group-item">Nothing found.</div></div>'
                    ],
                    header: [
                        '<div class="list-group search-results-dropdown">'
                    ],
                    suggestion: function(data) {
                        if(searchType == "shipments"){
                            return '<a href="/shipments/i/'+data.UUID+'"><div style="font-weight:normal; margin-top:-10px ! important;" class="list-group-item">' + data.pro_number + ' - Date: '+ data.date +'</div></a></div>'
                        }else if(searchType == "manifests"){
                             return '<a href="/carrier/manifests/view/'+data.id+'"><div style="font-weight:normal; margin-top:-10px ! important;" class="list-group-item">' + data.manifestNumber + ' - Origin: '+ data.terminalOrigin +'</div></a></div>'
                        }else if(searchType == "customers"){
                            return '<a href="/customers/i/'+data.url_string+'"><div style="font-weight:normal; margin-top:-10px ! important;" class="list-group-item">' + data.customer_name +'</div></a></div>'
                        }
                    }
                }
            });                         
        });
    </script>

在大多数情况下,我相信它有效。问题是它坚持一个选择,而不是在任何给定时间选择一个选择后更改选择。

我的搜索表单相当简单:

<form class="sidebar-form">
        <div class="input-group">
          <input type="text" name="search" class="form-control" placeholder="Search Pro Number" id="search">

          <span class="input-group-btn">
              <button type="submit" name="search" id="search-btn" class="btn btn-flat"><i class="fa fa-search"></i>
              </button>
            </span>
        </div>
          <select name="searchType" class="form-control">
                <option value="" selected>Search Type</option>
                <option value="shipments">Shipments</option>
                <option value="manifests">Manifests</option>
                <option value="customers">Customers</option>
            </select>
      </form>

正如我所说,前面的类型和数据返回都可以,然后如果我想更改它搜索的数据集,我必须重新加载页面。因此,如果我将下拉菜单从 manifests 更改为 shipping,它仍会尝试搜索 manifests。

【问题讨论】:

  • 按照我平时对 Matthew 的帖子的注释,您不需要在帖子上签名,也不需要添加注释来表示提前感谢和感谢

标签: jquery typeahead.js typeahead bloodhound


【解决方案1】:

您的问题是 Bloodhound 假定搜索 URL 仅在“查询”参数所在的位置发生变化。在大多数情况下,这个假设是正确的,所以他们将其设为默认值。

不过,在您的情况下,URL 应该在 两个 处发生变化,因此 Bloodhound 的默认行为还不够好。

通过提供 prepare 函数可以轻松覆盖默认行为,该函数可以在请求之前对 Bloodhound 的设置进行更广泛的更改。

当前的querysettings 正在作为参数传递。您需要做的就是修改settings.url

var bloodhound = new Bloodhound({
    datumTokenizer: Bloodhound.tokenizers.whitespace,
    queryTokenizer: Bloodhound.tokenizers.whitespace,
    remote: {
        url: 'required, but will be overridden in prepare() anyway',
        prepare: function (query, settings) {
            var searchType = $('select[name=searchType]').val();

            settings.url = '/' + searchType + '/find?' + $.param({
                q: query
            });

            return settings;
        }
    }
});

另一种方法是使用 N 个不同的 bloodhound 实例,一个用于每个可能的 searchType 值,但每个都有一个固定的 URL,然后在 typeahead 配置中动态决定使用哪个。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-01
    • 2014-03-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多