Last active
March 13, 2017 21:07
-
-
Save lalviarez/86f08336e6331d7f4400f77ee0224d0f 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.'); | |
} | |
} | |
} |
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 common\models; | |
use Yii; | |
/** | |
* This is the model class for table "consejo_comunal". | |
* | |
* @property integer $id | |
* @property string $fecha_inicio | |
* @property string $fecha_culminacion | |
* @property string $nombre | |
* @property string $codigo_registro | |
* @property integer $parroquia_id | |
* @property double $superficie | |
* @property string $nombre_corredor | |
* | |
* @property Actividad[] $actividads | |
* @property Parroquias $parroquia | |
* @property Equipo[] $equipos | |
* @property Estatus $estatus | |
* @property Limites $limites | |
* @property Observaciones $observaciones | |
* @property OtrasOrganizaciones[] $otrasOrganizaciones | |
* @property Problematica[] $problematicas | |
* @property ProyectosEjecutados[] $proyectosEjecutados | |
* @property ServiciosSociales[] $serviciosSociales | |
* @property Voceros[] $voceros | |
*/ | |
class ConsejoComunal extends \yii\db\ActiveRecord | |
{ | |
public $estado_id; | |
public $municipio_id; | |
/** | |
* @inheritdoc | |
*/ | |
public static function tableName() | |
{ | |
return 'consejo_comunal'; | |
} | |
/** | |
* @inheritdoc | |
*/ | |
public function rules() | |
{ | |
return [ | |
[['fecha_inicio', 'nombre', 'codigo_registro', 'parroquia_id', 'estado_id', 'municipio_id'], 'required'], | |
[['fecha_inicio', 'fecha_culminacion'], 'safe'], | |
[['parroquia_id'], 'integer'], | |
[['estado_id'], 'integer'], | |
[['municipio_id'], 'integer'], | |
[['superficie'], 'number'], | |
[['nombre', 'codigo_registro', 'nombre_corredor'], 'string', 'max' => 256], | |
[['parroquia_id'], 'exist', 'skipOnError' => true, 'targetClass' => Parroquias::className(), 'targetAttribute' => ['parroquia_id' => 'id']], | |
]; | |
} | |
/** | |
* @inheritdoc | |
*/ | |
public function attributeLabels() | |
{ | |
return [ | |
'id' => 'ID', | |
'fecha_inicio' => 'Fecha Inicio', | |
'fecha_culminacion' => 'Fecha Culminacion', | |
'nombre' => 'Nombre', | |
'codigo_registro' => 'Codigo Registro', | |
'estado_id' => 'Estado', | |
'municipio_id' => 'Municipio', | |
'parroquia_id' => 'Parroquia', | |
'superficie' => 'Superficie', | |
'nombre_corredor' => 'Nombre Corredor', | |
]; | |
} | |
/** | |
* @return \yii\db\ActiveQuery | |
*/ | |
public function getActividads() | |
{ | |
return $this->hasMany(Actividad::className(), ['consejo_comunal_id' => 'id']); | |
} | |
/** | |
* @return \yii\db\ActiveQuery | |
*/ | |
public function getParroquia() | |
{ | |
return $this->hasOne(Parroquias::className(), ['id' => 'parroquia_id']); | |
} | |
/** | |
* @return \yii\db\ActiveQuery | |
*/ | |
public function getEquipos() | |
{ | |
return $this->hasMany(Equipo::className(), ['consejo_comunal_id' => 'id']); | |
} | |
/** | |
* @return \yii\db\ActiveQuery | |
*/ | |
public function getEstatus() | |
{ | |
return $this->hasOne(Estatus::className(), ['consejo_comunal_id' => 'id']); | |
} | |
/** | |
* @return \yii\db\ActiveQuery | |
*/ | |
public function getLimites() | |
{ | |
return $this->hasOne(Limites::className(), ['consejo_comunal_id' => 'id']); | |
} | |
/** | |
* @return \yii\db\ActiveQuery | |
*/ | |
public function getObservaciones() | |
{ | |
return $this->hasOne(Observaciones::className(), ['consejo_comunal_id' => 'id']); | |
} | |
/** | |
* @return \yii\db\ActiveQuery | |
*/ | |
public function getOtrasOrganizaciones() | |
{ | |
return $this->hasMany(OtrasOrganizaciones::className(), ['consejo_comunal_id' => 'id']); | |
} | |
/** | |
* @return \yii\db\ActiveQuery | |
*/ | |
public function getProblematicas() | |
{ | |
return $this->hasMany(Problematica::className(), ['consejo_comunal_id' => 'id']); | |
} | |
/** | |
* @return \yii\db\ActiveQuery | |
*/ | |
public function getProyectosEjecutados() | |
{ | |
return $this->hasMany(ProyectosEjecutados::className(), ['consejo_comunal_id' => 'id']); | |
} | |
/** | |
* @return \yii\db\ActiveQuery | |
*/ | |
public function getServiciosSociales() | |
{ | |
return $this->hasMany(ServiciosSociales::className(), ['consejo_comunal_id' => 'id']); | |
} | |
/** | |
* @return \yii\db\ActiveQuery | |
*/ | |
public function getVoceros() | |
{ | |
return $this->hasMany(Voceros::className(), ['consejo_comunal_id' => 'id']); | |
} | |
/** | |
* @inheritdoc | |
* @return ConsejoComunalQuery the active query used by this AR class. | |
*/ | |
public static function find() | |
{ | |
return new ConsejoComunalQuery(get_called_class()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment