【问题标题】:Yii: CGridView Columns TotalsYii:CGridView 列总计
【发布时间】:2015-01-16 08:21:14
【问题描述】:

请帮忙!

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

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

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

|一个 | 10 | 5 | 3 |

|乙| 20 | 10 | 5 |

【问题讨论】:

  • 我认为你可以通过 jquery 做到这一点。为您的网格提供一个 ID,然后在 document.ready 上计算总数。因为 CGridView 没有这个功能。或者您可以使用其他工具,例如 JqGrid 插件
  • 谢谢亲爱的,但我不精通 jquery。
  • 我可以对页脚中的列求和,但不能在页眉中求和。

标签: yii cgridview


【解决方案1】:

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

<?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,
                ),
        ),
));

?>

【讨论】:

    猜你喜欢
    • 2013-09-16
    • 1970-01-01
    • 1970-01-01
    • 2012-03-12
    • 1970-01-01
    • 2020-01-02
    • 1970-01-01
    • 2012-06-20
    • 2015-06-19
    相关资源
    最近更新 更多