【发布时间】:2017-11-13 21:32:13
【问题描述】:
我正在尝试使用 Jquery 自动完成从我的 SQL 表中获取数据,当我搜索数据时,数据被正确返回,但它没有通过下拉框以图形方式显示。
我的搜索.ctp
<?php use Cake\Routing\Router; ?>
<?php echo $this->Form->input('id', ['type' => 'text']); ?>
<script>
jQuery('#id').autocomplete({
source:'<?php echo Router::url(array('controller' => 'Invoices', 'action' => 'search')); ?>'
});
</script>
我的搜索功能
public function search()
{
if ($this->request->is('ajax'))
{
$name = $this->request->query['term'];
$resultArr = $this
->Invoices
->find()
->where(
['Invoices.id LIKE' => ($name . '%')],
['Invoices.id' => 'string']
);
$resultsArr = [];
foreach ($resultArr as $result) {
$resultsArr[] = (strval($result['id']));
}
$this->set('invoices', $resultsArr);
// This line is what handles converting your array into json
// To get this to work you must load the request handler
$this->set('_serialize', ['invoices']);
}
}
如您所见,它正在返回数据但没有下拉菜单。
【问题讨论】:
-
看起来它还返回了很多“不是 json 的东西”,这很可能会破坏接收端的 json 解析。
-
@Randall 糟糕,我忘了删除一些我编辑过的代码,如果这样更好的话。
-
这很奇怪,因为 .autocomplete 的基本行为是在所选字段下添加一个 div。当您键入某些内容时,它显然会命中服务器,并且服务器正在返回一个 json 项目数组。所以...我唯一的猜测是,您是否有一些可能与选项显示冲突的 CSS?
-
另外,它是jquery ui.autocomplete的新版本吗?我问是因为旧版本只是寻找换行符,而不是 json 数组。
-
看看你的 JSON 输出,我很确定自动完成小部件不支持数据嵌套在任意属性中的对象。
标签: php jquery css cakephp-3.0 jquery-ui-autocomplete