costum column in cgridview

I’m working on a cgridview with, atm this:


$this->widget('zii.widgets.grid.CGridView', 

array(	'id'=>'standaard-verlofuren',	

	'dataProvider'=>$model->search(),

		'columns'=>array(	

		'week',			

		'some value',

	        'some description',

	),

));



which shows me:

week 1 | 15 | description

week 12 | 4 | description

week 13 | 7 | description

etc.

i can get these values for every single employee and list the data in the cgridview

Now i want to have the following columns added while being matched with the weeks that are in the db:

week 1 | first date of the week | 15 | description

week 2 | first date of the week | |

week 3 | first date of the week | |

week 4 | first date of the week | |

week 5 | first date of the week | |

week 6 | first date of the week | |

week 7 | first date of the week | |

week 8 | first date of the week | |

week 9 | first date of the week | |

week 10 | first date of the week | |

week 11 | first date of the week | |

week 12 | first date of the week | 4 | description

week 13 | first date of the week | 7 | description

etc.

i’ve got the array to summon all the weeks of the year and the dates:


function StartOfWeek($year, $week) {     

$Jan1 = mktime(1,1,1,1,1,$year);     

$MondayOffset = (11-date('w',$Jan1))%7-3;     

$desiredMonday = strtotime(($week-1) . ' weeks '.$MondayOffset.' days', $Jan1);     

return $desiredMonday; 

}


for($week = 1; $week<=52; $week++)	

		$weeks[] = date("Y m d",StartOfWeek(2012, $week));



how can i put these two columns in the cgridview and match them with the weeks in de db?

Thanks in advance.

You have to create your own GridColumn class:

in extensions or components or … file MyGridColumn.php




Yii::import('zii.widgets.grid.CGridColumn');

class MyGridColumn extends CGridColumn

{

        protected function renderDataCellContent($row,$data)

        {


//do your stuff here

                echo $desideredMonday;

        }

}



In your gridWiew conf:




                array(

                        'class'=>'ext.MyGridColumn',

                        'header'=>'Monday the:',

                ),



edited CGridView:


$this->widget('zii.widgets.grid.CGridView', 

array(	'id'=>'standaard-verlofuren',	

	'dataProvider'=>$model->findVerlofuren(),

		'columns'=>array(	

		'week',		

		'extra_uren',		

		'verlof_extra_uren',		

		'regulier_verlof',

			array(

				'class'=>'ext.MyGridColumn',

				'header'=>'Monday the:',

			),

	),

));

edited components:


Yii::import('zii.widgets.grid.CGridColumn');


class MyGridColumn extends CGridColumn{

		

	protected function renderDataCellContent($row,$data)        

	{


		for($week = 1; $week<=52; $week++)

				$weeks[] = date("Y m d",StartOfWeek(2012, $week));

		

		echo $weeks[$data->week - 1];

	}

	

}

as you can see i created an array, because I want 52 weeks to be shown.

The view does everything like i want it to do and matches every week when available in the db.

my only question left is: how can i accomplish to summon 52 weeks regardless of what comes out of the db?

(so there will always be 52 weeks, with results for every week that exists in db)

Many thanks in advance!

Did you mean the sum of extra_uren, verlof_extra_uren and regulier_verlof ? If yes this may help you

I’m sorry no,

I mean that for example there are 3 records in the db which are shown in the CGridView.

Every row has its own week like on the image in my previous post.

I want to show all the 52 weeks of the year in this view, regardless of the records i get from the db.

so i get the data like:

(first column represents the week, the other columns are not of any concern)

1 | some number | some number | some number | some description

3 | some number | some number | some number | some description

5 | some number | some number | some number | some description

but i want the data to be shown like:

1 | some number | some number | some number | some description

2

3 | some number | some number | some number | some description

4

5 | some number | some number | some number | some description

6

7

8

9

10

etc.

it seems that you don’t have any ‘week’ field in your db scheme.

  • if there is any ‘week’ field in the db scheme (even a related table with 52 ids), you can get better formatted data from $dataProvider

you can also call protected function renderDataCellContent($row,$data) with parameters, maybe this can do the trick.

I’ve called the function renderDataCellContent allready, thats where i match the week field with the corresponding dates.

The only thing thats wrong is that there must be 52 rows in the CGridView, regardless of what comes out of the db.

Which parameters do i need to fill the view with the weeks that are not in the db?

Could you give me some sample code?

Thanks!