Symfony2,Doctrine扩展树:生成类似“树"的下拉列表,选择列表 [英] Symfony2,Doctrine Extensions Tree : Generating a "tree"-like dropdown Select list

查看:95
本文介绍了Symfony2,Doctrine扩展树:生成类似“树"的下拉列表,选择列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用Doctrine Tree Extension的使用Tree体系结构构建的类别表 看起来像这样

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

在视觉上是这样的:

Movies
|
|
|->Packs
|->Dubbed

现在我想生成用于添加评论的表单,并为每部电影的下拉列表加载类别,因此我的电影评论中具有form-type-class

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:

如何配置菜单设置,这样它会生成像这样的 Tree-Like-下拉选择?

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

推荐答案

我不确定这是个好主意:用户将无法输入自己的选择.

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 :

首先,您可以按root和lft值对这三个值进行排序以正确显示它,因此添加一个查询构建器:

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');
},

然后,您需要向您的实体添加getIndentedTitle方法:

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

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

最后,在构建表单时,在选项中添加一个属性选项,以显示虚拟属性indentedTitle而不是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'

请参阅: http://symfony.com/doc/current /reference/forms/types/entity.html

这篇关于Symfony2,Doctrine扩展树:生成类似“树"的下拉列表,选择列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