猎犬限制不起作用

时间:2016-07-07 11:14:04

标签: typeahead bloodhound

我正在使用以下代码在输入字段上启用typeahead 有时候区域没有显示,但是当我看到"网络xhr请求"在检查元素。 url确实返回数据。

此示例中的限制无效的另一个问题。我尝试了不同的数字,但没有一个工作

var Regions = new Bloodhound({
    datumTokenizer: Bloodhound.tokenizers.obj.whitespace('label'),
    queryTokenizer: Bloodhound.tokenizers.whitespace,
    remote: {
        url: 'https://www.domain.com/getcities?query=%QUERY',wildcard: '%QUERY'
    },
    limit: 10
});
Regions.initialize();
var hotels = new Bloodhound({
    datumTokenizer: function (datum) {
        return Bloodhound.tokenizers.whitespace(datum.value);
    },
    queryTokenizer: Bloodhound.tokenizers.whitespace,
    remote: {
        url: 'https://www.domain.com/gethotels?query=%QUERY',
         wildcard: '%QUERY', 
    },
    limit: 10

});
hotels.initialize();

function typeAhead()
{



$('#myinput').typeahead({
      hint: true,
      highlight: true,
      minLength: 2
},
{
  name: 'nba-teams',
  displayKey: 'label',
  source: Regions.ttAdapter()  ,
  templates: {
    header: '<h3 class="league-name">Cities and regions</h3>'
  }
},
{
  name: 'nhl-teams',
   displayKey: 'label',
  source: hotels.ttAdapter()  ,
  templates: {
    header: '<h3 class="league-name">Hotels</h3>'
  }
});


}

1 个答案:

答案 0 :(得分:1)

请查看以下代码。

var Regions = new Bloodhound({
    datumTokenizer: Bloodhound.tokenizers.obj.whitespace('label'),
    queryTokenizer: Bloodhound.tokenizers.whitespace,
    remote: {
        url: 'https://www.domain.com/getcities?query=%QUERY',wildcard: '%QUERY'
    }
});
Regions.initialize();
var hotels = new Bloodhound({
    datumTokenizer: function (datum) {
        return Bloodhound.tokenizers.whitespace(datum.value);
    },
    queryTokenizer: Bloodhound.tokenizers.whitespace,
    remote: {
        url: 'https://www.domain.com/gethotels?query=%QUERY',
        wildcard: '%QUERY', 
    }
});
hotels.initialize();
function typeAhead(){
    $('#myinput').typeahead({
            hint: true,
            highlight: true,
            minLength: 2
        },
        {
            name: 'nba-teams',
            displayKey: 'label',
            source: Regions.ttAdapter()  ,
            limit: 10,
            templates: {
                header: '<h3 class="league-name">Cities and regions</h3>'
            }
        },
        {
            name: 'nhl-teams',
            displayKey: 'label',
            source: hotels.ttAdapter()  ,
            limit: 10,
            templates: {
            header: '<h3 class="league-name">Hotels</h3>'
        }
    });
}