Yii 1.1: gridcolumns

Grid View columns to provide column total in the footer, calculate running totals, and calculate values from other columns
22 followers

A collection of three columns for use with GCridView.

  • TotalColumn - puts the total value in the footer cell
  • RunningTotalColumn - the cell values are running totals (as in a bank account balance)
  • CalcColumn - cell values are calculated from other grid columns. The footer cell value can be the total of column cell values or calculated from other columns (the referenced columns must have the footerCell value)

Use of these columns save going to the model (and perhaps database) for calculations, and can be used with CArrayDataProvider (i.e. where there may not even be a data model).

Requirements

Developed using Yii 1.1.8 but should work with all releases having zii.

Documentation

Properties

All of these columns change the use of the "value" property and add the "output" property.

value string A PHP expression that will be evaluated and whose result will be used as the value (not the rendered output) of the cell; the expression can contain the variables $row - the row number (zero-based), $data - the data model for the row; and $this - the column object. (also see CalcColumn) output string A PHP expression that will be evaluated and whose result will be used to render the footer cell. The expression can contain the variables $value - the footer cell value and $this - the column.

init mixed Either, float: the initial value of the total, or string: A PHP expression that will be evaluated when the grid initialises and whose result becomes the initial value of the total. The expression can contain the variable $this - the column object.

TotalColumn

footer mixed boolean:TRUE means use the output expression to render the value; string: A PHP expression that will be evaluated and whose result will be used to render the footer cell. The expression can contain the variables $value - the footer cell value and $this - the column; any other value means render the footer value as a number.

CalcColumn

value string In addition to the usual variables the expression can reference other grid columns by $cn, where n is the zero-based column number, example: $c0+$c1+$c2 sums the first three columns in the grid. A CalcColumn can be used in the calculation of another CalcColumn; self and circular references are not allowed.

footer mixed boolean:TRUE the footer cell value is the column total; string: a PHP expression that will be evaluated and whose result is the value of the footer cell; the expression can contain the variables $total - the column total, references to other grid columns by $cn (see above; referenced columns must have the "footerCellValue" property), and $this - the column object.

footerOutput mixed If empty the footer cell is not rendered; this property determines whether the column has a footer (see CGridColumn::hasFooter); boolean:TRUE means use the output expression to render the value; string: A PHP expression that will be evaluated and whose result will be used to render the footer cell. The expression can contain the variables $value - the footer cell value and $this - the column; any other value means render the footer value as a number.

Installation

Extract the files in the archive and place in an application directory of your choice.

Usage

Use just like any other grid view column, i.e. in CGridView::columns set the class as the path alias to the column class and declare the properties; e.g.

$this->widget('zii.widgets.grid.CGridView', array(
  'dataProvider'=>$dataProvider,
  'columns'=>array(
     ...
     array(
      'class'=>'path.to.TotalColumn',
      'name'=>'value',
      'output'=>'Yii::app()->getNumberFormatter->formatCurrency($value,'.$currency.')',
      'type'=>'raw',
      'footer'=>true
     ),
     ...
  )
));

Total 5 comments

#15671 report it
acorncom at 2013/12/05 12:28pm
Added a Git repo

It's available here: https://github.com/acorncom/gridcolumns

Please submit any bug fixes, etc here. I'll be adding Composer/Packagist support over time as well.

@Yeti, happy to move the repo to your Github account if you want.

#14694 report it
Daniel at 2013/09/03 10:12pm
How to refer to other column value?

Hi, I want to format the balance according to the normal balance column, How can I achieve this?

array(
            'class' => 'ext.gridcolumns.TotalColumn',
            'footer' => true,
            'header' => 'Initial Balance [C]',
            'type' => 'raw',
            'value' => '$data["initBalC"] === null ? "" : floatval($data["initBalC"])',
            'output' => '$value === "" ? "" : ($value >= 0 ? number_format($value, 2, ",", ".") : "<span class=\"text-error\">(" . number_format($value, 2, ",", ".") . ")</span>")',
            'htmlOptions' => array('style' => 'text-align: right; width: 120px;'),
            'footerHtmlOptions' => array('style' => 'font-weight: bold; text-align: right; width: 120px;'),
        ),

In the output ...($value >= 0 ? .... I want to add ...($value >= 0 && $data["normal"] == "D" ?.....

But it complained for the $data. How could I refer to this column?

Thank you in advance.

Daniel

#7583 report it
acorncom at 2012/03/31 05:57pm
Found a bug

In the init function of TotalColumn:

public function init() {
....
    if(is_numeric($this->init))
        $this->l=$this->init;

should be (change is in last line):

public function init() {
....
    if(is_numeric($this->init))
        $this->_total=$this->init;

Thanks for the extension, it's quite helpful!

#7561 report it
russellfeeed at 2012/03/30 05:55am
Got it now!

This works for me - there are some ()'s missing from the example

array(
    'header'=>'Total Due',
    'class'=>'ext.TotalColumn',
    'value'=>'(floatval($data->price_ex_vat)+floatval($data->delivery_price_ex_vat)) * '.(1+$this->vat_rate),
    //'name'=>'value',
    'output'=>'Yii::app()->getNumberFormatter()->formatCurrency($value,"GBP")',
    'type'=>'raw',
    'footer'=>true
),

Thanks for a great extension!

#7560 report it
russellfeeed at 2012/03/30 05:37am
Some clarification please....?

Looks like this extension is just what I need... however I have some questions 1. Does TotalColumn actually add up all the values in the column for you? 2. How does the 'name' parameter affect things? Is it significant? 3. In your example, can you elaborate more on the line

'output'=>'Yii::app()->getNumberFormatter->formatCurrency($value,'.$currency.')',

What is $value?

My widget looks like this...

$this->widget('zii.widgets.grid.CGridView', array(
  'dataProvider'=>$dataProvider,
  'columns'=>array(
     ...
     array(
      'header'=>'Total Due',
      'class'=>'ext.TotalColumn',
      'value'=>'(floatval($data->price_ex_vat)+floatval($data->delivery_price_ex_vat)) * '.(1+$this->vat_rate),
      'name'=>'value',
      'output'=>'Yii::app()->getNumberFormatter->formatCurrency($value,"GBP")',
      'type'=>'raw',
      'footer'=>true
     ),
     ...
  )
));

Thanks in advance

Russell

Leave a comment

Please to leave your comment.

Create extension