drgc9632 2019-02-01 13:38
浏览 57

树层次结构渲染symfony 4

I'm learning on Symfony 4 and I would like to render my categories and my subs-categories as a tree with checkboxes (and subs - subs categories) in a form (for my products : ProduitType) like :

[ ]Category 1
     [ ]Sub Category 1
         [ ]Sub-Sub Category 1
     [ ]Sub Category 2
 [ ]Category 2 etc...

My twig form : new_produits.html.twig , My form : ProduitType , My class: Categorie

I tried many things in my twig but there isn't more helps on Symfony 4, if someone can give me some advices , please tell me .

 <form method="post">
                                    {{ form_start(form) }}

                                    <h5 class="text-center"> Informations </h5>
                                    {{ form_row(form.etat) }}
                                    {{ form_row(form.filename) }}
                                    {{ form_row(form.nom) }}
                                    {{ form_row(form.reference) }}
                                    {{ form_row(form.Gencod) }}
                                    {{ form_row(form.upc) }}
                                    {{ form_row(form.prix_base) }}
                                    {{ form_row(form.prix_final) }}

                                    {{ form_row(form.description) }}
                                    {{ form_row(form.short_description) }}
                                    {{ form_row(form.profondeur) }}
                                    {{ form_row(form.id_manufacturer) }}
                                    {{ form_row(form.weight) }}
                                    {{ form_row(form.unite) }}
                                    {{ form_row(form.prix_unite) }}
                                    <hr>
                                    <h5 class="text-center"> Caractéristiques </h5>

                                    {{ form_row(form.conditionnement) }}
                                    {{ form_row(form.unite_par_carton) }}
                                    {{ form_row(form.nb_carton_palette) }}
                                    {{ form_row(form.dlv_garantie) }}
                                    {{ form_row(form.dlv_theorique) }}
                                    {{ form_row(form.unite_par_couche) }}
                                    {{ form_row(form.produit_bio) }}
                                    {{ form_row(form.produit_nouveau) }}
                                    {{ form_row(form.produit_belle_france) }}

                                    <hr>
                                    <h5 class="text-center"> Associations </h5>
                                    
                                    <!-- This is my problem -->
                                    {{ form_row(form.id_categorie) }}




                                    <div class="form-group row text-right">
                                        <div class="col float-right col-sm-10 col-lg-9 offset-lg-0">
                                            <button type="submit" class="btn btn-space btn-primary">Enregistrer</button>

                                            {{ form_end(form) }}
->add('id_categorie',EntityType::class,[
                'required' =>false,
                'attr' => ['id' => 'data-value'],
                'class' => Categorie::class,
                'choice_label' => 'nom',
                'expanded' => true,
                'multiple' => true,
                'group_by' => 'id_parent',
                'label' => 'Catégorie',
                'query_builder' => function (CategorieRepository $c) {
                        $queryBuilder = $c->createQueryBuilder('c');
                        $query = $queryBuilder
                            ->where($queryBuilder->expr()->isNotNull('c.id_parent'));
                        return $query;
                }
namespace App\Entity;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;

/**
 * @ORM\Entity(repositoryClass="App\Repository\CategorieRepository")
 */
class Categorie
{
    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @ORM\Column(type="string", length=255, nullable=true)
     */
    private $nom;

    /**
     * @ORM\OneToMany(targetEntity="App\Entity\Produit", mappedBy="id_categorie")
     */
    private $id_produit;

    /**
     * @Gedmo\TreeParent
     * @ORM\ManyToOne(targetEntity="Categorie", inversedBy="children")
     * @ORM\JoinColumn(referencedColumnName="id", onDelete="CASCADE")
     */
    private $id_parent;


    /**
     * @Gedmo\TreeLeft
     * @ORM\Column(type="integer",nullable=true)
     */
    private $lft;

    /**
     * @Gedmo\TreeLevel
     * @ORM\Column(type="integer",nullable=true)
     */
    private $lvl;

    /**
     * @Gedmo\TreeRight
     * @ORM\Column(type="integer",nullable=true)
     */
    private $rgt;

    /**
     * @Gedmo\TreeRoot
     * @ORM\ManyToOne(targetEntity="Category")
     * @ORM\JoinColumn(referencedColumnName="id", onDelete="CASCADE")
     */
    private $root;

    /**
     * @ORM\OneToMany(targetEntity="Categorie", mappedBy="id_parent")
     * @ORM\OrderBy({"lft" = "ASC"})
     */
    private $children;

</div>
  • 写回答

1条回答 默认 最新

  • douduan5073 2019-03-15 15:16
    关注

    Did you find the way?

    Try this:

    $options = array(
        'decorate' => true,
        'rootOpen' => '<ul>',
        'rootClose' => '</ul>',
        'childOpen' => '<li>',
        'childClose' => '</li>',
        'nodeDecorator' => function($node) {
                                return '<input type="checkbox" id="check"'. $node['id'] .'> <a class="tag-li" href="#" onclick="clicked(this)" data-id="' . $node['somefield'] . '">'.$node['someotherfield'].'</a>';
                            });
    
    get_your_entity_manager()->getRepository(Categorie::class)->childrenHierarchy(
                null, /* starting from root nodes */
                false, /* false: load all children, true: only direct */
                $options);
    

    To make this work you will need a repository that implements "childrenHierarchy" method or use the Gedmo one changing repository annotation in your entity to

    @ORM\Entity(repositoryClass="Gedmo\Tree\Entity\Repository\NestedTreeRepository")

    Hope this helps

    评论

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP