Yii CGridView自定义页脚

Yii CGridView自定义页脚,yii,Yii,我使用CGridView以网格格式显示数据,但无法创建自定义页脚, 我使用的代码 <?php $this->widget('zii.widgets.grid.CGridView', array('dataProvider' => $dataProvider, 'columns' => array( array( 'name' => 'creat

我使用CGridView以网格格式显示数据,但无法创建自定义页脚, 我使用的代码

<?php
      $this->widget('zii.widgets.grid.CGridView',
           array('dataProvider' => $dataProvider,
           'columns' => array(
               array(
                   'name' => 'created_date',
                   'header' => 'Created',
               ),
               array(
                   'name' => 'access_date',
                   'header' => 'Accessed',
               ),
               array(
                   'name' => 'referenceCode',
                   'header' => 'Ref Code',
               ),
               array(
                   'name' => 'designation',
                   'header' => 'Designation',
               ),                  
               array(
                   'name' => 'company',
                   'header' => 'Company',
               ),
               array(
                        'name' => 'recommended_actions',
                        'header' => 'Recommended Action',
                        'type' => 'html',
                        'value' => function($jobBoard) {
                            return CHtml::link($recAction[0]['display_text'], Yii::app()->createUrl($actionUrl, $params));
                        }
                    ),
               array(
                   'class' => 'CDataColumn',
                   'header' => 'List of Actions',
                   'type' => 'html',
                   'value' => function($jobBoard){
                        echo '<div class="action-joborder">
                                <ul class="moveto-joborder">
                                    <li>Action <img height="6" width="7" alt="" src="images/bg_action.gif">
                                    <ul>';
                                      echo '<li>'.CHtml::link($actionArray['display_text'], Yii::app()->createUrl($actionUrl, $params)).'</li>';                
                              echo '</ul>
                                </li>
                                </ul>
                               </div>';                        
                   },
                   'name' => 'actions',                    
               ),
    )));         
?>

您可以扩展framework/zii/widgets/grid/CGridView.php并在代码中使用它。扩展以下方法以更改页脚

public function renderTableFooter()
{
  $hasFilter=$this->filter!==null && $this->filterPosition===self::FILTER_POS_FOOTER;
  $hasFooter=$this->getHasFooter();
  if($hasFilter || $hasFooter)
  {
    echo "<tfoot>\n";
    if($hasFooter)
    {
      echo "<tr>\n";
      foreach($this->columns as $column)
              $column->renderFooterCell();
      echo "</tr>\n";
    }
    if($hasFilter)
      $this->renderFilter();
    echo "</tfoot>\n";
  }
}
公共函数renderTableFooter()
{
$hasFilter=$this->filter!==null&&$this->filterPosition===self::filter\u POS\u FOOTER;
$hasFooter=$this->getHasFooter();
如果($hasFilter | |$hasFooter)
{
回音“\n”;
如果($hasFooter)
{
回音“\n”;
foreach($this->columns as$column)
$column->renderFooterCell();
回音“\n”;
}
如果($hasFilter)
$this->renderFilter();
回音“\n”;
}
}

protected/components/GridView.php

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


class GridView extends CGridView
{

/**
 * Renders the table footer.
 */
public function renderTableFooter()
{
    $hasFilter=$this->filter!==null && $this->filterPosition===self::FILTER_POS_FOOTER;
    $hasFooter=$this->getHasFooter();
    if($hasFilter || $hasFooter)
    {
        echo "<tfoot>\n";
        if($hasFooter)
        {
            echo "<tr>\n";
            foreach($this->columns as $column){
                if($column->footer!='')
                    $column->footer=eval("return " . $column->footer.";");

                $column->renderFooterCell();
            }
            echo "</tr>\n";
        }
        if($hasFilter)
            $this->renderFilter();
        echo "</tfoot>\n";
    }
}

public function sumTotal($attribute){
    $data=$this->dataProvider->getData();

    $sum=0;
    foreach($data as $index => $value ){

        if(isset($data[$index]->{$attribute}))
            $sum+=$data[$index]->{$attribute};
    }

    return $sum;
}

} 
Yii::import('zii.widgets.grid.CGridView');
类GridView扩展了CGridView
{
/**
*呈现表格页脚。
*/
公共函数renderTableFooter()
{
$hasFilter=$this->filter!==null&&$this->filterPosition===self::filter\u POS\u FOOTER;
$hasFooter=$this->getHasFooter();
如果($hasFilter | |$hasFooter)
{
回音“\n”;
如果($hasFooter)
{
回音“\n”;
foreach($this->columns as$column){
如果($column->footer!='')
$column->footer=eval(“返回“$column->footer.”;”;
$column->renderFooterCell();
}
回音“\n”;
}
如果($hasFilter)
$this->renderFilter();
回音“\n”;
}
}
公共函数总和($attribute){
$data=$this->dataProvider->getData();
$sum=0;
foreach($index=>$value的数据){
if(isset($data[$index]->{$attribute}))
$sum+=$data[$index]->{$attribute};
}
返回$sum;
}
} 
视图/{controllers}/{action}.php

  $this->widget('GridView',
       array('dataProvider' => $dataProvider,
       'columns' => array(
           array(
               'name' => 'created_date',
               'header' => 'Created',
           ),
           array(
               'name' => 'access_date',
               'header' => 'Accessed',
           ),
           array(
               'name' => 'referenceCode',
               'header' => 'Ref Code',
           ),
           array(
               'name' => 'designation',
               'header' => 'Designation',
           ),                  
           array(
               'name' => 'company',
               'header' => 'Company',
           ),
           array(
                    'name' => 'recommended_actions',
                    'header' => 'Recommended Action',
                    'type' => 'html',
                    'value' => function($jobBoard) {
                        return CHtml::link($recAction[0]['display_text'], Yii::app()->createUrl($actionUrl, $params));
                    }
                    'footer'=> 'number_format($this->sumTotal("recommended_actions"))',

           ),
           array(
               'class' => 'CDataColumn',
               'header' => 'List of Actions',
               'type' => 'html',
               'value' => function($jobBoard){
                    echo '<div class="action-joborder">
                            <ul class="moveto-joborder">
                                <li>Action <img height="6" width="7" alt="" src="images/bg_action.gif">
                                <ul>';
                                  echo '<li>'.CHtml::link($actionArray['display_text'], Yii::app()->createUrl($actionUrl, $params)).'</li>';                
                          echo '</ul>
                            </li>
                            </ul>
                           </div>';                        
               },
               'name' => 'actions',                    
           ),
)));         
$this->widget('GridView',
数组('dataProvider'=>$dataProvider,
'列'=>数组(
排列(
“名称”=>“创建日期”,
'标题'=>'已创建',
),
排列(
“名称”=>“访问日期”,
'标题'=>'已访问',
),
排列(
'name'=>'referenceCode',
'标题'=>'参考代码',
),
排列(
“名称”=>“名称”,
“标题”=>“名称”,
),                  
排列(
“名称”=>“公司”,
'标题'=>'公司',
),
排列(
“名称”=>“建议的行动”,
“标题”=>“建议的操作”,
'type'=>'html',
“值”=>函数($jobBoard){
返回CHtml::link($recAction[0]['display_text'],Yii::app()->createUrl($actionUrl,$params));
}
'footer'=>'number_format($this->sumTotal(“建议的_操作”)),
),
排列(
“类”=>“CDATA列”,
'标题'=>'操作列表',
'type'=>'html',
“值”=>函数($jobBoard){
回声'
  • 行动
      ”; echo“
    • ”.CHtml::link($actionArray['display_text'],Yii::app()->createUrl($actionUrl,$params))。
    • ; 回声'
'; }, “名称”=>“操作”, ), )));