4

I need to set custom data-attribute to the ListView elements. As example I try to get current DataProvider model id. But I still view error "htmlspecialchars() expects parameter 1 to be string, object given". Please check my code and halp me - how I should get this ID?

 <?= ListView::widget([
            'dataProvider' => $photoProvider,
            'id' => 'photo-list',
            'itemView' => '_photoListItem',
            'viewParams' => [
                'fullView' => true,
            ],
            'options' => [
                'tag' => 'ul',
                'class' => 'list-view'
            ],
            'itemOptions' => [
                'tag' => 'li',
                'class' => 'item',
                'data' =>[
                    'test' => function ($model, $key, $index, $widget) {
                        return Html::encode($model->id);
                    }
                ]
            ],
            'pager' => [
                'class' => ScrollPager::className(),
                'container' => '#photo-list',
                'item' => '.item',
                'triggerText' => '<div class="btn more">Load More</div>',
                'noneLeftText' => '',
                'triggerOffset' => 2,
                'negativeMargin' => 200
            ],
            'layout' => "{items}\n{pager}",
        ]);
        ?>

1 Answer 1

2

This is not possible in the ListView, according to the doc:

'data' => [
  'test' => function ($model, $key, $index, $widget) {
    return Html::encode($model->id);
  }
]

You cannot use a function here. Only static values are allowed.

Alternatively, you can provide those attributes in tags of your _photoListItem view file.

1
  • @ДимаПолхов It starts working after trying it 3 days! ;-) No, I know, there are similar things that work this way. And IMO it would useful if these data attributes could be populated how you have tried it. You could make a feature request for this on github.
    – robsch
    Oct 7, 2015 at 12:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.