我有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'=>'']); }
您可以使用[卡爾蒂克DepDrop(http://demos.krajee.com/widget-details/depdrop) –
要添加到什麼@InsaneSkull建議您可以使用DepDrop插件,並仍然使用Select2,因爲DepDrop插件支持Select2下拉菜單。你必須使用'type'選項。 – sm1979
@InsaneSkull謝謝..我沒有看到過這個,,,我看見了,我有一些問題'「依賴」 => [「帳戶LEV0」],'我沒有得到它,我有父ID是'patient_id'有是別的東西,我在控制器代碼糊塗了'$名單=帳戶::發現() - > andWhere([ '父'=> $ ID]) - > asArray() - >所有();' –