[英]how to display array to CGridView (yii framework)
我有下一個變量:
$type_model = ProductTypeModel::model()->findByPk($id);
$prod = $type_model->product;
現在在$ prod中:
array
(
0 => ProductModel#1
(
[CActiveRecord:_new] => false
[CActiveRecord:_attributes] => array
(
'product_id' => '6'
'product_type_id' => '5'
)
...
)
1 => ProductModel#2
(
'product_id' => '8'
'product_type_id' => '5'
)
...
如何在CGridView中顯示產品? 謝謝。
我假設您正在使用CarrayDataProvider。 因此,在您的控制器中
$dataProvider = new CArrayDataProvider($prod);
在這里,$ product可以是您要在CgridView中顯示的任何數組。 現在,在您的視圖中編寫此內容。
$gridColumns = array(
array(
'header' => 'First Name',
'value' => 'ProductTypeModel::model()->findByPk($data->product_id)->key',
'htmlOptions' => array('style' => 'text-align:center;')
),
$this->widget('zii.widgets.grid.CGridView',array('dataProvider' => $dataProvider,));
由於在CarrayDataprovider
中獲得了數組,因此我們不能在其中使用關系。 那就是為什么你必須寫'ProductTypeModel::model()->findByPk($data->product_id)->key'
在這里,您可以顯示ProductTypeModel
任何屬性,因此您可以將上述key
替換為所需的屬性
試試這個...它會自動轉換為數組數據提供程序。
$dataProvider=new CArrayDataProvider($type_model->product);
謝謝大家 通過回答“ naveen goyal”和“被監禁”,我做到了:
$dataProvider=new CArrayDataProvider($type_model->product);
$dataProvider->keyField = 'product_id';
$this->widget('bootstrap.widgets.TbGridView', array(
'dataProvider' => $dataProvider,
'columns' => array(
array(
'header' => 'Title',
'value' => 'CHtml::encode($data["product_title"])',
),
)));
對我來說很好。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.