简体   繁体   English

Yii CActiveDataProvider与类参数?

[英]Yii CActiveDataProvider with class argument?

I found this code at this website. 我在网站上找到了此代码。 What would have to be in the $dp dataprovider for the class TotalColumn to be called in the CGridView? 要在CGridView中调用类TotalColumn,必须在$ dp数据提供程序中包含什么? Do I have to have the class TotalColumn be somewhere in $dp? 我是否必须将类TotalColumn放在$ dp中? Any idea how I would declare that CActiveDataProvider? 我知道如何声明该CActiveDataProvider吗?

<?php
// protected/views/inventory/index.php

Yii::import('zii.widgets.grid.CGridColumn');

class TotalColumn extends CGridColumn {

private $_total = 0;

public function renderDataCellContent($row, $data) { // $row number is ignored

    $this->_total += $data->quantity;

    echo $this->_total;
}
}

$this->widget('zii.widgets.grid.CGridView', array(
'dataProvider' => $dp,   // provided by the controller
'columns' => array(
    'id',
    'name',
    'quantity',
    array(
        'header' => 'Total',
        'class'  => 'TotalColumn'
    )
)));

Here is my code, but nothing in my custom column is displayed: 这是我的代码,但自定义列中未显示任何内容:

Yii::import('zii.widgets.grid.CGridColumn');
    class TotalSkills extends CGridColumn
    {
        private $test = "blah";

        public function renderSkills($row, $data)
        {
            echo $this->test;
        }

    }




// People
echo CHtml::label('People', 'yw0', $shared_html_options);
$dataProvider = new CActiveDataProvider('Person');
$this->widget('zii.widgets.grid.CGridView', array(
    'dataProvider'=>$dataProvider,
    'columns'=>array(
                    'name',
                    'age',
                    array(
                    'header'=>'Total Skills',
                    'class'=>'TotalSkills'
                    ) 
                )
));

You should create the TotalColumn class inside your protected/components directory as a TotalColumn.php file. 您应该在protected / components目录中将TotalColumn类创建为TotalColumn.php文件。 That way you can use it in many different view files, instead of the view file that is defined only. 这样,您可以在许多不同的视图文件中使用它,而不是仅使用定义的视图文件。 Yii will load it automatically then. Yii会自动加载它。

$dp should be a typical DataProvider class (more likely a CActiveDataProvider ) that is defined in your controller and passed to your view. $dp应该是在控制器中定义并传递到视图的典型DataProvider类(很有可能是CActiveDataProvider )。 The DataProvider can be as easy as the CGridView documentation describes it. DataProvider可以像CGridView文档中描述的那样简单。

public function renderDataCellContent($row, $data) 公共函数renderDataCellContent($ row,$ data)

is defined method in gridview but there is no method such as 是在gridview中定义的方法,但是没有诸如

public function renderSkills($row, $data) 公共函数renderSkills($ row,$ data)

in gridview 在GridView中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM