2016-11-21 2 views
1

yii2フィールドクラスのドロップダウンリストのサブメニューに問題がありますか? ドロップダウンリストのサブメニューまたは副選択リストの作成方法。私はSHORT_NAMEを使用し、エコーデータyii2ドロップダウンリストサブメニューの作成方法は?

+----+------------------------------------+------------+--------------+-------------+ 
| id | group_name       | short_name | parent_group | admin_group | 
+----+------------------------------------+------------+--------------+-------------+ 
| 1 | Jizzax Davlat Pedagogika instituti | JDPI  |   NULL |  NULL | 
| 2 | O'quv ishlari bo'yicha Prorektor | Prorektor |   1 |  NULL | 
| 3 | Axborot tehnologiyalari markazi | ATM  |   2 |  NULL | 
+----+------------------------------------+------------+--------------+-------------+ 

enter image description here my form

私のデシベルスキーム

+--------------+--------------+------+-----+---------+----------------+ 
| Field  | Type   | Null | Key | Default | Extra   | 
+--------------+--------------+------+-----+---------+----------------+ 
| id   | int(11)  | NO | PRI | NULL | auto_increment | 
| group_name | varchar(255) | NO |  | NULL |    | 
| short_name | varchar(32) | NO |  | NULL |    | 
| parent_group | int(11)  | YES |  | NULL |    | 
| admin_group | int(11)  | YES |  | NULL |    | 
+--------------+--------------+------+-----+---------+----------------+ 

私が見る選択:

-JDPI 
--Prorektor 
---ATM 
+0

?ナビゲーション用ですか? –

+0

get idを使用して項目を選択 – DasturchiUZ

+0

私はあなたが依存するドロップダウン構造を使用する必要があると思う –

答えて

1

はモデルクラス

に、このメソッドを追加します。
public static function getItems($indent = '', $parent_group = null) 
{ 
    $items = []; 
    // for all childs of $parent_group (roots if $parent_group == null) 
    $groups = self::find()->where(['parent_group'=>$parent_group]) 
     ->orderBy('short_name')->all(); 
    foreach($groups as $group) 
    { 
     // add group to items list 
     $items[$group->id] = $indent.$group->short_name; 
     // recursively add children to the list with indent 
     $items = array_merge($items, self::getItems($indent.' ', $group->id)); 
    } 
    return $items; 
} 

メソッドの出力は、ビュー

YourModelClassは、あなたのモデルクラスの名前です
echo $form->field($model, 'group_id')->dropDownList(YourModelClass::getItems()); 

[ 
    1 => 'JDPI', 
    2 => ' Prorektor', 
    3 => ' ATM' 
] 

でなければなりません。私は店の木information.Thereにネストされたセットを使用するようにお勧め

は、ネストされたセットのためのいくつかの拡張機能です。あなたはそれを使用したいどのような目的のために

+0

ありがとうコードが働いています – DasturchiUZ

関連する問題