-
-
Save kkoci/b15f4f774bd13737a4cbfb9e23c34df3 to your computer and use it in GitHub Desktop.
Yii2 combolist ajax
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 | |
use yii\helpers\Html; | |
use yii\widgets\ActiveForm; | |
use yii\helpers\ArrayHelper; | |
use yii\helpers\Url; | |
use yii\web\View; | |
/* @var $this yii\web\View */ | |
/* @var $model common\models\ConsejoComunal */ | |
/* @var $form yii\widgets\ActiveForm */ | |
$url = Url::to(['ajax-municipio']); | |
$this->registerJs( | |
" | |
$('#consejocomunal-estado_id').on('change', function(e) { | |
$.ajax({ | |
url: '".$url."', | |
data: {estado_id: $('#consejocomunal-estado_id').val()}, | |
success: function(html) { | |
console.log(html); | |
$('#".Html::getInputId($model, 'municipio_id')."').html(html); | |
}, | |
error: function(result) { | |
alert('Estado no encontrado'); | |
} | |
}); | |
}); | |
", View::POS_READY); | |
$url = Url::to(['ajax-parroquia']); | |
$this->registerJs( | |
" | |
$('#consejocomunal-municipio_id').on('change', function(e) { | |
$.ajax({ | |
url: '".$url."', | |
data: {municipio_id: $('#consejocomunal-municipio_id').val()}, | |
success: function(html) { | |
console.log(html); | |
$('#".Html::getInputId($model, 'parroquia_id')."').html(html); | |
}, | |
error: function(result) { | |
alert('Municipio no encontrado'); | |
} | |
}); | |
}); | |
", View::POS_READY); | |
?> | |
<div class="consejo-comunal-form"> | |
<?php $form = ActiveForm::begin(); ?> | |
<?= $form->field($model, 'fecha_inicio')->textInput() ?> | |
<?= $form->field($model, 'fecha_culminacion')->textInput() ?> | |
<?= $form->field($model, 'nombre')->textInput(['maxlength' => true]) ?> | |
<?= $form->field($model, 'codigo_registro')->textInput(['maxlength' => true]) ?> | |
<?= $form->field($model, 'estado_id')->dropDownList( | |
ArrayHelper::map($estados, 'id','estado'), | |
[ | |
'prompt'=>'--Seleccione--', | |
] | |
); ?> | |
<?= $form->field($model, 'municipio_id')->dropDownList( | |
ArrayHelper::map($municipios, 'id','municipio'), | |
[ | |
'prompt'=>'--Seleccione--', | |
] | |
); ?> | |
<?= $form->field($model, 'parroquia_id')->dropDownList( | |
ArrayHelper::map($parroquias, 'id','parroquia'), | |
[ | |
'prompt'=>'--Seleccione--', | |
] | |
); ?> | |
<?= $form->field($model, 'superficie')->textInput() ?> | |
<?= $form->field($model, 'nombre_corredor')->textInput(['maxlength' => true]) ?> | |
<div class="form-group"> | |
<?= Html::submitButton($model->isNewRecord ? 'Siguiente >>' : 'Siguiente >>', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?> | |
</div> | |
<?php ActiveForm::end(); ?> | |
</div> |
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 | |
namespace frontend\controllers; | |
use Yii; | |
use common\models\ConsejoComunal; | |
use common\models\ConsejoComunalSearch; | |
use common\models\Estados; | |
use common\models\Municipios; | |
use common\models\Parroquias; | |
use yii\web\Controller; | |
use yii\web\NotFoundHttpException; | |
use yii\filters\VerbFilter; | |
use yii\helpers\Json; | |
/** | |
* ConsejoComunalController implements the CRUD actions for ConsejoComunal model. | |
*/ | |
class ConsejoComunalController extends Controller | |
{ | |
/** | |
* @inheritdoc | |
*/ | |
public function behaviors() | |
{ | |
return [ | |
'verbs' => [ | |
'class' => VerbFilter::className(), | |
'actions' => [ | |
'delete' => ['POST'], | |
], | |
], | |
]; | |
} | |
/** | |
* Lists all ConsejoComunal models. | |
* @return mixed | |
*/ | |
public function actionIndex() | |
{ | |
$searchModel = new ConsejoComunalSearch(); | |
$dataProvider = $searchModel->search(Yii::$app->request->queryParams); | |
return $this->render('index', [ | |
'searchModel' => $searchModel, | |
'dataProvider' => $dataProvider, | |
]); | |
} | |
/** | |
* Displays a single ConsejoComunal model. | |
* @param integer $id | |
* @return mixed | |
*/ | |
public function actionView($id) | |
{ | |
return $this->render('view', [ | |
'model' => $this->findModel($id), | |
]); | |
} | |
/** | |
* Creates a new ConsejoComunal model. | |
* If creation is successful, the browser will be redirected to the 'view' page. | |
* @return mixed | |
*/ | |
public function actionCreate() | |
{ | |
$model = new ConsejoComunal(); | |
$estados = Estados::find()->all(); | |
$municipios = []; | |
$parroquias = []; | |
if ($model->load(Yii::$app->request->post()) && $model->save()) { | |
return $this->redirect(['estatus/create', 'consejo_comunal_id' => $model->id]); | |
} else { | |
return $this->render('create', [ | |
'model' => $model, | |
'estados' => $estados, | |
'municipios' => $municipios, | |
'parroquias' => $parroquias, | |
]); | |
} | |
} | |
/** | |
* Updates an existing ConsejoComunal model. | |
* If update is successful, the browser will be redirected to the 'view' page. | |
* @param integer $id | |
* @return mixed | |
*/ | |
public function actionUpdate($id) | |
{ | |
$model = $this->findModel($id); | |
$model->estado_id = $model->parroquia->municipio->estado->id; | |
$model->municipio_id = $model->parroquia->municipio->id; | |
$parroquias = Parroquias::find() | |
->where(['municipio_id'=>$model->municipio_id])->all(); | |
$municipios = Municipios::find() | |
->where(['estado_id'=>$model->estado_id])->all(); | |
$estados = Estados::find()->all(); | |
if ($model->load(Yii::$app->request->post()) && $model->save()) { | |
return $this->redirect(['estatus/update', 'consejo_comunal_id' => $model->id]); | |
} else { | |
return $this->render('update', [ | |
'model' => $model, | |
'estados' => $estados, | |
'municipios' => $municipios, | |
'parroquias' => $parroquias, | |
]); | |
} | |
} | |
/** | |
* Deletes an existing ConsejoComunal model. | |
* If deletion is successful, the browser will be redirected to the 'index' page. | |
* @param integer $id | |
* @return mixed | |
*/ | |
public function actionDelete($id) | |
{ | |
$this->findModel($id)->delete(); | |
return $this->redirect(['index']); | |
} | |
/** | |
* LJAH | |
* Finds the Municipios model based on estado_id value. | |
* @param integer $id | |
* @return Json Municipios data if success | |
*/ | |
public function actionAjaxMunicipio($estado_id) | |
{ | |
if (Yii::$app->request->isAjax) | |
{ | |
if (($model = Municipios::find() | |
->where(['estado_id' => $estado_id])->all()) !== null) { | |
if (count($model) > 0) | |
{ | |
foreach($model as $municipio) | |
{ | |
echo '<option value="'.$municipio->id.'">'.$municipio->municipio.'</option>'; | |
} | |
} | |
else | |
{ | |
echo '<option>No hay resultados</option>'; | |
} | |
} else { | |
throw new NotFoundHttpException('The requested page does not exist.'); | |
} | |
} | |
else { | |
throw new NotFoundHttpException('The requested page does not exist.'); | |
} | |
} | |
/** | |
* LJAH | |
* Finds the Parroquias model based on municipio_id value. | |
* @param integer $id | |
* @return Json Municipios data if success | |
*/ | |
public function actionAjaxParroquia($municipio_id) | |
{ | |
if (Yii::$app->request->isAjax) | |
{ | |
if (($model = Parroquias::find() | |
->where(['municipio_id' => $municipio_id])->all()) !== null) { | |
if (count($model) > 0) | |
{ | |
foreach($model as $parroquia) | |
{ | |
echo '<option value="'.$parroquia->id.'">'.$parroquia->parroquia.'</option>'; | |
} | |
} | |
else | |
{ | |
echo '<option>No hay resultados</option>'; | |
} | |
} else { | |
throw new NotFoundHttpException('The requested page does not exist.'); | |
} | |
} | |
else { | |
throw new NotFoundHttpException('The requested page does not exist.'); | |
} | |
} | |
/** | |
* Finds the ConsejoComunal model based on its primary key value. | |
* If the model is not found, a 404 HTTP exception will be thrown. | |
* @param integer $id | |
* @return ConsejoComunal the loaded model | |
* @throws NotFoundHttpException if the model cannot be found | |
*/ | |
protected function findModel($id) | |
{ | |
if (($model = ConsejoComunal::findOne($id)) !== null) { | |
return $model; | |
} else { | |
throw new NotFoundHttpException('The requested page does not exist.'); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment