Pass viewParams in ListView widget

I would like to pass viewParams in ListView widget.

However, when I do so, I get an exception.

My code:




<?= ListView::widget([

    'dataProvider' => $dataProvider,

    'options' => [

        'tag' => 'div',

        'class' => 'all-items item-list row',

        'id' => 'all-items',

    ],


    'layout' => "<div class='col-xs-12'><p>{summary}</p></div> {items} <div class='col-xs-12'>{pager}</div>",

    'emptyText' => '<div class="col-xs-12">Nothing added yet.</div>',

    'itemView' => function ($order, $key, $index, $widget) {

        return $this->render('_badge', [

            'order' => $order

        ]);

    },

    'viewParams' => [

        'isClient' => $isClient

    ],


    'itemOptions' => [

        'tag' => false,

    ],

]) ?>

<?php Pjax::end(); ?></div>



$isClient is passed from the Controller, however it seems that it is not recognized by the view.

And I have this exception:

[spoiler]




PHP Notice – yii\base\ErrorException


Undefined variable: isClient

5. in /var/www/test/vendor/yiisoft/yii2/widgets/ListView.php at line 193 –

An Error occurred while handling another error:

exception 'ReflectionException' with message 'Method yii\base\View::{closure:/var/www/test/common/components/views/order-badge/index.php:28-32}() does not exist' in /var/www/test/vendor/yiisoft/yii2/web/ErrorHandler.php:195

Stack trace:

#0 /var/www/test/vendor/yiisoft/yii2/web/ErrorHandler.php(195): ReflectionMethod->__construct('yii\base\View', '{closure:/var/w...')

#1 /var/www/test/vendor/yiisoft/yii2/views/errorHandler/callStackItem.php(26): yii\web\ErrorHandler->addTypeLinks('yii\base\View::...')

#2 /var/www/test/vendor/yiisoft/yii2/web/ErrorHandler.php(245): require('/var/www/yii-we...')

#3 /var/www/test/vendor/yiisoft/yii2/web/ErrorHandler.php(304): yii\web\ErrorHandler->renderFile('@yii/views/erro...', Array)

#4 /var/www/test/vendor/yiisoft/yii2/web/ErrorHandler.php(326): yii\web\ErrorHandler->renderCallStackItem('/var/www/yii-we...', 193, 'yii\base\View', '{closure:/var/w...', Array, 5)

#5 /var/www/test/vendor/yiisoft/yii2/views/errorHandler/exception.php(381): yii\web\ErrorHandler->renderCallStack(Object(yii\base\ErrorException))

#6 /var/www/test/vendor/yiisoft/yii2/web/ErrorHandler.php(245): require('/var/www/yii-we...')

#7 /var/www/test/vendor/yiisoft/yii2/web/ErrorHandler.php(116): yii\web\ErrorHandler->renderFile('@yii/views/erro...', Array)

#8 /var/www/test/vendor/yiisoft/yii2/base/ErrorHandler.php(111): yii\web\ErrorHandler->renderException(Object(yii\base\ErrorException))

#9 [internal function]: yii\base\ErrorHandler->handleException(Object(yii\base\ErrorException))

#10 {main}

Previous exception:

exception 'yii\base\ErrorException' with message 'Undefined variable: isClient' in /var/www/test/common/components/views/order-badge/_badge.php:23

Stack trace:

#0 /var/www/test/vendor/yiisoft/yii2/base/View.php(250): yii\base\View->renderPhpFile()

#1 /var/www/test/vendor/yiisoft/yii2/base/View.php(152): yii\base\View->renderFile()

#2 /var/www/test/common/components/views/order-badge/index.php(31): yii\base\View->render()

#3 /var/www/test/vendor/yiisoft/yii2/widgets/ListView.php(193): yii\base\View->{closure:/var/www/test/common/components/views/order-badge/index.php:28-32}()

#4 /var/www/test/vendor/yiisoft/yii2/widgets/ListView.php(193): ::call_user_func()

#5 /var/www/test/vendor/yiisoft/yii2/widgets/ListView.php(124): yii\widgets\ListView->renderItem()

#6 /var/www/test/vendor/yiisoft/yii2/widgets/BaseListView.php(159): yii\widgets\ListView->renderItems()

#7 /var/www/test/vendor/yiisoft/yii2/widgets/BaseListView.php(134): yii\widgets\BaseListView->renderSection()

#8 /var/www/test/vendor/yiisoft/yii2/widgets/BaseListView.php(137): yii\widgets\BaseListView->yii\widgets\{closure}()

#9 /var/www/test/vendor/yiisoft/yii2/widgets/BaseListView.php(137): ::preg_replace_callback()

#10 /var/www/test/vendor/yiisoft/yii2/base/Widget.php(139): yii\widgets\BaseListView->run()

#11 /var/www/test/common/components/views/order-badge/index.php(37): yii\base\Widget::widget()

#12 /var/www/test/vendor/yiisoft/yii2/base/View.php(330): ::unknown()

#13 /var/www/test/vendor/yiisoft/yii2/base/View.php(250): yii\base\View->renderPhpFile()

#14 /var/www/test/vendor/yiisoft/yii2/base/View.php(152): yii\base\View->renderFile()

#15 /var/www/test/vendor/yiisoft/yii2/base/Widget.php(234): yii\base\View->render()

#16 /var/www/test/common/components/OrderBadgeWidget.php(29): yii\base\Widget->render()

#17 /var/www/test/vendor/yiisoft/yii2/base/Widget.php(139): common\components\OrderBadgeWidget->run()

#18 /var/www/test/frontend/views/history/index.php(18): yii\base\Widget::widget()

#19 /var/www/test/vendor/yiisoft/yii2/base/View.php(330): ::unknown()

#20 /var/www/test/vendor/yiisoft/yii2/base/View.php(250): yii\base\View->renderPhpFile()

#21 /var/www/test/vendor/yiisoft/yii2/base/View.php(152): yii\base\View->renderFile()

#22 /var/www/test/vendor/yiisoft/yii2/base/Controller.php(381): yii\base\View->render()

#23 /var/www/test/frontend/controllers/HistoryController.php(54): yii\base\Controller->render()

#24 /var/www/test/vendor/yiisoft/yii2/base/InlineAction.php(57): frontend\controllers\HistoryController->actionIndex()

#25 /var/www/test/vendor/yiisoft/yii2/base/InlineAction.php(57): ::call_user_func_array()

#26 /var/www/test/vendor/yiisoft/yii2/base/Controller.php(156): yii\base\InlineAction->runWithParams()

#27 /var/www/test/vendor/yiisoft/yii2/base/Module.php(523): yii\base\Controller->runAction()

#28 /var/www/test/vendor/yiisoft/yii2/web/Application.php(102): yii\base\Module->runAction()

#29 /var/www/test/vendor/yiisoft/yii2/base/Application.php(380): yii\web\Application->handleRequest()

#30 /var/www/test/frontend/web/index.php(17): yii\base\Application->run()

#31 {main}



[/spoiler]

According to the docs, I am doing everything correctly.

What could be the cause of the problem?

Hi,

Are you sure? you are passing the variable


 $isClient 

from your controller to your view file?

I suspect that may be the issue!

Yes, because I’m able to var_dump() isClient variable before I pass it to the _badge view, however I cannot pass it to the next view by viewParams.

I also tried to pass some random value of isClient to _badge and it is still showing me the same error message.




PHP Notice – yii\base\ErrorException


Undefined variable: isClient


...



Even if I do it like this:




...

    'itemView' => function ($order, $key, $index, $widget) {

        return $this->render('_badge', [

            'order' => $order

        ]);

    },


    'viewParams' => [

        'isClient' => 'test'

    ],


    'itemOptions' => [

        'tag' => false,

    ],

...



The API says $itemView must be a view name when you want to use $viewParams.

http://www.yiiframework.com/doc-2.0/yii-widgets-listview.html#$viewParams-detail

I can’t really understand what it means, could you give at least small example?

You can’t use an anonymous function for ‘itemView’ when you want to use ‘viewParams’.




    ...

    'itemView' => '_badge',


    'viewParams' => [

        'isClient' => 'test'

    ],

    ...



Okay, but what If I want to pass some information to the file _badge from dataProvider? For this case I use nonymous function, but If I remove it, than it is useless.

What should I do in this case?

What is $order? Is it the model that dataProvider returns for each item?

If so, then you should be able to access it in the item view with the name "model" without using annonymous function.

http://www.yiiframework.com/doc-2.0/yii-widgets-listview.html#$itemView-detail




        return $this->render('_badge', [

            'order' => $order

            'isClient' => 'test'

        ]);

    },



your use of ‘_badge’ sounds like a renderPartial (like _form.php in the CRUD stuff) If this is the case, then the controller as to pass isClient to the initial view, and that view would have to pass it along to the partial view.

Maybe this will point you in the right direction.