1
votes

I have 2 select2 dropdownlist the second depends on data from the first one

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 :

select doctor_name from doctors

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" )

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 .

------------------------------- after update ----
as the comments there is DepDrop but i got confused how to use it.

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'=>'']); }

1
To add to what @InsaneSkull suggested you can use the DepDrop plugin and still use Select2 because the DepDrop plugin has support for Select2 dropdowns. You have to use the type option. - sm1979
@InsaneSkull Thank you .. i didn't see this before ,,, i saw it and i have some issues 'depends'=>['account-lev0'], i didn't get it i have the parent id is patient_id and there is something else i got confused in controller code $list =Account::find()->andWhere(['parent'=>$id])->asArray()->all(); - Naeem Ali
@EdvinTenovimas i got confused how to use it . I will update my question - Naeem Ali
Ok, so I see you have written Select2 and Depdrop. It looks fine (at first at least). Where you're stuck? - Gytis TG

1 Answers

0
votes

First field (Select2):

<?= $form->field($model, 'patient_id')->widget(Select2::classname(), [
    'data' => ArrayHelper::map(patient::find()->asArray()->all(), 'patient_id', 'patient_name')]);
?>

Second field (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. In this case, an element with ID receipts-doctor_id should be present. 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.

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
    }
}