Wednesday, November 21, 2012

Re: CakePHP 2.2 and Autocomplete Jquery

This code snippet needs refactoring in order to use JsHelper, but it works:

controller code (Controllers/PeopleController.php):
function autoComplete() {
$this->autoRender = false;
$search_string = trim($this->request->query['term']);
if (!is_null($search_string) && $search_string != '') {
$people = $this->Person->find('all',
array(
'conditions' => array (
'OR' => array (
'Person.first_name LIKE'   => '%' . $search_string . '%',
'Person.last_name LIKE'    => '%' . $search_string . '%',
'Person.family_info LIKE'  => '%' . $search_string . '%',
'Person.comments LIKE'     => '%' . $search_string . '%',
'Person.home_street LIKE'  => '%' . $search_string . '%',
'Person.work_street_1 LIKE'=> '%' . $search_string . '%',
'Person.work_street_2 LIKE'=> '%' . $search_string . '%'
)
),
'fields' => array(
'Person.first_name', 'Person.last_name',
'Person.active', 'Person.requested_do_not_call',
'Person.home_street', 'Person.home_city',
'Person.home_state', 'Person.home_zip',
'Person.work_street_1', 'Person.work_street_2',
'Person.work_city', 'Person.work_state', 'Person.work_zip',
'Person.id', 'Person.home_phone', 'Person.mobile_phone',
'Person.tty', 'Person.video_phone', 'Person.email'
),
'order' => array(
'Person.active DESC',
'Person.last_name ASC',
'Person.first_name ASC'
),
'recursive' => 0
)
);
$i=0;
foreach($people as $person){
$response[$i]['id'] = $person['Person']['id'];
$response[$i]['label'] = $person['Person']['last_name'] . ', ' . $person['Person']['first_name'];
$response[$i]['value'] = $person['Person']['last_name'] . ', ' . $person['Person']['first_name'];
$i++;
}
echo json_encode($response);
}
}


view code (Views/People/search.ctp):
<?php
echo $this->Html->script('https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js', array('inline' => false));
echo $this->Html->script('https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.21/jquery-ui.js', array('inline' => false));
echo $this->Html->script('autocomplete/jquery.ui.autocomplete.html.js', array('inline' => false));
    ob_start();
?>
$(document).ready(function(){
$("#PersonQ").autocomplete({
source: "../people/autoComplete.json",
minLength: 2
});
$("input:submit").button();
$("#PersonQ").autocomplete({
select: function(event, ui) {
selected_id = ui.item.id;
$('#PersonSearchForm').append('<input id="PersonId" type="hidden" name="data[Person][id]" value="' + selected_id + '" />');
$('#PersonSearchForm').submit();
}
});
$("#PersonQ").autocomplete({
open: function(event, ui) {
$('#PersonId').remove();
}
});
});
<?php
$jscript = ob_get_contents();
ob_end_clean();
echo $this->Html->scriptBlock($jscript, array('inline' => false));
?>

On Tuesday, November 20, 2012 5:34:56 AM UTC-5, franscelstain wrote:
if anyone can give an example?

Pada Senin, 12 November 2012 11:01:40 UTC+7, franscelstain menulis:
Is there someone who can help me make jquery autocomplete using CakePHP 2.2

--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP
 
---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com.
To unsubscribe from this group, send email to cake-php+unsubscribe@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php?hl=en.
 
 

No comments: