代码之家  ›  专栏  ›  技术社区  ›  user759235

Listview小部件yii2,如何更改索引值

  •  1
  • user759235  · 技术社区  · 7 年前

    由于在使用listview小部件时可以在视图中使用索引值,因此我希望在不同位置使用相同的视图时更改此索引值,因此并非我使用的每个元素在页面上都具有相同的索引。

    somepage.php文件

    echo ListView::widget([
        'summary' => false,
        'viewParams' => [
            'url' => '',
        ],
    
        'index' => 'index + 4',//the idea
    
        'options' => [
                'class' => 'xxxx',
        ],
        'itemOptions'=> [
            'class' => 'xxxx',
        ],
        'itemView' => '@frontend/views/product/_item_product_small',
        'dataProvider' => $provider,
    ]);
    

    前端/views/product/_item_product_small.php

     <div data-index="<?=$index?>">
      // content
     </div>
    
    1 回复  |  直到 7 年前
        1
  •  0
  •   rob006    7 年前

    您需要为此类情况创建中间视图:

    somepage.php

    echo ListView::widget([
        // ...
        'itemView' => '_view',
    ]);
    

    _view.php

    $this->render('@frontend/views/product/_item_product_small', ['index' => $index + 4]);