4

I am using CGridView to show data in grid format, but i am not able to create a custom footer, the code that i am using,

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

the above code gives me footer with pagination only, i want to place a some Button element to give the grid some control. the grid should look like, enter image description here

is there any way i can make custom element at the footer.

2 Answers 2

5

You can extend framework/zii/widgets/grid/CGridView.php and use it in your code. extend the following method to change 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)
              $column->renderFooterCell();
      echo "</tr>\n";
    }
    if($hasFilter)
      $this->renderFilter();
    echo "</tfoot>\n";
  }
}
0
1

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;
}

} 

views/{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',                    
           ),
)));         

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.