Clistview in yii Undefined variable dataProvider

2019-03-04 10:19发布

问题:

I need a list view in my index.php. This is does not have any model so I used CSqlDataProvider for declaring the data provider and rendering it to the index from the controller. Here is my controller action...

public function actionIndex()
{
        $sql="select * from Ads";
        $totalItemCount=20;
        $dataProvider = new CSqlDataProvider($sql, array(
                'totalItemCount' => $totalItemCount,
                                )); 

        $this->render('index',array('dataProvider'=>$dataProvider));
}

Here is my index.php code....

$this->widget('zii.widgets.CListView', array(
    'dataProvider'=>$dataProvider,
    'itemView'=>'index',
    'id'=>'list',
    ));

I am getting an error

"Undefined variable: dataProvider"

回答1:

Your itemView cannot be index. It has to be a partial view that will render the items in your list. $dataProvider is available when the list is being rendered but not when the items are being rendered hence the error. For more information http://www.yiiframework.com/doc/api/1.1/CListView#itemView-detail