2014-02-10 137 views
1

我旁邊變量:如何顯示陣列CGridView(Yii框架)

在$督促
$type_model = ProductTypeModel::model()->findByPk($id); 
$prod = $type_model->product; 

現在:

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顯示我的產品? Thx。

回答

1

我假設你正在使用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

0

試試這個...... 它自動轉換爲數組數據提供者。

$dataProvider=new CArrayDataProvider($type_model->product); 
0

感謝所有。通過回答「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"])', 
     ), 

))); 

對我來說不錯。