【问题标题】:Codeigniter and Typeaheadjs - problemsCodeigniter 和 Typeaheadjs - 问题
【发布时间】:2014-02-07 19:37:39
【问题描述】:

我的 CI 和 Typeahead 有一些问题,我没有得到任何结果;/

JS

var countries = new Bloodhound({
  datumTokenizer: function(d) { return Bloodhound.tokenizers.whitespace(d.name); },
  queryTokenizer: Bloodhound.tokenizers.whitespace,
  limit: 10,
  prefetch: {
    url: base_url + 'home/search?q=%QUERY',
    filter: function(list) {
      return $.map(list, function(country) { return { name: country }; });
    }
  }
});

countries.initialize();

$('.typeahead').typeahead(null, {
  name: 'countries',
  displayKey: 'name',
  source: countries.ttAdapter()
});

CI 控制器

public function search(){
    $query = $this->home_model->getData();
    echo json_encode($query);
}

CI 模型

private $_cityTable = 'city';

public function getData(){
    $city = $this->input->get('q');
    $query = $this->db->select('city_name')->like('city_name', $city, 'after')->get($this->_cityTable);
    foreach ($query->result_array as $key) {
        $array[] = $key['city_name'];
    }
    return $array;
}

然后查看

<input type="text" class="typeahead">

谁能帮我解决这个问题?

【问题讨论】:

  • 浏览器控制台是否出现错误?
  • nope ;/如果我在 Bloodhound 中设置本地数组,typeahead 工作正常,我认为问题出在控制器或模型中,但我不知道在哪里;/
  • 您的基本 URL 是本地的还是远程的?如果它是远程的,我认为您需要使用“远程”而不是“预取”
  • 它的本地,它的虚拟主机app.local
  • 如果您在加载页面时查看网络流量(例如使用 Chrome 调试工具中的“网络”选项卡),您是否可以看到按预期发送到浏览器的结果?

标签: codeigniter-2 typeahead.js bloodhound


【解决方案1】:

我在调试this例子的时候发现传入filter函数的parsedResponse对象的结构如下:

parsedResponse ={page: x, results:[], total_pages:x, total_results: x};

其中“x”是一个整数。

因此,要让您的代码正常工作,请更改您的过滤器函数,以便您遍历列表对象内的结果数组,即

filter: function(list) {
 return $.map(list.results, function(country) { return { name: country }; });
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-10-10
    • 2011-11-08
    • 1970-01-01
    • 1970-01-01
    • 2018-07-20
    • 2015-08-07
    • 2016-09-17
    • 1970-01-01
    相关资源
    最近更新 更多