How do I calculate a column in CgridView?

Hello,

I have a qty and price for each row and I want to calculate the extended price.

I also want to total this value.

(qty * price) = extended price.

I have searched the forum, but cannot get this working.

Thanks for the help.

Frank

in your model:




public function getExtendedPrice()

{

	return $this->qty * $this->price;

}



access with:


$data->ExtendedPrice

Thanks, that works great.

How can I also total the result in the footer?

I appriciate the help.

in your ExtentedPrice column you can use the property ‘footer’: http://www.yiiframework.com/doc/api/1.1/CGridColumn#footer-detail

you must create a function to sum each line for you

Thank you,

This seems to be working.




//CGridView

array( 

					'name'=>'ExtPrice', 

					'value'=>$data->ExtPrice,

					'footer'=>$model->getExtTotals(), 

		),


//model

public function getExtTotals()

	{

		$exttotal = 0;

		$cart = $this->getCart();

		foreach ($cart as $row)

			$exttotal += $row->ct_price * $row->ct_qty;

		return number_format($exttotal,2);

	}