Does Yii have anything to help build a parent/child dropdown list?

I need to build a dropdown list from data with a parent/child relationship, with unlimited levels, such as:


id | parent_id | name

1    0           foo

2    0           bar

3    1           cat

4    1           dog

5    2           fish

6    3           cow

7    5           goat

8    6           donkey

…and so on.

So, before I go building something myself for this, since I’m sure working with data like this is common, I wanted to check if Yii any helper functions that could output a nested dropdown list with this data for you and you can just pass it an array or something with the required data?

Thanks!

This might help: http://demos.krajee.com/widget-details/depdrop

Thanks Patrick, I’ll take a look. :)

Ok, I had a look, but that appears to be more of a frontend select plugin? I already am using select2 and I can display the data correctly, but I was just checking to see if Yii had a helper function to take the data and sort them into their correct parent/child relationships so I could then feed that data to the select list.




 we can make folle me

 in Form or gridview use want to use it

<?php


namespace backend\models;


use Yii;

use yii\base\Model;

use yii\data\ActiveDataProvider;

use backend\models\Categories;


/**

 * CategoriesSeach represents the model behind the search form about `backend\models\Categories`

 */

class CategoriesSearch extends Categories

{

 public function dequymenu(){

        $query = Categories::find()->select('id,parent_id,name, display_order')->where(['status'=>0]);

        $dataProvider = new ActiveDataProvider([

            'query' => $query,

            'sort' => [

                'defaultOrder' => [

                    'display_order' => SORT_ASC,

                ]

            ],

            'pagination' => false

        ]);

        return $dataProvider->getModels();

    }

}

 // END FUNCTION in categoriessearch 


// THIS IS CODE IN _FORM.php

 <?php 

  use backend\models\CategoriesSearch;

  function showMenuTree($categories, $parent_id = 0, $char = '', &$array){

      foreach ($categories as $key => $item){

          if ($item['parent_id'] == $parent_id){

                  $array[$item['id']] = $char. $item['name'];

              unset($categories[$key]);

              static::showMenuTree($categories, $item['id'], $char.'---', $array);

          }

      }

      return $array;

  }

   $arrcate=CategoriesSearch::dequymenu();

   $arrMenu=array();

   showMenuTree($arrcate,0,'', $arrMenu);

?>

<?php $form = ActiveForm::begin(['options' => ['enctype' => 'multipart/form-data']]) ?>

<?= $form->field($model, 'id')->dropdownList($arrMenu, ['class'=>'select2 css-select','data-select-width'=>250,'prompt' => 'SELECE MENU']) ?>

<?php ActiveForm::end(); ?>




if You do not understand, you can email: hongduyphp@gmail.com or skype: duyminh123.

Hey thanks Hong, but I ended up working on my own since it didn’t appear Yii had anything as yet.

Appreciate the post anyway :)