Created
September 10, 2012 23:26
-
-
Save ruslanas/3694758 to your computer and use it in GitHub Desktop.
Typeahead with id
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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 | |
}); |
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
Thank you for sharing this. I found it useful.