สอบถามครับ อยากให้ Dropdown ในส่วนของ Update ดึงค่าเดิมมาแสดงด้วยทำอย่างไรครับ

wave
final888
1,957
สังคมนักพัฒนาระบบ (Community)
สอบถามครับ อยากให้ Dropdown ในส่วนของ Update ดึงค่าเดิมมาแสดงด้วยทำอย่างไรครับ

อยากให้เวลา Update ข้อมูลในส่วนของ Dropdown เอาค่าเดิมมาแสดงด้วยต้องทำอย่างไรครับ 

ส่วนของ _form

<?php

use yii\helpers\Html;
use yii\helpers\Url;
use yii\widgets\ActiveForm;
use yii\helpers\ArrayHelper;
use app\models\Department;
use app\models\Prorisk;
use app\models\Prorisksub;
use kartik\depdrop\DepDrop;
use app\models\Risktype;
use app\models\Severity;

$department = Department::find()->all();



/* @var $this yii\web\View */
/* @var $model app\models\Risk */
/* @var $form yii\widgets\ActiveForm */
?>

<div class="risk-form">

    <?php
    $items = ArrayHelper::map(Department::find()->all(), 'dep_id', 'name');
    $pror = ArrayHelper::map(Prorisk::find()->all(), 'pro_id', 'name');
    $pros = ArrayHelper::map(Prorisksub::find()->all(), 'pro_id', 'name');
    ?>

    <?php $form = ActiveForm::begin(); ?>


    <div class="col-lg-12">
        <?php echo $form->field($model, 'dep_destination')->dropDownList($items, ['prompt' => 'กรุณาเลือกหน่วยงานที่ห่วงใย']); ?>
    </div>
    <div class="col-lg-3"> 
        <?php
        $array = Risktype::find()
                ->all();
        $risktype = ArrayHelper::map($array, 'type_id', 'name');
        echo $form->field($model, 'pro_type')->dropDownList($risktype, ['id' => 'type_id', 'prompt' => 'เลือก...'])
        ?>      
    </div>

    <div class="col-lg-3"> 
        <?php
        echo $form->field($model, 'pro_risk')->widget(DepDrop::classname(), [
            'data' => !empty($type) ? $type : [],
            'options' => ['id' => 'risk_type'],
            'pluginOptions' => [
                'depends' => ['type_id'],
                'placeholder' => 'เลือก...',
                'url' => Url::to(['/insertrisk/risk/gettype'])
            ]
        ]);
        ?>

    </div>
    <div class="col-lg-3"> 
        <?php
        echo $form->field($model, 'pro_sub')->widget(DepDrop::classname(), [
            'data' => !empty($amp) ? $amp : [],
            'options' => ['id' => 'pro_id'],
            'pluginOptions' => [
                'depends' => ['risk_type'],
                'placeholder' => 'เลือก...',
                'url' => Url::to(['/insertrisk/risk/getamp'])
            ]
        ]);
        ?>
    </div>

    <div class="col-lg-3"> 
        <?php
        echo $form->field($model, 'severity')->widget(DepDrop::classname(), [
            'data' => !empty($severity) ? $severity : [],
            'options' => ['id' => 'id_severity'],
            'pluginOptions' => [
                'depends' => ['type_id'],
                'placeholder' => 'เลือก...',
                'url' => Url::to(['/insertrisk/risk/getseverity'])
            ]
        ]);
        ?>

    </div>
    <div class="col-lg-12"> 
        <?= $form->field($model, 'detail')->textarea(['rows' => '6']) ?>
    </div>

    <div class="col-lg-12">
        <?php
        if (Yii::$app->user->identity->edit_acc == '0') {
            echo $form->field($model, 'solve')->textarea(['rows' => '6']);
        }
        ?>

    </div>
    <div class="col-lg-12">

        <?php
        if (Yii::$app->user->identity->edit_acc == '0') {
            echo $form->field($model, 'leader_solve')->textarea(['rows' => '6']);
        }
        ?>
    </div>


    <div class="form-group col-lg-12">
        <?= Html::submitButton('Save', ['class' => 'btn btn-success']) ?>
    </div>

    <?php ActiveForm::end(); ?>


ส่วนของ Controller

<?php

namespace app\modules\insertrisk\controllers;

