树层次结构渲染symfony 4

问题描述 投票:1回答:1

我正在学习Symfony 4,我想将我的类别和我的子类别渲染为一个带有复选框(和subs-subs类别)的树形式(对于我的产品:ProduitType),如:

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

我的树枝形式:new_produits.html.twig,My form:ProduitType,My class:Categorie

我在我的树枝上尝试了很多东西,但是对于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;
php symfony twig symfony-forms
1个回答
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 \实体(repositoryClass = “Gedmo \树\实体\库\ NestedTreeRepository”)

希望这可以帮助

© www.soinside.com 2019 - 2024. All rights reserved.