2014-10-29 18 views
0

即時嘗試用javascript和樹枝分類我的項目類別,所以從數據庫中帶來的所有產品後,我不知道如何分離產品與{%爲%}。這是我的枝杈代碼:我如何分類與樹枝分類項目

<div class="col-sm-4" itemscope itemtype="http://schema.org/Product"> 
    <div class="panel panel-default"> 
     <div class="panel-heading"> 
      <h5 class="panel-title truncate">Categoria:{{ producto.idCategoria }}</h5> 
     </div> 
     <div class="panel-body"> 
      Nombre:{{ producto.producto }} 
      <img src="{{ asset('bundles/savainventario/images/'~producto.filePersistencePath) }}" 
       alt="404 file not found" class="img-thumbnail"/> 
     </div> 
     <div class="panel-footer"> 

      <div class="container-fluid"> 

       {#Precio#} 
       <span itemprop="price"> 
       Precio:{{ producto.precio }}.Bsf 
       </span> 
       {#Form#} 
       <form class="form-inline" role="form" method="get" 
         action={{ path('sava_inventario_addcart', {'id': producto.idProducto }) }}> 
        <div class="form-group"> 
         <input class="btn btn-default" type="submit" value="Agregar"> 
        </div> 

        {#Ver mas#} 
        <!-- Button trigger modal --> 
       </form> 
       <button class="btn btn-primary btn-sm" data-toggle="modal" 
         data-target="#myModal{{ producto.idProducto }}"> 
        Ver mas... 
       </button> 
       <!-- Modal --> 
       <div class="modal fade" id="myModal{{ producto.idProducto }}" tabindex="-1" role="dialog" 
        aria-labelledby="myModalLabel" aria-hidden="true"> 
        <div class="modal-dialog"> 
         <div class="modal-content"> 
          <div class="modal-header"> 
           <button type="button" class="close" data-dismiss="modal"><span 
              aria-hidden="true">&times;</span><span class="sr-only">Close</span> 
           </button> 
           <h4 class="modal-title" id="myModalLabel">{{ producto.producto }}</h4> 
          </div> 
          <div class="modal-body"> 
           <!-- Datos productos --> 
           <table class="table table-striped"> 
            <tr> 
             <td>Nombre:</td> 
             <td>{{ producto.producto }}</td> 
            </tr> 
            <tr> 
             <td>Image:</td> 
             <td> 
              <img src="{{ asset('bundles/savainventario/images/'~producto.filePersistencePath) }}" 
               alt="404 file not found"/></td> 
            </tr> 
            <tr> 
             <td>Descripcion</td> 
             <td>{{ producto.descripcionProducto }}</td> 
            </tr> 
            <tr> 
             <td>Precio:</td> 
             <td>{{ producto.precio }}</td> 
            </tr> 
            <tr> 
             <td>Cantidad:</td> 
             <td>{{ producto.cantidad }}</td> 
            </tr> 
            <tr> 
             <td>Categoria:</td> 
             <td>{{ producto.idCategoria.categoria }}</td> 
            </tr> 
            <tr> 
             <td>Modelo:</td> 
             <td>{{ producto.idModelo.modelo }}</td> 
            </tr> 
            <tr> 
             <td>Video:</td> 
             <td> 
              <iframe width="433" height="315" 
                src="//www.youtube.com/embed/tQShyqnRx3s?list=PLw4rBoBPv1Vbq16M4SFkJPZj08FMaaR-8" 
                frameborder="0" allowfullscreen></iframe> 
             </td> 
            </tr> 
           </table> 
          </div> 
          <div class="modal-footer"> 
           <button type="button" class="btn btn-default" data-dismiss="modal">Close</button> 
          </div> 
         </div> 
        </div> 
       </div> 
      </div> 


     </div> {#footer end#} 
    </div> 
</div> 

{% endif %} 

{% endfor %} 

</div> 
</div> 

我的問題是我怎麼能按類別分開我的項目中,一類我打印在一個DIV容器中,另一個DIV容器我添加的產品從其他類別等。

+0

查找控制中斷算法。這裏是C中的一個例子 - http://pastebin.com/sArUmwRa – dmnptr 2014-10-29 15:13:40

+0

你不能破壞樹枝中的循環http://twig.sensiolabs.org/doc/tags/for.html。 – 2one2 2014-10-29 19:38:17

+0

你爲什麼要打破它? – dmnptr 2014-10-29 21:40:11

回答

0

如果您的entites是正確設置這個不應該是很難:

//Contoller 

/** 
* @Template() 
*/ 
public function showProductsByCategory() 
{ 
    $categories = $this->getDoctrine()->getManager() 
     ->getRepository("NamespacedBundle:Category")->findAll(); 
    return array(
     'categories' => $categories 
    ); 
} 

假設你的類別瞭解產品

//Category Entity 
/** 
* @ORM\OneToMany(targetEntity="Product", mappedBy="category") 
*/ 
protected $products; 

而且你的產品聯繫在一起的類別

//Product Entity 
/** 
* @ORM\ManyToOne(targetEntity="Category", inversedBy="products") 
* @ORM\JoinColumn(name="category", referencedColumnName="category_id") 
*/ 
protected $category; 

那麼你的樹枝可能是基於:

//Sample Twig 
{% for category in categories %} 
    <div class="container"> 
     <h1>{{category.name}}</h1> 
     <ul> 
     {% for product in category.products %} 
      <li>{{product.name}}</li> 
     {% endfor %} 
    </div> 
{% endfor %} 
+0

由於即時通訊使用elasticsearch而不是學說,我不能使用這種方法,仍然給了我一個想法,如何解決它。感謝您的回答。 – 2one2 2014-10-29 22:57:03