[英]How to access a property while binding an array with CGridView in YII
我有一個網格,我需要綁定一個月的列表。 我創建了一個月份列表並將其綁定到列表,如下所示。
$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,));
到目前為止,每件事都很好。 現在在視圖文件中,我有以下代碼
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'users-grid',
'dataProvider'=>$dataProvider,
'columns'=>array(
'Months',
array(
'name'=>'Months',
'value'=>'$data->Months'),
)));
現在問題是我無法在View文件中以$ data-> Months的形式訪問Months,就好像我可以直接訪問幾個月一樣。 如何以$ data-> Months的形式訪問Months 。 我傳遞的$ items數組具有以下值:
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
)
)
您正在傳遞數組數據,因此您需要以'$data["Months"]'
訪問您的數據。
你有很多方法可以做到這一點。 例如:
控制器:
$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,));
視圖:
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.
),
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.