자동 완성 기능이 작동하지 않아 오류가 발견되지 않습니다.JqueryUI 자동 완성이 CakePHP 2.0에서 작동하지 않습니다
JQUERY :
<script>
$(function() {
$('#autoComplete').autocomplete({
//source: "/groceries/items/autoComplete", ///This works but response isn't formatted correctly'
//dataType: "json"
minLength: 2,
source: function(request, response) {
$.ajax({
url: "/groceries/items/autoComplete",
dataType: "jsonp",
data: {
featureClass: "P",
style: "full",
maxRows: 12,
term: request.term
},
success: function(data) {
response($.map(data, function(el) {
return { label: el.id, value: el.name }
}));
}
});
}
});
});
컨트롤러 :
public function autoComplete() {
Configure::write('debug', 0);
$this->layout = 'ajax';
$query = $_GET['term'];
$items = $this->Item->find('all', array(
'conditions' => array('Item.name LIKE' => $query . '%'),
'fields' => array('name', 'id', 'category_id'),
'group' => array('name')));
$this->set('items', $items);
}
형태 :
,995 943,210응답 : 해명
0: {Item:{name:Cake, id:6, category_id:null}}
1: {Item:{name:Carrot Cake, id:9, category_id:null}}
2: {Item:{name:Carrots, id:8, category_id:null}}
3: {Item:{name:Casserole, id:11, category_id:null}}
4: {Item:{name:Cauliflower, id:10, category_id:null}}
편집.
JqueryUI는 label과 value를 기대하며 그 맵은 배열을 재 배열해야하지만 실제로는 그렇지 않습니다. 어떤 아이디어?
더 나은 해결책을 발견했습니다. 이것은 컨트롤러에서 완전히 수행됩니다. 보기가 필요하지 않습니다. 당신은 잊었처럼
public function autoComplete() {
Configure::write('debug', 0);
*$this->autoRender=false;*
$this->layout = 'ajax';
$query = $_GET['term'];
$items = $this->Item->find('all', array(
'conditions' => array('Item.name LIKE' => $query . '%'),
'fields' => array('name', 'id', 'category_id'),
'group' => array('name')));
*$i=0;
foreach($items as $item){
$response[$i]['value']="'".$item['Item']['id']."'";
$response[$i]['label']="'".$item['Item']['name']."'";
$i++;
}
echo json_encode($response);*
}
당신이 아약스가 사용하는 폼을 표시 할 수 있습니다 컨트롤러에서 코드 자동 완성? 이것은 테스트 과정에서 도움이 될 것입니다. 이 문제를 해결하는 데 도움이되고 싶습니다. –
디버그를 0으로 설정하는 것을 잊지 마십시오. Configure :: write ('debug', 0); –
@ChuckBurgess 추가 정보로 질문을 업데이트했습니다. – Chris