如何将数组显示到CGridView(yii框架)

时间:2014-02-10 08:40:33

标签: php yii

我有下一个变量:

$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中显示我的产品? THX。

3 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

试试这个...... 它会自动转换为数组数据提供者。

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

答案 2 :(得分: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"])',
        ),

)));

对我来说很好。

相关问题