编程语言
首页 > 编程语言> > php – Yii2 renderAjax嵌套Listview

php – Yii2 renderAjax嵌套Listview

作者:互联网

我试图通过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文件项中,我有另一个列表视图,它给出了异常.

解决方法:

实际上,列表视图项不必访问已通过窗口小部件发送的外部变量.
但是,如果必须在内部视图文件中使用$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控件细节

标签:php,ajax,listview,yii2,yii2-advanced-app
来源: https://codeday.me/bug/20190708/1399684.html