0
我是CakePHP3(+ stackoverflow)的新手,嘗試實現自動完成。我想將自動完成功能附加到我的index.ctp中的searchinput中。Cakephp3 jQuery自動完成
- 請求返回所有汽車對象(忽略get.term)
- 的響應沒有連接到searchinput
將是罰款得到一些幫助 - 謝謝!
index.ctp
<?php
echo $this->Form->create('Cars');
echo $this->Form->input('name', [
'label' => 'Search',
'id' => 'autocomplete',
'class' => 'ui-autocomplete'
]);
echo $this->Form->button('Search', ['type' => 'submit']);
echo $this->Form->end();
?>
<script>
$("#autocomplete").autocomplete(
{
search: function() {},
source: function (request, response)
{
$.ajax(
{
source: "/cars/autocomplete",
dataType: "json",
data:
{
term: request.term,
},
success: function (data)
{
response(data);
console.log(data);
}
});
},
minLength: 2
});
</script>
CarsController.php
function autocomplete() {
if ($this->request->is('ajax','get')) {
$term = $this->request->data["term"];
$terms = $cars->find('all', [
'conditions' => ['Cars.name >' => $term],
'limit' => 10
]);
$data = array();
foreach($terms as $term) {
$row = $term->name;
array_push($data, $row);
}
// $this->layout = 'ajax';
$this->set('terms', $terms);
echo json_encode($data);
// return json_encode($data);
}
else {
echo json_encode('Nothings found');
}
}