[英]Yii2 : kartik Select2
I have 2 select2 dropdownlist the second depends on data from the first one 我有2个select2下拉列表,第二个依赖于来自第一个的数据
the first one code : 第一个代码:
<?= $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
],
])?>
the second one code : 第二个代码:
<?= $form->field($model, 'doctor_id')->widget(select2::className(),[
'data'=> arrayhelper::map(doctors::find()->all(),'doctor_id','doctor_name'),
'options'=>['placeholder'=>'أختر اسم الطبيب '],
'pluginOptions'=>[
'allowClear'=>true
],
])?>
i know the sql code in the second one is : 我知道第二个中的sql代码是:
select doctor_name from doctors
从医生那里选择doctor_name
so i need it to be : 所以我需要它:
SELECT DISTINCT doctor_name from doctors where doctor_id in (SELECT doctor_id from patient_services WHERE patient_id="THE VALUE FROM THE 1st DROPDOWNLIST" )
从医生那里选择DISTINCT doctor_name(在doctor_services中选择doctor_id,患者_id =“第一次下降的价值”)
in the regular dropdownlist it work by like this way Yii2 Lesson - 20 Dependent Drop Down Lists By DoingITeasyChannel but in select2 i didn't find how to do it . 在常规下拉列表中,它通过这样的方式工作Yii2课程 - 20依赖下拉列表通过DoingITeasyChannel但在select2中我没有找到如何做到这一点。
------------------------------- after update ---- -------------------------------更新后----
as the comments there is DepDrop but i got confused how to use it. 作为评论有DepDrop,但我很困惑如何使用它。
i've changed 我改变了
<?= $form->field($model, 'patient_id')->widget(Select2::classname(), [ 'data' => ArrayHelper::map(patient::find()->asArray()->all(), 'patient_id', 'patient_name')]); ?>
and the other one is : 另一个是:
<?= $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 ...', ] ]); ?>
in controller 在控制器中
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'=>'']); }
First field (Select2): 第一个字段(Select2):
<?= $form->field($model, 'patient_id')->widget(Select2::classname(), [
'data' => ArrayHelper::map(patient::find()->asArray()->all(), 'patient_id', 'patient_name')]);
?>
Second field (DepDrop): 第二个字段(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 ...',
]
]);
?>
Plugin option 'depends' => ['receipts-doctor_id'],
shows which element (takes element's ID) must be changed (on click, on select, etc.) in order to send Ajax request and retrieve results from controller. 插件选项
'depends' => ['receipts-doctor_id'],
显示必须更改哪个元素(获取元素的ID)(点击,选择等),以便发送Ajax请求并从控制器检索结果。 In this case, an element with ID receipts-doctor_id
should be present. 在这种情况下,应该存在具有ID
receipts-doctor_id
的元素。 If you don't know or you want to set ID for parent element, you can use 'options' => ['id' => 'receipts-doctor_id'],
for your parent element. 如果您不知道或者您想为父元素设置ID,则可以对父元素使用
'options' => ['id' => 'receipts-doctor_id'],
。
And for controller, you can retrieve value like this: 对于控制器,您可以检索如下值:
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
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.