Created
April 25, 2012 09:20
-
-
Save kiecoorp/2488431 to your computer and use it in GitHub Desktop.
Autocomplete with ajax Gist
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
<?php | |
// | |
public function actionAjaxView($id) | |
{ | |
$model = $this->loadModel($id); | |
$this->renderPartial('//intervenant/_view', array('model' => $model), false, true); | |
} | |
// Ajax function | |
public function actionSearchOwner() { | |
$returnVal = array(); | |
$models = Intervenant::model()->searchOwner($_GET['term']); | |
foreach($models as $model) | |
{ | |
$returnVal[] = array( | |
'label' => $model->identity->fullname, // liste de sélection | |
'value' => $model->identity->fullname, // Affiché dans le input après sélection | |
'id' => $model->id, // ça peu servir !! | |
//'address'=>$model->address, | |
//'city'=>$model->city, | |
); | |
} | |
echo CJSON::encode($returnVal); | |
Yii::app()->end(); | |
} |
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
<?php | |
public function searchOwner($match) { | |
$criteria=new CDbCriteria; | |
$criteria->with = array( | |
"address" => array('alias' => 'address_i'), | |
"identity" => array('alias' => 'identity_i'), | |
); | |
$criteria->compare('type_id', IntervenantType::OWNER_ID); | |
$criteria->compare('concat(identity_i.firstname, identity_i.lastname)', $match, true); | |
return Intervenant::model()->findAll($criteria); | |
} |
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
<h2>Propriétaire</h2> | |
<?php if ($model->isNewRecord) : ?> | |
<div class="control-group"> | |
<label class="control-label" for="input01">Propriétaire existant ? : </label> | |
<div class="controls"> | |
<?php | |
$this->widget('zii.widgets.jui.CJuiAutoComplete', array( | |
'model' => 'Intervenant', | |
'name' => 'Intervenant[id]', | |
'attribute' => 'id', | |
'source' => $this->createUrl('intervenant/searchOwner'), | |
'options' => array( | |
'showAnim'=>'fold', | |
'select'=> | |
"js: function(event, ui) { | |
$('#owner_id').val(ui.item['id']);". | |
CHtml::ajax(array('type'=>'GET', 'url' => array("intervenant/ajaxView"), 'update' => '#owner', 'data'=> "js: {id: ui.item['id']}")) | |
."}", | |
) | |
)); | |
?> | |
<p class="help-block"></p> | |
</div> | |
</div> | |
<div id="owner"></div> | |
<input name="Owner[id]" id="owner_id" type="hidden" value="" /> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment