Yii Framework Forum: Yii Cgridview Footer With Sum Of Column Values - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Yii Cgridview Footer With Sum Of Column Values yii cgridview footer with sum Rate Topic: ****- 8 Votes

#1 User is offline   Sivanthi 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 21
  • Joined: 14-February 13
  • Location:Nagercoil, Kanya Kumari, Tamilnadu. India

Posted 15 March 2013 - 06:13 AM

In the cgridview add following lines

'columns'=>array(
		................
		array(
		'name'=>'column name',
		'type'=>'text',
		'footer'=>$model->getTotals($model->search()->getKeys()),
		),
		..................
		),



we are passing currently displayed primary keys as parameter to getTotals function [in array format]


kindly create a function in model like below


public function getTotals($ids)
	{
		$ids = implode(",",$ids);
		
		$connection=Yii::app()->db;
		$command=$connection->createCommand("SELECT SUM(columnname)
											FROM `tablename` where id in ($ids)");
		return "Total Rs: ".$amount = $command->queryScalar();
	}

Attached File(s)


3

#2 User is offline   savoo 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 1
  • Joined: 14-March 13

Posted 15 March 2013 - 07:08 AM

nice
0

#3 User is offline   seenivasan 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 620
  • Joined: 17-June 12
  • Location:Chennai,TamilNadu,India.

Posted 15 March 2013 - 09:46 AM

Dear Friend

Let us have a model :Wage.

And we are having a field days.

admin.php
.......other columns......
array(
	'name'=>'days',
	'footer'=>"Total: ".$model->fetchTotalDays($model->search()->getKeys()),
		),
........other columns......


The model function in Wage.php
public function fetchTotalDays($keys)
{
	$wages=self::model()->findAllByPk($keys);
	$days=0;
	foreach($wages as $wage)
		$days+=$wage->days;
	return $days;
}	


Actually CActiveDataProvider::getKeys is wrapper around protected function CActiveDataProvider::fetchKeys.
CActiveDataProvider::fetchKeys get primaryKeys in array from CActiveDataProvider::getData.

Instead of using CActiveDataProvider::getKeys,we can use CActiveDataProvider::getData.

the column declaration in admin.php.
array(
	'name'=>'days',
	'footer'=>"Total: ".$model->fetchTotalDays($model->search()->getData()),
		),


The utility function in the model.
public function fetchTotalDays($records)
{
	$days=0;
	foreach($records as $record)
		$days+=$record->days;
	return $days;
}	


Regards.
1

#4 User is offline   softark 

  • Keep It Simple
  • Yii
  • Group: Moderators
  • Posts: 2,071
  • Joined: 16-February 11
  • Location:Japan

Posted 16 March 2013 - 02:08 AM

/* Moved from "General Discussion for Yii 1.1.x" to "Tips, Snippets and Tutorials" */
0

#5 User is offline   abundance 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 5
  • Joined: 09-July 13

Posted 06 September 2013 - 02:31 PM

Good stuff. Thanks.

You can also use YiiBooster's Extended Gridview as well to perform this function and many more.
0

#6 User is offline   vicente.zambrano 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 5
  • Joined: 14-July 12

Posted 14 November 2013 - 11:32 AM

Perfect and simple, also could be extended to more functionalities.
0

#7 User is offline   hajdar 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 11
  • Joined: 16-November 13

Posted 17 November 2013 - 03:27 PM

Thanks, great tutorial!!!
0

#8 User is offline   Gustavo Andrade 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 121
  • Joined: 16-August 11
  • Location:Brazil - MG

Posted 08 January 2014 - 06:54 PM

Easy e usefull tutorial!!! Congrats!
0

#9 User is offline   vuxor 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 1
  • Joined: 28-January 14

Posted 01 February 2014 - 11:23 AM

If anyone need to get totals from multiple columns you can use code like this:

this chunk of code goes to your view file
'columns'=>array(
                ..........
		array(
			'name'=>'age',
			'footer'=>'Total: ' . $model->getTotal($model->search()->getData(), 'age'),
		),
		array(
			'name'=>'weight',
			'footer'=>'Total: ' . $model->getTotal($model->search()->getData(), 'weight'),
		),
		..........
	),


and this one goes to your model file
	public function getTotal($records, $column)
	{
		$total = 0;
		foreach ($records as $record) {
			$total += $record->$column;
		}
		return $total;
	}

2

#10 User is offline   tyankee 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 14
  • Joined: 26-March 14

Posted 08 May 2014 - 11:47 PM

View Postseenivasan, on 15 March 2013 - 09:46 AM, said:

Dear Friend

Let us have a model :Wage.

And we are having a field days.

admin.php
.......other columns......
array(
	'name'=>'days',
	'footer'=>"Total: ".$model->fetchTotalDays($model->search()->getKeys()),
		),
........other columns......


The model function in Wage.php
public function fetchTotalDays($keys)
{
	$wages=self::model()->findAllByPk($keys);
	$days=0;
	foreach($wages as $wage)
		$days+=$wage->days;
	return $days;
}	


Actually CActiveDataProvider::getKeys is wrapper around protected function CActiveDataProvider::fetchKeys.
CActiveDataProvider::fetchKeys get primaryKeys in array from CActiveDataProvider::getData.

Instead of using CActiveDataProvider::getKeys,we can use CActiveDataProvider::getData.

the column declaration in admin.php.
array(
	'name'=>'days',
	'footer'=>"Total: ".$model->fetchTotalDays($model->search()->getData()),
		),


The utility function in the model.
public function fetchTotalDays($records)
{
	$days=0;
	foreach($records as $record)
		$days+=$record->days;
	return $days;
}	


Regards.


could you please explain where the array '$records' comes from?? is this a field in one of your tables??
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

3 User(s) are reading this topic
0 members, 3 guests, 0 anonymous users