1. Download Tokeninput extension for Yii from here.
2. Extract it in your project under protected/extensions/tokeninput
3. Add extension to your main.php file
'import' => 'application.extensions.tokeninput',
4. Now change your view code
<?php $this->widget('ext.tokeninput.TokenInput', array(
'model' => $person,
'attribute' => 'name',
'id' =>'director',
'url' => array('person/personsearch'),
'options' => array(
'allowCreation' => true,
'preventDuplicates' => true,
'resultsFormatter' => 'js:function(item){ return "<li><p>" + item.name + "</p></li>" }',
'theme' => 'facebook',
)
)); ?>
This will give you comma saprated id of persons e.i. 22,33,44
5. here is your controller code to get the data from database. I am using Mysql database
public function actionPersonsearch($q){
$term = trim($q);
$response = array();
$criteria=new CDbCriteria;
$criteria->alias = "PE";
$condition = "PE.name like '%" . $term . "%'";
$criteria->condition = $condition;
$dataProvider = new CActiveDataProvider(get_class(Person::model()), array(
'criteria'=>$criteria,'pagination'=>false,
));
$persons = $dataProvider->getData();
$person_list = array();
foreach($persons as $person) {
$person_list[] = array(
'name'=>$person->name,
'id'=>$person->id,
);
}
$response = CJSON::encode($person_list);
echo $response;
}
Post a comment if you have any issue .

No comments:
Post a Comment