Yii: CGridView Columns Totals
请帮忙!
我需要在 CGridView 的标题下总计三列(预算、发布和支出),如下所示
|基金 |预算 |发布 |支出|
| |总计:30 |总计:15 |总计:8 |
|一个 | 10 | 5 | 3 |
|乙| 20 | 10 | 5 |
您可以通过继承 CGridView 并覆盖
第一个。制作你的网格:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | <?php Yii::import('zii.widgets.grid.CGridView'); class CGridViewWithTotals extends CGridView { public function renderTableHeader() { if(!$this->hideHeader) { echo"<thead>\ "; if($this->filterPosition===self::FILTER_POS_HEADER) $this->renderFilter(); echo"<tr>\ "; foreach($this->columns as $column) $column->renderHeaderCell(); echo"</tr>\ "; if($this->filterPosition===self::FILTER_POS_BODY) $this->renderFilter(); if($this->getHasFooter()) { echo"<tr>\ "; foreach($this->columns as $column) $column->renderFooterCell(); echo"</tr>\ "; } echo"</thead>\ "; } else if($this->filter!==null && ($this->filterPosition===self::FILTER_POS_HEADER || $this->filterPosition===self::FILTER_POS_BODY)) { echo"<thead>\ "; $this->renderFilter(); echo"</thead>\ "; } } } |
为了防止页脚渲染(如果你不需要它),覆盖
第二。一如既往地使用你的新网格:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | <?php $this->widget('CGridViewWithTotals', array( 'id'=>'my-grid', 'dataProvider'=>$model->search(), 'emptyText'=>'No items found.', 'ajaxUpdate'=>false, 'columns'=>array( array( 'name'=>'field1', 'footer'=>$total, ), ), )); ?> |