Yii2 renderAjax嵌套Listview

时间:2015-07-18 13:48:27

标签: php ajax listview yii2 yii2-advanced-app

我正在尝试通过ajax呈现listview,但它给了我以下错误:

Exception (Invalid Configuration) 'yii\base\InvalidConfigException' with message &#039
;The "dataProvider" property must be set.

控制器:

public function actionLoadListviewAjax()
{
     $dataProvider = // call to a function which returns ArrayDataProvider
     return $this->renderAjax('myview', [ 'dataProvider' => $dataProvider ]);
}

查看:

echo ListView::widget([
        'dataProvider' => $dataProvider,
        'itemView'     => 'items',
        'options' => ['class' => 'list-view-post'],
        'summary' => '',
        'emptyText' => '', 
    ]);

$ dataProvder:

<pre>yii\data\ArrayDataProvider Object
(
    [key] => 
    [allModels] => Array
        (
            [0] => Array
                (
                    [RecommendationCategory] => 
                    [ID] => 37
                    [GUID] => 
                    [Title] => test
                    [WallPostTypeID] => 1
                    [RecommendationCategoryID] => 0
                    [CommentsJSON] => 
                    [TotalComments] => 
                    [PostedMessage] => test
                    [FirstName] => test
                    [LastName] => test
                    [ProfileImagePath] => Lighthouse.jpg
                    [AddedOn] => 2015-07-18 15:14:06
                    [ImagePath] => 
                    [CommentProfileImagePath] => 
                    [IsSubscribe] => 1
                )

        )

    [id] => 
    [_sort:yii\data\BaseDataProvider:private] => 
    [_pagination:yii\data\BaseDataProvider:private] => 
    [_keys:yii\data\BaseDataProvider:private] => 
    [_models:yii\data\BaseDataProvider:private] => 
    [_totalCount:yii\data\BaseDataProvider:private] => 
    [_events:yii\base\Component:private] => Array
        (
        )

    [_behaviors:yii\base\Component:private] => 
)

我也尝试了renderPartial,但仍然是同样的错误。有什么想法给出这个例子吗?

更新

在我的itemView文件items中,我有另一个列表视图,它给出了异常。

1 个答案:

答案 0 :(得分:1)

实际上列表视图项不必访问已通过窗口小部件发送的外部变量。 但是,如果您必须在内部视图文件中使用$dataProvider变量,请尝试使用callable在列表视图中显示项目。

类似的东西:

```

echo ListView::widget([
    'dataProvider' => $dataProvider,
    'itemView'     => function ($model, $key, $index, $widget) {
        return $this->renderAjax('items', [
            'dataProvider' => $widget->dataProvider,
        ]);
    },
    'options' => ['class' => 'list-view-post'],
    'summary' => '',
    'emptyText' => '', 
]);

```

此处有更多信息

http://www.yiiframework.com/doc-2.0/yii-widgets-listview.html# $ ItemView控件细节