2013-10-17 48 views
0

我正在使用Symfony2和樹枝:我有一個設置爲使用Doctrine處理分層數據的表格。這是一個類別列表與父 - >兒童 - > sub_children-> sub_sub_children等...許多變化。從樹枝模板中的表格顯示分層數據

我能夠在網頁上顯示它們像這樣,但問題是沒有「缺口」,以顯示其元素屬於哪個父母或子女或子女的孩子等等等等

<table class="table" id="tableList"> 
      <thead> 
      <tr> 
       <th>Category Name</th> 
      </tr> 
      </thead> 
      <tbody> 
      {% for productCategory in productCategories %} 
       <tr> 
        <td>{{ productCategory.name }}</td> 
       </tr> 
      {% endfor %} 
      </tbody> 
     </table> 

我倒是像列表看起來是這樣的:

Parent 
     Child 
      Child 
Parent 
     Child 
Parent 
     Child 
      Child 
       Child 

,而不是像這樣:

Parent 
Child 
Child 
Child 
Parent 
Child 
Parent 
etc. 
etc. 

原始實體:

<?php 

namespace WIC\ProductCategoryBundle\Entity; 

use Doctrine\ORM\Mapping as ORM; 
use Doctrine\Common\Collections\ArrayCollection; 
use Gedmo\Mapping\Annotation as Gedmo; 
use WIC\CommonBundle\DoctrineExtensions\Mapping\Annotation as Common; 
use Symfony\Component\Validator\Constraints as Assert; 


/** 
    * ProductCategory 
    * 
    * @ORM\Table() 
    * @ORM\Entity(repositoryClass="Gedmo\Tree\Entity\Repository\NestedTreeRepository") 
    * @Gedmo\Tree(type="nested") 
    * @Common\Loggable(logEntryClass="WIC\ProductCategoryBundle\Entity\ProductCategoryLog") 
    * @Common\Accountable 
    * @Gedmo\SoftDeleteable(fieldName="deletedAt") 
    */ 
class ProductCategory 
{ 
/** 
* @var integer 
* 
* @ORM\Column(name="id", type="integer") 
* @ORM\Id 
* @ORM\GeneratedValue(strategy="AUTO") 
*/ 
private $id; 

/** 
* @var string 
* @ORM\Column(name="name", type="string", length=255, nullable=false) 
* @Common\Versioned 
* @Assert\NotBlank(message="Location Name Cannot Be Left Blank.") 
*/ 
protected $name; 

/** 
* @Gedmo\TreeLeft 
* @ORM\Column(name="lft", type="integer") 
*/ 
protected $lft; 

/** 
* @Gedmo\TreeLevel 
* @ORM\Column(name="lvl", type="integer") 
*/ 
protected $lvl; 

/** 
* @Gedmo\TreeRight 
* @ORM\Column(name="rgt", type="integer") 
*/ 
protected $rgt; 

/** 
* @Gedmo\TreeRoot 
* @ORM\Column(name="root", type="integer", nullable=true) 
*/ 
protected $root; 

/** 
* @Gedmo\TreeParent 
* @ORM\ManyToOne(targetEntity="ProductCategory", inversedBy="children") 
* @ORM\JoinColumn(name="parent_id", referencedColumnName="id", onDelete="SET NULL") 
*/ 
protected $parent; 

/** 
* @ORM\OneToMany(targetEntity="ProductCategory", mappedBy="parent") 
* @ORM\OrderBy({"lft" = "ASC"}) 
*/ 
protected $children; 

/** 
* @ORM\ManyToMany(targetEntity="WIC\ProductBundle\Entity\Product", mappedBy="productCategories", cascade={"persist"}) 
*/ 
protected $products; 

/** 
* @ORM\ManyToOne(targetEntity="WIC\UserBundle\Entity\User") 
* @ORM\JoinColumn(name="created_by", referencedColumnName="id") 
* @Common\Blameable(on="create") 
*/ 
private $createdBy; 

/** 
* @ORM\ManyToOne(targetEntity="WIC\UserBundle\Entity\User") 
* @ORM\JoinColumn(name="updated_by", referencedColumnName="id") 
* @Common\Blameable(on="update") 
*/ 
private $updatedBy; 

/** 
* @ORM\ManyToOne(targetEntity="WIC\AccountBundle\Entity\Account", inversedBy="productCategorys", cascade={"remove","persist"}) 
* @ORM\JoinColumn(name="account_id", referencedColumnName="id", nullable=false, onDelete="CASCADE") 
* @Common\Versioned 
* @Common\Blameable(on="create") 
*/ 
protected $account; 


/** 
* @var datetime $created 
* 
* @Common\Timestampable(on="create") 
* @ORM\Column(type="datetime") 
*/ 
private $created; 

/** 
* @var datetime $updated 
* 
* @Common\Timestampable(on="update") 
* @ORM\Column(type="datetime", nullable=true) 
*/ 
protected $updated; 

/** 
* @ORM\Column(name="deletedAt", type="datetime", nullable=true) 
*/ 
private $deletedAt; 


public function __construct() 
{ 
    $this->products = new ArrayCollection(); 
} 


/** 
* Get id 
* 
* @return integer 
*/ 
public function getId() 
{ 
    return $this->id; 
} 

/** 
* Set name 
* 
* @param string $name 
* @return ProductCategory 
*/ 
public function setName($name) 
{ 
    $this->name = $name; 

    return $this; 
} 

/** 
* Get name 
* 
* @return string 
*/ 
public function getName() 
{ 
    return $this->name; 
} 


public function setLvl(ProductCategory $lvl = null) 
{ 
    $this->lvl = $lvl; 
} 

public function getLvl() 
{ 
    return $this->lvl; 
} 


public function setParent(ProductCategory $parent = null) 
{ 
    $this->parent = $parent; 
} 

public function getParent() 
{ 
    return $this->parent; 
} 

/** 
* Set account 
* 
* @param \WIC\AccountBundle\Entity\Account $account 
* @return User 
*/ 
public function setAccount(\WIC\AccountBundle\Entity\Account $account = null) 
{ 
    $this->account = $account; 

    return $this; 
} 


/** 
* Get account 
* 
* @return \WIC\AccountBundle\Entity\Account 
*/ 
public function getAccount() 
{ 
    return $this->account; 
} 

/** 
* Add product 
* 
* @param \WIC\ProductBundle\Entity\Product $product 
* @return Product 
*/ 
public function addProduct(\WIC\ProductBundle\Entity\Product $product) 
{ 
    $this->products[] = $product; 

    return $this; 
} 

/** 
* Remove product 
* 
* @param \WIC\ProductBundle\Entity\Product $product 
*/ 
public function removeProduct(\WIC\ProductBundle\Entity\Product $product) 
{ 
    $this->products->removeElement($product); 
} 

/** 
* Set products 
* 
* @return Product 
*/ 
public function setProducts($products) 
{ 
    $this->products = $products; 

    return $this; 
} 

/** 
* Get products 
* 
* @return \Doctrine\Common\Collections\Collection 
*/ 
public function getProducts() 
{ 
    return $this->products; 
} 

/** 
* Set updated 
* 
* @param \DateTime $updated 
* @return User 
*/ 
public function setUpdated($updated) 
{ 
    $this->updated = $updated; 

    return $this; 
} 

/** 
* Get updated 
* 
* @return \DateTime 
*/ 
public function getUpdated() 
{ 
    return $this->updated; 
} 

/** 
* Set deletedAt 
* 
* @param \DateTime $deletedAt 
* @return User 
*/ 
public function setDeletedAt($deletedAt) 
{ 
    $this->deletedAt = $deletedAt; 

    return $this; 
} 

/** 
* Get deletedAt 
* 
* @return \DateTime 
*/ 
public function getDeletedAt() 
{ 
    return $this->deletedAt; 
} 

public function getOptionLabel() 
{ 
    return str_repeat(
      html_entity_decode('&nbsp;', ENT_QUOTES, 'UTF-8'), 
      ($this->getLvl() + 0) * 3 
    ) . $this->getName(); 
} 


} 

