Skip to content

Instantly share code, notes, and snippets.

@zastava2012
Forked from geuis/remote-typeahead.js
Created March 22, 2012 01:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zastava2012/2155082 to your computer and use it in GitHub Desktop.
Save zastava2012/2155082 to your computer and use it in GitHub Desktop.
Remote data querying for Twitter Bootstrap 2.0 Typeahead without modifications (debugging)
<!-- VIEW start -->
<script src="/js/bootstrap/js/bootstrap-typeahead.js" type="text/javascript"></script>
<input type="text" id="searchinput" data-provide="typeahead">
<script>
// Charles Lawrence - Feb 16, 2012. Free to use and modify. Please attribute back to @geuis if you find this useful
// Twitter Bootstrap Typeahead doesn't support remote data querying. This is an expected feature in the future. In the meantime, others have submitted patches to the core bootstrap component that allow it.
// The following will allow remote autocompletes *without* modifying any officially released core code.
// If others find ways to improve this, please share.
var autocomplete = $('#searchinput').typeahead()
.on('keyup', function(ev){
/* console.log(ev); */
ev.stopPropagation();
ev.preventDefault();
//filter out up/down, tab, enter, and escape keys
if( $.inArray(ev.keyCode,[40,38,9,13,27]) === -1 ){
var self = $(this);
//set typeahead source to empty
self.data('typeahead').source = [];
//active used so we aren't triggering duplicate keyup events
if( !self.data('active') && self.val().length > 0){
self.data('active', true);
//Do data request. Insert your own API logic here.
/* console.log($(this).val()); */
//Do data request. Insert your own API logic here.
$.getJSON("/site/search",{
q: $(this).val()
}, function(data) {
/* console.log(data.results.length); */
//set this to true when your callback executes
self.data('active',true);
//Filter out your own parameters. Populate them into an array, since this is what typeahead's source requires
var arr = [],
i=data.results.length;
console.log(data.results.length);
while(i--){
arr[i] = data.results[i].text
console.log(data.results[i].text);
}
//set your results into the typehead's source
self.data('typeahead').source = arr;
//trigger keyup on the typeahead to make it search
self.trigger('keyup');
//All done, set to false to prepare for the next remote query.
self.data('active', false);
});
}
}
});
</script>
<!-- VIEW end -->
<!-- CONTROLLER start -->
public function actionSearch($q)
{
$term = trim($q);
$models=Data::model()->findAll(array(
'condition'=>'title LIKE :keyword',
'order'=>'title',
'limit'=>10,
'params'=>array(':keyword'=>"%$term%")
));
$suggest=array();
foreach($models as $model) {
$suggest[] = array('id' => $model->id, 'name' => $model->title);
}
header('Content-type: application/json');
echo CJSON::encode($suggest);
Yii::app()->end();
}
<!-- CONTROLLER end -->
@luxlyny
Copy link

luxlyny commented May 17, 2013

@zastava2012, I am Yii-bie. and tried your code without luck... would you mind to update to a work code...

thanks in advance

@chiragdhori
Copy link

did anyone tell me how to retrive data from database. . i tired lots but unable to get data from database. from array its working fine. i dnt get it wheater even calls or not. any suggestion code than pls hel me out.

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