You are passing array data so you need to access your data as '$data["Months"]'
.
How to access a property while binding an array with CGridView in YII
-
23-06-2022 - |
Question
I have a grid in which I need to bind a list of months. I have create a list of months and bind it to the list as shown below.
$items = $this->getMonths();
$dataProvider= new CArrayDataProvider(array(),array('keyField'=>false));
$dataProvider->setData($items);
$this->render('monthlyReports',
array('model'=>$this->loadModel($_POST['Users']['user_id']),
'dataProvider'=>$dataProvider,));
Upto this every thing is working fine. Now in the view file I have the following code
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'users-grid',
'dataProvider'=>$dataProvider,
'columns'=>array(
'Months',
array(
'name'=>'Months',
'value'=>'$data->Months'),
)));
Now the problem is that I am not able to access Months as $data->Months in the View file where as if I directly access months I can do so. How can I access the Months as $data->Months. The $items array that I am passing has the following values:
Array
(
[0] => Array
(
[Months] => January
)
[1] => Array
(
[Months] => February
)
[2] => Array
(
[Months] => March
)
[3] => Array
(
[Months] => April
)
[4] => Array
(
[Months] => May
)
[5] => Array
(
[Months] => June
)
)
Solution
OTHER TIPS
You have many ways to do this. Ex:
Controller:
$monthList = $this->getMonths();
$this->render('monthlyReports',
array('model'=>$this->loadModel($_POST['Users']['user_id']),
'monthList'=>$monthList, // Pass the month list to the view
'dataProvider'=>$dataProvider,));
View:
array(
'name'=>'Months',
'value'=>function ($data, $row) use ($monthList){ return $data->Month? $monthList[$data->Month]['Months'] : '' ; }, // $data->Month it's the model attribute with the month value.
),
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow