Yii CGridView自定义页脚

时间:2011-12-14 08:54:16

标签: yii

我使用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',                    
               ),
    )));         
?>

上面的代码只给了我页脚的分页,我想放置一些Button元素给网格一些控件。 网格看起来像, enter image description here

有什么方法可以在页脚上创建自定义元素。

2 个答案:

答案 0 :(得分:4)

您可以扩展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";
  }
}

答案 1 :(得分:0)

保护/组件/ 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;
}

} 

视图/ {控制器} / {行动} .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',                    
           ),
)));