预先输入多个远程数据集 - 无法通过If语句进行选择

时间:2015-09-01 09:05:47

标签: if-statement typeahead.js typeahead twitter-typeahead bloodhound

我正在使用Twitter Typeahead库的v0.10.5,并使用远程数据集示例连接到两个数据集,但在尝试根据用户搜索的数据集运行代码时遇到问题。

我尝试过使用If语句,但这似乎不起作用(控制台日志中没有任何内容)。

有什么明显我做错了吗?是否有不同的方法在on:selected事件中指定特定数据集?

var addresses = new Bloodhound({
  name: "addresses",
  datumTokenizer: Bloodhound.tokenizers.obj.whitespace('ADDRESS'),
  queryTokenizer: Bloodhound.tokenizers.whitespace,
  remote: {
    url: 'php/search_addresses.php?query=%QUERY',
    wildcard: '%QUERY',
    ajax: {
      beforeSend: function (jqXhr, settings) {
        $("#searchicon").removeClass("fa-search").addClass("fa-refresh fa-spin");
      },
      complete: function (jqXHR, status) {
        $('#searchicon').removeClass("fa-refresh fa-spin").addClass("fa-search");
      }
    }
  },
  limit: 50
});

var planning = new Bloodhound({
  name: "planning",
  datumTokenizer: Bloodhound.tokenizers.obj.whitespace('APPLICATION_NUMBER'),
  queryTokenizer: Bloodhound.tokenizers.whitespace,
  remote: {
    url: 'php/search_planning.php?query=%QUERY',
    wildcard: '%QUERY',
    ajax: {
      beforeSend: function (jqXhr, settings) {
        $("#searchicon").removeClass("fa-search").addClass("fa-refresh fa-spin");
      },
      complete: function (jqXHR, status) {
        $('#searchicon').removeClass("fa-refresh fa-spin").addClass("fa-search");
      }
    }
  },
  limit: 50
});

addresses.initialize();
planning.initialize();

$("#searchbox").typeahead({
  minLength: 5,
  hint: false
},
{
  name: 'addresses',
  displayKey: 'ADDRESS',
  source: addresses.ttAdapter(),
  templates: {
    header: "<h4 class='typeahead-header'>Addresses</h4>"
  }
},
{
  name: 'planning',
  displayKey: 'APPLICATION_NUMBER',
  source: planning.ttAdapter(),
  templates: {
    header: "<h4 class='typeahead-header'>Planning Applications</h4>"
  }
}).on("typeahead:selected", function (obj, datum) {
  if (datum.name === "addresses") {
    console.log("address selected"); 
  }
  if (datum.name === "planning") {
    console.log("planning selected");
  }
});

0 个答案:

没有答案