2016-12-30 60 views
1

我有2 SELECT2下拉列表第二取决于数据从第一个Yii2:卡尔蒂克选择二

第一个代码:

<?= $form->field($model, 'patient_id')->widget(select2::className(),[ 
    'data'=> arrayhelper::map(patient::find()->all(),'patient_id','patient_name'), 
    'options'=>['placeholder'=>'select patient Name ... '], 
    'pluginOptions'=>[ 
      'allowClear'=>true 
    ], 
])?> 

第二个代码:

<?= $form->field($model, 'doctor_id')->widget(select2::className(),[ 
    'data'=> arrayhelper::map(doctors::find()->all(),'doctor_id','doctor_name'), 
    'options'=>['placeholder'=>'أختر اسم الطبيب '], 
    'pluginOptions'=>[ 
      'allowClear'=>true 
    ], 
])?> 

我知道第二个sql代码是:

从医生

选择doctor_name所以我需要它是:

选择从医生DISTINCT doctor_name其中doctor_id在(从patient_services SELECT doctor_id WHERE patient_id = “FROM THE第一DROPDOWNLIST值”)

在正常的下拉列表中它像这样工作Yii2 Lesson - 20 Dependent Drop Down Lists By DoingITeasyChannel但在select2中我没有找到如何去做。

-------------------------------更新后----
的意见有DepDrop但我很困惑如何使用它。

我已经改变

<?= $form->field($model, 'patient_id')->widget(Select2::classname(), [ 'data' => ArrayHelper::map(patient::find()->asArray()->all(), 'patient_id', 'patient_name')]); ?>

并且另一个是:

<?= $form->field($model, 'doctor_id')->widget(DepDrop::classname(), [ 'options' => ['placeholder' => 'Select ...'], 'type' => DepDrop::TYPE_SELECT2, 'select2Options'=>['pluginOptions'=>['allowClear'=>true]], 'pluginOptions'=>[ 'depends'=>['receipts-doctor_id'], // here i got confused 'url' => Url::to(['/receipts/child']), 'loadingText' => 'Loading child level 1 ...', ] ]); ?>

在控制器

public function actionChild() { $out = []; if (isset($_POST['depdrop_parents'])) { // what they meaning by depdrop_parents or what i should change it ?
$id = end($_POST['depdrop_parents']); $list = Account::find()->andWhere(['parent'=>$id])->asArray()->all(); $selected = null; if ($id != null && count($list) > 0) { $selected = ''; foreach ($list as $i => $account) { $out[] = ['id' => $account['id'], 'name' => $account['name']]; if ($i == 0) { $selected = $account['id']; } } // Shows how you can preselect a value echo Json::encode(['output' => $out, 'selected'=>$selected]); return; } } echo Json::encode(['output' => '', 'selected'=>'']); }

+3

您可以使用[卡尔蒂克DepDrop(http://demos.krajee.com/widget-details/depdrop) –

+0

要添加到什么@InsaneSkull建议您可以使用DepDrop插件,并仍然使用Select2,因为DepDrop插件支持Select2下拉菜单。你必须使用'type'选项。 – sm1979

+0

@InsaneSkull谢谢..我没有看到过这个,,,我看见了,我有一些问题'“依赖” => [“帐户LEV0”],'我没有得到它,我有父ID是'patient_id'有是别的东西,我在控制器代码糊涂了'$名单=帐户::发现() - > andWhere([ '父'=> $ ID]) - > asArray() - >所有();' –

回答

3

第一字段(S elect2):

<?= $form->field($model, 'patient_id')->widget(Select2::classname(), [ 
    'data' => ArrayHelper::map(patient::find()->asArray()->all(), 'patient_id', 'patient_name')]); 
?> 

第二场(DepDrop):

<?= $form->field($model, 'doctor_id')->widget(DepDrop::classname(), [ 
    'options' => ['placeholder' => 'Select ...'], 
    'type' => DepDrop::TYPE_SELECT2, 
    'select2Options'=> ['pluginOptions' => ['allowClear' => true]], 
    'pluginOptions'=> [ 
     'depends' => ['receipts-doctor_id'], 
     'url' => Url::to(['/receipts/child']), 
     'loadingText' => 'Loading child level 1 ...', 
    ] 
]); 
?> 

插件选项'depends' => ['receipts-doctor_id'],显示哪些元素(需要元素的ID)必须改变(上点击,就选择等),以发送Ajax请求并从控制器中检索结果。在这种情况下,应存在ID为receipts-doctor_id的元素。如果您不知道或者您想要为父元素设置ID,则可以使用'options' => ['id' => 'receipts-doctor_id'],作为父元素。

而对于控制器,可以检索值这样的:

public function actionChild() 
{ 
    $depdropParents = Yii::$app->request->post('depdrop_parents'); 

    if ($depdropParents !== null) { 
     $value = $depdropParents[0]; 
     // Now your $value contains what was being selected in parent element 
    } 
} 
+0

有什么不清楚的地方。在我选择第一个下拉列表后没有任何事情发生,第二个DepDrop仍然禁用! –

+0

如果是这样,那么这意味着控制器(后端代码)出现错误。使用网络(如果您知道如何)查看错误消息。 –

相关问题