簡體   English   中英

在yii2 listview小部件中的第三個列表項之后添加塊

[英]Add block after 3rd list item in a yii2 listview widget

我之前沒有使用過listview窗口小部件,因此我找不到解決方案,因為我輸出了一個列表,其中包含我想做的兩件事:1.自動遞增和每個列表項的唯一ID /數字,2.添加一個每個第三個列表項之后的代碼塊(自定義代碼)。

我找不到與此有關的任何文檔,因此不確定是否可行。

echo ListView::widget([
    'id' => 'listofitems',
    'dataProvider' => $dataProvider
]);


<div id="listofitems">
   <div class="list_item_wrapper">

       // my items which are in a seperate file
       <div class="list_item_wrapper">
       // when using the $index to check for a certain number the code will be build here.
       </div>

   </div>

   // the needed solution
   if($index == 12 || $index == 12){
      echo 'some div here';
   }
</div>

您可以為ListView小部件使用itemView選項,您可以在其中提供

  • 指定為回調function ($model , $key , $index , $widget) {並在其中添加自定義HTML並執行自定義操作,例如檢查每三個項目,或通過調用$model->id並附加實際id它帶有html tag屬性,它為您提供

    • $model :混合數據模型
    • $key :混合的,與數據項關聯的鍵值
    • $index :整數,$ dataProvider返回的items數組中數據項的從零開始的索引。
    • $widget :ListView,此小部件實例

    例如

     echo ListView::widget([ 'id' => 'listofitems', 'dataProvider' => $dataProvider, 'itemView'=>function ($model , $key , $index , $widget) { //Do your Thing with Html you want to draw //return '<div></div>'; } ]); 
  • 或提供選項的視圖文件路徑,您仍然可以在視圖文件中使用上述給定的參數

    例如

      echo ListView::widget([ 'id' => 'listofitems', 'dataProvider' => $dataProvider, 'itemView'=>'_view-name' ]); 

    您的視圖可能看起來像

     <?php use yii\\helpers\\Html; ?> <div class="card"> <div class="header"> <h3 class="title"><?= Html::encode ( $model->title ) ?></h3> </div> <div class="body"><img src="<?= Html::encode ( $model->name ) ?>"><?= Html::encode ( $model->id ) ?></div> <div class="footer"></div> </div> 

更新

如果您的要求是在每個項目或任何數量的項目之后繪制或添加元素,則可以使用afterItem選項,該選項采用匿名函數,該函數在呈現每個數據模型AFTER即被調用,它傳遞的參數集與beforeItem相同

  • $model :當前正在渲染的數據模型
  • $key :與當前數據模型關聯的鍵值
  • $index :$ dataProvider返回的模型數組中數據模型的從零開始的索引
  • $widget :ListView對象

UPDATE2

以下內容適用於給定的HTML

<div id="listofitems">
    <?php
    echo ListView::widget ( [
        'id' => 'listofitems' ,
        'dataProvider' => $dataProvider ,
        'afterItem=' > function($model , $key , $index , $widget) {
            // the needed solution
            if ( $index == 12 || $index == 12 ) {
                return 'some div here';
            }
        } ,
        'itemView' => function ($model , $key , $index , $widget) {
            //Do your Thing with Html you want to draw
            return '<div class="list_item_wrapper">

                // my items which are in a seperate file
                <div class="list_item_wrapper">
                // when using the $index to check for a certain number the code will be build here.
                </div>

            </div>
         ';
        }
    ] );
    ?>

</div>

默認情況下,小部件中有一個這樣的索引。 配置ListView小部件的'itemView'屬性,這意味着您可以使用將為每個項目呈現的自定義視圖。 像這樣:

<?= ListView::widget([
    'id' => 'listofitems',
    'dataProvider' => $dataProvider,
    'itemView' => '/site/item',
?>

在您的視圖文件/site/item.php ,您可以訪問當前項目的索引:

<?php 
    var_dump($index);
?>

有關itemView屬性的更多信息,請itemView此處。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM