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: ****- 3 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)


0

#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: 601
  • 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: 1,527
  • 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

Share this topic:


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

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