我有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代码是:
从医生那里选择doctor_name
所以我需要它:
从医生那里选择DISTINCT doctor_name(来自patient_services的SELECT doctor_id,患者_id =“来自第一个下降名单的价值”)
在常规下拉列表中,它以这种方式工作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'=>'']);
}
答案 0 :(得分:0)
第一个字段(Select2):
select count(*)
from (select distinct t1.var1 from table_a t1
join table_b t2 on t1.var2 = t2.var1
and t2.date between ('2016-11-05 00:00:00') and ('2016-11-10 23:59:59')
and t2.var3 = 3 ) as temp
第二个字段(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'],
'url' => Url::to(['/receipts/child']),
'loadingText' => 'Loading child level 1 ...',
]
]);
?>
显示必须更改哪个元素(获取元素的ID)(单击,选择等),以便发送Ajax请求并从控制器检索结果。在这种情况下,应该存在ID为'depends' => ['receipts-doctor_id'],
的元素。如果您不知道或者您想为父元素设置ID,则可以使用receipts-doctor_id
作为父元素。
对于控制器,您可以检索这样的值:
'options' => ['id' => 'receipts-doctor_id'],