首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >树层次渲染symfony 4

树层次渲染symfony 4
EN

Stack Overflow用户
提问于 2019-02-01 21:38:29
回答 1查看 748关注 0票数 2

我正在学习Symfony 4,我想将我的类别和子类别呈现为一个带有复选框(和子类别)的树(对于我的产品: ProduitType),如下所示:

代码语言:javascript
复制
[ ]Category 1
     [ ]Sub Category 1
         [ ]Sub-Sub Category 1
     [ ]Sub Category 2
 [ ]Category 2 etc...

我的细枝形式: new_produits.html.twig,我的形式: ProduitType,我的类: Categorie

我尝试了很多东西,但是Symfony 4上没有更多的帮助,如果有人能给我一些建议,请告诉我。

代码语言:javascript
复制
 <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) }}
代码语言:javascript
复制
->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;
                }
代码语言:javascript
复制
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;

EN

回答 1

Stack Overflow用户

发布于 2019-03-15 23:16:16

你找到路了吗?

试试这个:

代码语言:javascript
复制
$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 one changing存储库注释来

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

希望这能有所帮助

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54480693

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档