A collection of three columns for use with GCridView.
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).
Developed using Yii 1.1.8 but should work with all releases having zii.
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.
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.
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.
Extract the files in the archive and place in an application directory of your choice.
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 3 comments
In the init function of TotalColumn:
should be (change is in last line):
Thanks for the extension, it's quite helpful!
This works for me - there are some ()'s missing from the example
Thanks for a great extension!
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
What is $value?
My widget looks like this...
Thanks in advance
Russell
Leave a comment
Please login to leave your comment.