Skip to content

Instantly share code, notes, and snippets.

@ruslanas
Created September 10, 2012 23:26
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ruslanas/3694758 to your computer and use it in GitHub Desktop.
Save ruslanas/3694758 to your computer and use it in GitHub Desktop.
Typeahead with id
/*
* CakePHP Twitter Bootstrap Typeahead with id
* ProjectsController.php
* <?php $this->set('projects', $this->Project->find('list'); $this->render('list', 'ajax'); ?>
* /app/View/Projects/list.ctp
* <?php echo json_encode($projects); ?>
* data = {'id': 'item name', ..};
* /app/View/Tasks/index.ctp
* <input type="hidden" id="TaskProjectId" name="data[Task][project_id]"/>
* <input autocomplete="off" data-provide="typeahead" class="typeahead" type="search" id="TaskProject">
*/
var finished = true;
$('#TaskProject').typeahead({
source: function(query, process){
if(!finished) {
return;
}
finished = false;
$.getJSON('/projects/names/'+query,{}, function(response){
var data = new Array();
for(var i in response) {
data.push(i+'_'+response[i]);
}
process(data);
finished = true;
});
},
highlighter: function(item) {
var parts = item.split('_');
parts.shift();
return parts.join('_');
},
updater: function(item) {
var parts = item.split('_');
$('#TaskProjectId').val(parts.shift());
return parts.join('_');
},
minLength: 0
});
@Rajan
Copy link

Rajan commented Jan 20, 2013

Thank you for sharing this. I found it useful.

@joshdcid
Copy link

you put : set('projects', $this->Project->find('list'); $this->render('list', 'ajax'); ?> inside ProjectsController function names() ???

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment