2016-12-30 65 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

如果是這樣,那麼這意味着控制器(後端代碼)出現錯誤。使用網絡(如果您知道如何)查看錯誤消息。 –

相關問題