Skip to content

Instantly share code, notes, and snippets.

@kiecoorp
Created April 25, 2012 09:20
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kiecoorp/2488431 to your computer and use it in GitHub Desktop.
Save kiecoorp/2488431 to your computer and use it in GitHub Desktop.
Autocomplete with ajax Gist
<?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();
}
<?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);
}
<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