2012-06-11 71 views
1

是否有可能使用cgridview創建嵌套表?yii - 創建嵌套的cgridview

我想有最終輸出如下

| Transaction  | Total | 
| T-001   | $100 | 
     | Item | Price |  // here is the nested table 
     | I-1 | $50 | 
     | I-2 | $50 | 
| T-002   | $90 | 
     | Item | Price |  // here is the nested table 
     | I-3 | $90 | 

我知道你可以使用自定義模板做到這一點,但我想使用類似CGRidView一個部件一個整潔的解決方案。

感謝

回答

1

我覺得達到你想要什麼,最好的辦法就是使用自定義功能,在您的CActiveRecord模型(如果您已經對電網CActiveDataprovider),並把那個「嵌套表」正常列:

| Transaction | Item | Price | Total | 
------------------------------------------ 
| T-001  | I-1 | $50 |  $100 | 
       | I-2 | $50 | 
------------------------------------------ 
| T-002  | I-3 | $90 |  $90 | 
------------------------------------------ 

在你的模型,你所定義的GET功能,在HTML換行與BR返回數據(例如:

class Item extends CActiveRecord { 
... 
public function getIdItems() 
{ 
    $string = ''; 
    foreach($this->items as $item) { 
     if ($string != '') $string .= '<br/>'; 
     $string .= ' '.$item->textId; // 'I-3', 'I-2'... 
    } 
    return $string; 
} 
public function getPriceItems() 
{ 
    $string = ''; 
    foreach($this->items as $item) { 
     if ($string != '') $string .= '<br/>'; 
     $string .= ' '.$item->price; // $50, $90... 
    } 
    return $string; 
} 
... 
} 

,並顯示出日在網格ë新列:

$this->widget('zii.widgets.grid.CGridView', array(
    'id'=>'anotacionesGrid', 
    'dataProvider'=>$dataProvider, 
    'columns'=>array(
     'transaction', 
     'idItems:html:Item', 
     'priceItems:html:Price', 
     'total' 
    ) 
); 
2

如果嵌套表內電池,那麼你可以,只要創建模型中的作用,將呈現表並返回的內容。您可以將widget功能的第三個參數設置爲true來返回內容。如果你想爲嵌套表啓用分頁,那麼一定要手動設置小部件ID並允許ajax更新。

在模型:

function getNestedTable() { 

    return Yii::app()->controller->widget(..., ..., true); 

} 

在列定義使用:

'columns' => array(
    array(
    'name' => 'nestedTable', 
    'type' => 'raw' 
) 
)