0

Dependent DropdownList is working particularly ok. But I cannot see list of options when i click on Dropdown

I have read all forums but i cannot find any problem like mine

index.php

$a = [
    'Qoraqalpog‘iston Respublikasi' => 'Qoraqalpog‘iston Respublikasi',
    'Andijon viloyati' =>   'Andijon viloyati',
    'Buxoro viloyati' =>  'Buxoro viloyati',
    'Jizzax viloyati' =>   'Jizzax viloyati',
    'Qashqadaryo viloyati' => 'Qashqadaryo viloyati',
    'Navoiy viloyati' =>   'Navoiy viloyati',
    'Namangan viloyati' =>   'Namangan viloyati',
    'Samarqand viloyati' =>   'Samarqand viloyati',
    'Sirdaryo viloyati' =>   'Sirdaryo viloyati',
    'Surxondaryo viloyati' =>   'Surxondaryo viloyati',
    'Toshkent viloyati' =>   'Toshkent viloyati',
    'Farg‘ona viloyati' =>   'Farg‘ona viloyati',
    'Xorazm viloyati' => 'Xorazm viloyati',
    'Toshkent shahri' => 'Toshkent shahri',
];
<?php $f = ActiveForm::begin([
                        'fieldConfig' => ['options' => ['class' => 'input-field']],
                        'enableAjaxValidation' => false,
                        'enableClientValidation'=>true,

                        'options' => [
                            'enctype' => 'multipart/form-data',
                            // 'onSubmit'=> 'return false'
                        ]
                    ])?>
                    <?= $f->field($contact,'name_of')->label(Yii::t('template','Ta\'lim muassasasi nomi yoki raqami*'))->error(false)?>
                    <?= $f->field($contact, 'city' ,['options' => ['class' => 'input-field']])->error(false)->label(Yii::t('template',"Ta'lim muassasasi joylashgan hudud*"))
                        ->dropDownList(
                            $a,
                            [
                                'prompt'=>Yii::t('template','Hududni tanlang'),
                                'onchange'=> '
                                $.post( "'.Yii::$app->urlManager->createUrl('site/regions?id=').'"+$(this).val(), function( data ) {
                                  $( "select#contactform-region" ).html( data );
                                });
                                               '
                            ]

                        );?>

                    <?= $f->field($contact,'region')->error(false)->dropDownList(
                            $empty,
                        [
                        ]);?>

actionRegions()

public function actionRegions($id){
  $posts = Regions::find()
            ->where(['idCity' => $id])
            ->all();
        $count = Regions::find()
            ->where(['idCity' => $id])
            ->count();
        $lang = Yii::$app->language;
        if ($count>0){
            foreach($posts as $post) {
                echo "<option value='".$post->id."'>".$post->{"name_".$lang}."</option>";
            }
        } else {
            echo "<option>-</option>";
        }

    }

here is is empty https://i.stack.imgur.com/vw6mY.jpg on click dropdown nothing is happened https://i.stack.imgur.com/IXTsA.jpg

KBell
  • 502
  • 8
  • 23
Terrabayt
  • 13
  • 5
  • 1
    why dont you use [select2 DepDrop by kartik](http://demos.krajee.com/widget-details/depdrop) and make your life easier – Muhammad Omer Aslam Jan 23 '19 at 19:00
  • Maybe it is something I do not understand, but I do not see that `index.php` calls `actionRegions`. Have you validated the HTML? You can use [NuHtml](https://validator.w3.org/nu/) for that, or [W3C Validator Service](https://validator.w3.org/). – DinoCoderSaurus Jan 24 '19 at 02:57
  • @DinoCoderSaurus its `Yii framework` using the `MVC` pattern you dont need to call the action. – Muhammad Omer Aslam Jan 24 '19 at 08:18
  • in the googleChrome dev tools , in Network tab , you can see how to do a post conection ? status is 200 ? what is the response ? i – eborrallo Jan 25 '19 at 12:34
  • You have to return a json endoded array or option => value. – Kalu Jan 25 '19 at 13:40

2 Answers2

0

Try returning a json string of the html markup.

    $list ='';
    if ($count>0){
        foreach($posts as $post) {
            $list .= "<option value='".$post->id."'>".$post->{"name_".$lang}."</option>";
        }
    } else {
        $list = "<option>-</option>";
    }
    return json_encode($list);
Kalu
  • 561
  • 2
  • 7
0

It seems to me that you are using select wrapper. I think you should update your select wrapper plugin content after ajax. if you are using materialize css here is a sollution How to dynamically modify <select> in materialize css framework

Jasco
  • 61
  • 4