Skip to content

Instantly share code, notes, and snippets.

@wescleymatos
Created December 1, 2011 19:13
Show Gist options
  • Save wescleymatos/1419104 to your computer and use it in GitHub Desktop.
Save wescleymatos/1419104 to your computer and use it in GitHub Desktop.
autocomplete com cake e jqueryUI
function admin_busca(){
$this->layout = "ajax";
$list = $this->Car->find('list',array('fields' => array('Car.id', 'Car.type')));
foreach($list as $key => $value){
$array[] = array("label" => $value, "value" => $value, "id" => $key);
}
echo json_encode($array);
}
========================================
<div class="ui-widget">
<?php echo $this->Form->input('teste', array('id'=>'teste'));?>
</div>
========================================
var cache = {},
lastXhr;
$("#teste").autocomplete({
minLength: 2,
source: function(request, response) {
var term = request.term;
if ( term in cache ) {
response(cache[ term ]);
return;
}
lastXhr = $.getJSON("busca", request, function(data, status, xhr) {
cache[term] = data;
if (xhr === lastXhr) {
response(data);
}
});
},
select: function(event, ui) {
alert(ui.item.id);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment