Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony2,Doctrine Extensions Tree : Generating a "tree"-like dropdown Select list

I have a Categories table, built with Tree architecture, using Doctrine Tree Extension and it looks something like this

id  parent_id   title   lft lvl rgt root
864 (NULL)  Movies  1   0   18  864
865 864 Packs   2   1   3   864
866 864 Dubbed  4   1   5   864

and visually like this:

Movies
|
|
|->Packs
|->Dubbed

now i want to generated form for adding reviews , and loading categories as dropdown list for each movie, so i have in my movie-review form-type-class

public function buildForm(FormBuilder $builder, array $options)
{

    $builder->add('name');
    $builder->add('file');
    $builder->add('cover');
    $builder->add('category','entity',           array('class'=>'Tracker\MembersBundle\Entity\Category', 'property'=>'title', ));           
}

which generates a normal dropdown menu like this:

enter image description here

how can i configure my menu settings, so it generates a Tree-Like-dropdown select like this?

enter image description here

like image 513
Confidence Avatar asked Feb 16 '12 10:02

Confidence


1 Answers

I'm not sure this is a good idea : users won't be able to type in their choice.

Haven't tested this solution, but it should work :

First, you can sort the three by root and lft value to display it properly, so add a query builder:

'query_builder' => function($er) {
    return $er->createQueryBuilder('c')
        ->orderBy('c.root', 'ASC')
        ->addOrderBy('c.lft', 'ASC');
},

Then, you need to add a getIndentedTitle method to your entity:

public function getIndentedTitle() {
    return str_repeat("--", $this->lvl).$this->title;
}

Finally, add a property option to your options when you build the form, to display the virtual property indentedTitle instead of title :

'property' => 'indentedTitle'

See : http://symfony.com/doc/current/reference/forms/types/entity.html

like image 84
neurofr Avatar answered Nov 14 '22 23:11

neurofr