Yii:CGridView Columns Totals

时间:2015-01-16 08:21:14

标签: yii cgridview

请帮忙!

我需要在CGridView的标题下共有三列(预算,发布和支出),如下所示

|基金|预算|发布|支出|

| |总计:30 |总计:15 |总计:8 |

| A | 10 | 5 | 3 |

| B | 20 | 10 | 5 |

1 个答案:

答案 0 :(得分:1)

您可以通过继承CGridView并覆盖renderTableHeader函数来实现。 1。制作你的网格:

<?php

Yii::import('zii.widgets.grid.CGridView');

    class CGridViewWithTotals extends CGridView
    {
        public function renderTableHeader()
                {
                        if(!$this->hideHeader)
                        {
                                echo "<thead>\n";

                                if($this->filterPosition===self::FILTER_POS_HEADER)
                                        $this->renderFilter();

                                echo "<tr>\n";
                                foreach($this->columns as $column)
                                        $column->renderHeaderCell();
                                echo "</tr>\n";

                                if($this->filterPosition===self::FILTER_POS_BODY)
                                        $this->renderFilter();

                                if($this->getHasFooter())
                                {
                                        echo "<tr>\n";
                                        foreach($this->columns as $column)
                                                $column->renderFooterCell();
                                        echo "</tr>\n";
                                }

                                echo "</thead>\n";
                        }
                        else if($this->filter!==null && ($this->filterPosition===self::FILTER_POS_HEADER || $this->filterPosition===self::FILTER_POS_BODY))
                        {
                                echo "<thead>\n";
                                $this->renderFilter();
                                echo "</thead>\n";
                        }
                }
    }

要防止页脚呈现(如果您不需要),请覆盖CGridView::renderTableFooter()

第二。一如既往地使用新网格:

<?php

$this->widget('CGridViewWithTotals', array(
        'id'=>'my-grid',
        'dataProvider'=>$model->search(),
        'emptyText'=>'No items found.',
        'ajaxUpdate'=>false,
        'columns'=>array(
                array(
                        'name'=>'field1',
                        'footer'=>$total,
                ),
        ),
));

?>