简体   繁体   中英

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? Do I have to have the class TotalColumn be somewhere in $dp? Any idea how I would declare that 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. 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.

$dp should be a typical DataProvider class (more likely a CActiveDataProvider ) that is defined in your controller and passed to your view. The DataProvider can be as easy as the CGridView documentation describes it.

public function renderDataCellContent($row, $data)

is defined method in gridview but there is no method such as

public function renderSkills($row, $data)

in gridview

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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