Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Yii2, Kartik DepDrop on update pre selected value

Tags:

php

yii2

I have four dependent drop downs of hierarchy library > catalog > category > subject

I am using kartik depdrop, every thing is working really fine but on update I couldn't find any solution for pre selected values. What I need is to show pre selected values on update with all other values as well so that user can change the selection also.

Here is my code.

_form.php

<div class="inner-addon right-addon">
        <div class="catalog-input">
            <span><strong>Library</strong></span>
            <?php echo $form->field($modelDep, 'lesson_name[0]')->dropDownList($libList, [
                'prompt' => 'Select Library',
                'id'=>'lib-id-1',
                'name'=>'Lesson[library]'
            ])->label(false); ?>
        </div> 
        <div class="category-input">
            <span><strong>Catalog</strong></span>
            <?php
            echo $form->field($modelDep, 'lesson_name[1]')->widget(DepDrop::classname(), [  
                'options'=>['id'=>'catalog-id-1', 'name'=>'Lesson[catalog]'],
                'pluginOptions'=>[
                    'depends'=>['lib-id-1'],
                    'placeholder'=>'Select Catalog',
                    'url'=>Url::to(['/lesson/catalog'])
                ]
            ])->label(false);
            ?>
        </div> 
        <div class="subject-input">
            <span><strong>Category</strong></span>
            <?php
                echo $form->field($modelDep, 'lesson_name[2]')->widget(DepDrop::classname(), [
                    'options'=>['id'=>'cat-id-1', 'name'=>'Lesson[category]'],
                    'pluginOptions'=>[
                        'depends'=>['catalog-id-1'],
                        'placeholder'=>'Select Category',
                        'url'=>Url::to(['/lesson/category'])
                    ]
                ])->label(false);
            ?>
        </div> 
        <div class="topic-input">
            <span><strong>Subject</strong></span>
            <?php
                echo $form->field($modelDep, 'lesson_name[3]')->widget(DepDrop::classname(), [
                    'options'=>['id'=>'subject-id-1', 'name'=>'Lesson[fk_subject]'],
                    'pluginOptions'=>[
                        'depends'=>['cat-id-1'],
                        'placeholder'=>'Select Subject',
                        'url'=>Url::to(['/lesson/subject'])
                    ]
                ])->label(false);
            ?>
        </div> 
    </div>

Controller

public function actionCatalog() {
    $out = [];
    if (isset($_POST['depdrop_parents'])) {
        $parents = $_POST['depdrop_parents'];
        if ($parents != null) {
            $lib_id = $parents[0];
            $out = $this::getCatalogList($lib_id);
            echo Json::encode(['output'=>$out, 'selected'=>'']);
            return;
        }
    }
    echo Json::encode(['output'=>'', 'selected'=>'']);
}
public static function getCatalogList($lib_id){
    $company_name = array_shift((explode(".", $_SERVER['HTTP_HOST'])));
    $company = \common\models\Company::find()->where(['company_name' => $company_name])->one();
    $data= \common\models\Catalog::find()
       ->where(['fk_library'=>$lib_id,'fk_company'=>$company->id])
       ->select(['id','catalog_name AS name' ])->asArray()->all();
    return $data;
}



public function actionCategory() {
    $out = [];
    if (isset($_POST['depdrop_parents'])) {
            $parents = $_POST['depdrop_parents'];
            if ($parents != null) {
                $cat_id = $parents[0];
                $out = $this::getCategoryList($cat_id);
                echo Json::encode(['output'=>$out, 'selected'=>'']);
                return;
            }
        }
    echo Json::encode(['output'=>'', 'selected'=>'']);
}
public static function getCategoryList($cat_id){
    $company_name = array_shift((explode(".", $_SERVER['HTTP_HOST'])));
    $company = \common\models\Company::find()->where(['company_name' => $company_name])->one();
    $data= \common\models\Category::find()
       ->where(['fk_catalog'=>$cat_id,'fk_company'=>$company->id])
       ->select(['id','category_name AS name' ])->asArray()->all();
    return $data;
}





public function actionSubject() {
    $out = [];
    if (isset($_POST['depdrop_parents'])) {
            $parents = $_POST['depdrop_parents'];
            if ($parents != null) {
                $category_id = $parents[0];
                $out = $this::getSubjectList($category_id);
                echo Json::encode(['output'=>$out, 'selected'=>'']);
                return;
            }
        }
    echo Json::encode(['output'=>'', 'selected'=>'']);
}
public static function getSubjectList($category_id){
    $company_name = array_shift((explode(".", $_SERVER['HTTP_HOST'])));
    $company = \common\models\Company::find()->where(['company_name' => $company_name])->one();
    $data= \common\models\Subject::find()
       ->where(['fk_category'=>$category_id,'fk_company'=>$company->id])
       ->select(['id','subject_name AS name' ])->asArray()->all();
    return $data;
}
like image 430
Jim Hamilton Avatar asked Oct 30 '22 16:10

Jim Hamilton


2 Answers

The 2 previous answers have a bit of the answer but not too clear how to implement.

In the kartik documentation page you have documentation about the data option with the following sample:

'data' => [2 => 'Music'], 

You can set the default in your controller but you don't have the updated id there only the parent id. In the view you will have for sure the id in your model but not always the field with the description.

The good thing is that you don't need the name to show the default just the id for example:

    $panelBookRights .= $form->field($model, 'primary_agent_id')->widget(DepDrop::classname(), [
    'data' => [$model->primary_agent_id => 'default'], 
    'options' => ['id' => 'primary-agent-id'],
    'pluginOptions' => [
        'depends' => ['book-rights_owner_id'],
        'initialize' => true,
        'placeholder' => 'Select...',
        'url' => Url::to(['contact/list_primaryagents'])
    ]
]);

I put default there but it works if the id is in the list you load from the controller.

like image 175
Zifre Avatar answered Nov 15 '22 07:11

Zifre


Have you tried setting the 'selected' array attribute? One of your action after some cleanup:

public function actionSubject() {
    $out = [];
    if (isset($_POST['depdrop_parents'])) {
        $parents = $_POST['depdrop_parents'];
        if ($parents != null) {
            $category_id = $parents[0];
            $out = $this::getSubjectList($category_id);
            $pre_selected_subject_id = 5; // or whatever you want to be default
        }
    }
    echo Json::encode(['output'=>$out, 'selected'=>$pre_selected_subject_id ]);
}
like image 23
BarneyK Avatar answered Nov 15 '22 05:11

BarneyK