2012-06-11 72 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' 
) 
)