【发布时间】:2016-12-30 12:20:49
【问题描述】:
我有 2 个 select2 下拉列表,第二个取决于第一个的数据
第一个代码:
<?= $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代码是:
从医生中选择医生姓名
所以我需要它:
从医生 ID 所在的医生中选择 DISTINCT 医生名称(从病人服务中选择医生 ID WHERE patient_id="THE VALUE FROM THE 1st DROPDOWNLIST")
在常规下拉列表中,它的工作方式与 Yii2 Lesson - 20 Dependent Drop Down Lists By DoingITeasyChannel 类似,但在 select2 中我没有找到方法。
-------------------更新后----
作为 cmets 有 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'=>'']);
}
【问题讨论】:
-
你可以使用Kartik DepDrop
-
要添加到 @InsaneSkull 建议的内容,您可以使用 DepDrop 插件并仍然使用 Select2,因为 DepDrop 插件支持 Select2 下拉菜单。您必须使用
type选项。 -
@InsaneSkull 谢谢 .. 我以前没见过这个 ,,, 我看到了,我有一些问题
'depends'=>['account-lev0'],我没明白我的父 ID 是patient_id还有一些我在控制器代码$list =Account::find()->andWhere(['parent'=>$id])->asArray()->all();中感到困惑 -
@EdvinTenovimas 我对如何使用它感到困惑。我会更新我的问题
-
好的,我看到你已经写了 Select2 和 Depdrop。它看起来不错(至少一开始是这样)。你被困在哪里了?
标签: php yii2 yii2-advanced-app select2