任何人都知道如何做到這一點?

非常感謝!

+0

你可以發佈'productCategory'的實體結構嗎?需要知道您的父類和子類是如何關聯和訪問的,以幫助代碼遍歷它們。 –

+0

編輯原帖以反映,謝謝。 – LargeTuna

回答

3

使用無序列表,縮進將更容易,因爲它們會自動由瀏覽器的默認用戶代理樣式表縮進。這應做到:

此方法添加到您的產品分類實體:

public function getChildren() { 
    return $this->children; 
} 

,這在樹枝模板

{% for productCategory in productCategories %} 
    {{ _self.display_tree(productCategory) }} 
{% endfor %} 

{% macro display_tree(level) %} 
    <ul> 
     <li>{{ level.name }} 
     {% if level.children|default() %} 
      {% for child in level.children %} 
       {{ _self.display_tree(child) }} 
      {% endfor %} 
     {% endif %} 
     </li> 
    </ul> 
{% endmacro %} 
+0

謝謝肯給我一個答案,但我有點問題。我在安裝代碼後收到此錯誤: 對象「WIC \ ProductCategoryBundle \ Entity \ ProductCategory」的方法「children」在/ Applications/XAMPP/xamppfiles/htdocs/symfonydev/src/WIC/ProductCategoryBundle/Resources中不存在/views/ProductCategory/list.html.twig第58行 – LargeTuna

+0

哦,我只是看着你的實體,你需要添加一個'getChildren()'方法。更新我的答案。 –

+0

哦,您可能還需要在'ProductCategory'實體的'__construct()'中將'children'設置爲'ArrayCollection',就像您製作產品一樣。 –

0

只是一點點升級到前面的回答。

<ul> 
{% for productCategory in productCategories %} 
    {{ _self.display_tree(productCategory) }} 
{% endfor %} 
</ul> 

{% macro display_tree(level) %} 
    <li>{{ level.name }} 
    {% if level.children|default() %} 
     <ul> 
     {% for child in level.children %} 
      {{ _self.display_tree(child) }} 
     {% endfor %} 
     </ul> 
    {% endif %} 
    </li> 
{% endmacro %} 

這將構建更好看的HTML。