You need to make the dropDownList have a tree type list

Category1

--Category1.1

--Category1.2

Category 2 ....

There is a code that forms this list, but in it I can only select a subcategory, but the main one cannot.

enter image description here

So the question is how to make it so that you can choose the first category.

Listing category search method

public static function getHierarchy() { $options = []; $parents = self::find()->where("parent_id=0")->all(); foreach ($parents as $id => $p) { $children = self::find()->where('parent_id=:parent_id', ['parent_id' => $p->id])->asArray()->all(); $child_options = []; foreach ($children as $child) { $child_options[$child->id] = $child->name; } $options[$p->name] = ArrayHelper::map($children, 'id', 'name'); } return $options; } 
  • The select is customized with the help of some js framework similar to bootstrap or jquery, so dig in the direction of where it is initialized, then the documentation on it, options and profit. - Naumov
  • “Categories” are created with the help of <optgroup> if I’m not mistaken, that’s why it’s impossible to choose it ... You <optgroup> rather need to use styles to implement this. - Roman Grinyov

1 answer 1

On optgroup it is impossible to make select because of the html features The easiest solution

 .optionGroup { font-weight: bold; font-style: italic; } .optionChild { padding-left: 15px; } <select multiple="multiple"> <option value="0" class="optionGroup">Parent Tag</option> <option value="1" class="optionChild">Child Tag</option> <option value="2" class="optionChild">Child Tag</option> </select> 

https://stackoverflow.com/questions/9892247/selectable-optgroup-in-html-select-tag

  • cheops, thank you! can you tell me something I can't find, how to insert a snippet ... - RSalnikov