树层次结构渲染symfony 4

时间:2019-02-01 13:38:29

标签: php symfony twig symfony-forms

我正在学习Symfony 4,我想将我的类别和子类别呈现为带有复选框(以及子类别-子类别)的树状结构(对于我的产品:ProduitType):

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

我的树枝形式:new_produits.html.twig,我的形式:ProduitType,我的班级:分类

我在细枝上做了很多尝试,但是Symfony 4并没有更多帮助,如果有人可以给我一些建议,请告诉我。

 <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;

1 个答案:

答案 0 :(得分:0)

您找到路了吗?

尝试一下:

$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);

要完成这项工作,您将需要一个实现“ childrenHierarchy”方法的存储库,或在您的实体中使用Gedmo更改存储库的注释来

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

希望这会有所帮助