dpw43061 2017-05-17 05:22
浏览 40
已采纳

Symfony复选框列出了枝条渲染

I'm quite new to Symfony and I have a specific question. I have a form for a job application where the applicant can choose competencies linked to categories.

Entities:

Application linked to Competence as many to many

Compentence linked to Category as Many to One

So in ApplicationType I have:

->add('competences', EntityType::class, array(
    'class'        => 'SanOffresBundle:Competence',
    'label' => 'Compétences (sélectionnez-en autant que vous voulez)',
    'choice_label' => 'nom',
    'multiple'     => true,
    'expanded' => true,
    'query_builder' => function (CompetenceRepository $er) {
        return $er->createQueryBuilder('cc')
        ->orderBy('cc.nom', 'ASC');},
    'group_by' => function($val, $key, $index) {
        return $val->getCategorie()->getNom();},        
    ))

With this code and variations I get:

  • 'expanded' => false : a menu with competences sorted by category, but the categories are not in alphabetical order. This would be an acceptable alternative if the categories were sorted, but with more than 100 competences to choose from, it's not user friendly.

  • 'expanded' => true : a list of competences checkboxes displayed grouped by categories, but the categories are not shown.

Would anybody have an idea?

  • 写回答

1条回答 默认 最新

  • dtnd30892 2017-05-17 06:38
    关注

    If you'd like to change order in secect with all Competence than you probably should extend your query builder with join

    'query_builder' => function (CompetenceRepository $er) {
            return $er->createQueryBuilder('cc')
                   ->join('cc.category', 'cat') //something like that
                   ->orderBy('cat.name', 'ASC')->addOrderBy('cc.nom', 'ASC');
    }
    

    another way would be to try @ORM\OrderBy({...}) Annotation in your relations between Compentence and Category. See link

    'expanded' => true : a list of competences checkboxes displayed grouped by categories, but the categories are not shown.

    Which is correct since it's inherited option from ChoiceType. Group by works only if you render it as a <SELECT> dropdown where you can make use of <optgroup> for grouping. I think there's no way to get a bunch (list) of checkboxes with any grouping from symfony's FormTypes out of the box..at least without any extra offer.

    Of cource you can create your own FormType and extend Twig-Widgets an so on. ...Or you could try to get desired look with Collection of (embedet) Forms ...but that are just thoughts since I do not know how your entities and their relations look like.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

悬赏问题

  • ¥15 el-select光标位置问题
  • ¥15 单片机 TC277 PWM
  • ¥15 在更新角色衣服索引后,Sprite 并未正确显示更新的效果该如何去解决orz(标签-c#)
  • ¥15 VAE代码如何画混淆矩阵
  • ¥15 求遗传算法GAMS代码
  • ¥15 雄安新区高光谱数据集的下载网址打不开
  • ¥66 android运行时native和graphics内存详细信息获取
  • ¥15 rk3566 Android11 USB摄像头 微信
  • ¥15 torch框架下的强化学习DQN训练奖励值浮动过低,希望指导如何调整
  • ¥35 西门子博图v16安装密钥提示CryptAcquireContext MS_DEF_PROV Error of containger opening
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部