use Yii;
use app\models\Risk;
use app\modules\insertrisk\models\RiskSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use app\models\Prorisk;
use app\models\Prorisksub;
use app\models\Risktype;
use app\models\Severity;
use yii\helpers\Json;
use yii\helpers\ArrayHelper;
/**
 * RiskController implements the CRUD actions for Risk model.
 */
class RiskController extends Controller {

    /**
     * {@inheritdoc}
     */
    
    public function behaviors() {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['POST'],
                ],
            ],
        ];
    }

    /**
     * Lists all Risk models.
     * @return mixed
     */
    public function actionIndex() {
        $searchModel = new RiskSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

        return $this->render('index', [
                    'searchModel' => $searchModel,
                    'dataProvider' => $dataProvider,
        ]);
    }

    /**
     * Displays a single Risk model.
     * @param integer $id
     * @return mixed
     * @throws NotFoundHttpException if the model cannot be found
     */
    public function actionView($id) {
        return $this->render('view', [
                    'model' => $this->findModel($id),
        ]);
    }

    /**
     * Creates a new Risk model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate() {
        $model = new Risk();

        if ($model->load(Yii::$app->request->post())) {
            $model->dep_source = Yii::$app->user->identity->dep;
            $model->date_regis = Yii::$app->formatter->asDate('now', 'yyyy-MM-dd');
            $model->save();

            return $this->redirect(['view', 'id' => $model->id]);
        }

        return $this->render('create', [
                    'model' => $model,
        ]);
    }

    /**
     * Updates an existing Risk model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param integer $id
     * @return mixed
     * @throws NotFoundHttpException if the model cannot be found
     */
    public function actionUpdate($id) {
        $model = $this->findModel($id);
 
        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->id]);
        }

        return $this->render('update', [
                    'model' => $model,
        ]);
    }

    /**
     * Deletes an existing Risk model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     * @param integer $id
     * @return mixed
     * @throws NotFoundHttpException if the model cannot be found
     */
    public function actionDelete($id) {
        $this->findModel($id)->delete();

        return $this->redirect(['index']);
    }

    /**
     * Finds the Risk model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param integer $id
     * @return Risk the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id) {
        if (($model = Risk::findOne($id)) !== null) {
            return $model;
        }

        throw new NotFoundHttpException('The requested page does not exist.');
    }

       public function actionGettype() {
        $parents = \Yii::$app->request->post('depdrop_parents');
        if ($parents) {
            $type_id = $parents[0];
            $out = Prorisk::find()
                    ->select('pro_id as id,name as name')
                    ->where(['risk_type' => $type_id])
                    ->asArray()
                    ->all();
            return Json::encode(['output' => $out, 'selected' => '']);
        }
    }
    
   public function actionGetamp() {
        $parents = \Yii::$app->request->post('depdrop_parents');
        if ($parents) {
            $risk_type = $parents[0];
            $out = Prorisksub::find()
                    ->select('pro_sub_id as id,name as name')
                    ->where(['pro_id' => $risk_type])
                    ->asArray()
                    ->all();
            return Json::encode(['output' => $out, 'selected' => '']);
        }
    }
    
   public function actionGetseverity() {
        $parents = \Yii::$app->request->post('depdrop_parents');
        if ($parents) {
            $type_id = $parents[0];
            $out = Severity::find()
                    ->select('id_severity as id,name as name')
                    ->where(['type_id' => $type_id])
                    ->asArray()
                    ->all();
            return Json::encode(['output' => $out, 'selected' => '']);
        }
    }
        
 
    
}

 

2 ความคิดเห็น

  • final888

  • มานพ กองอุ่น

ทำนองนี้ครับ เน้นสร้าง relation ใน Model ให้เรียบร้อยก่อนครับ ใส่ในส่วน view นะคัรบ

if(!$model->isNewRecord){
$amp = ArrayHelper::map(Amp::find()->where(['id' => $model->pro_id])->all(), 'id', 'amp_name');
$type = ArrayHelper::map(Type::find()->where(['id' => $model->amp->type_id])->all(), 'id', 'type_name');

$model->dep_destination = $model->amp->type->dep_destination_id;
}

 

ขอบคุณมากครับ ทำได้แล้วครับ ^_^