|
|
1
Muhammad Omer Aslam
7 年前
您可以使用
itemview
选项来查看
listview
widget,其中您可以提供
-
指定为回调
函数($model,$key,$index,$widget)
并在内部添加自定义HTML,然后执行自定义操作,如检查每个第三个项,或使用实际的
id
by calling
$model->id
并将其附加到html标记属性中,它将为您提供
-
>$model.
:mixed,the data model
-
$key
:mixed,the key value associated with the data item
-
$index
:integer,the zero based index of the data item in the items array returned by$dataprovider.
-
-
$widget
:listview,this widget instance
例如
echo listview::widget([
'id'=>'listoitems',
'dataprovider'=>$dataprovider,
'itemView'=>函数($model,$key,$index,$widget){
//使用要绘制的HTML进行操作
//返回'<DIV></DIV>';
}
)
-
或者提供选项的视图文件路径,您仍然可以在视图文件中使用上述给定的参数。
例如
echo listview::widget([
'id'=>'listoitems',
'dataprovider'=>$dataprovider,
'itemview'=>“视图名称”
)
您的视图可以看起来像
<?PHP
使用yii\helpers\html;
?gt;
<div class=“card”>
<div class=“header”>
<h3 class=“title”><?=html::编码($model->标题)?></h3>
</DIV>
<DIV class=“body”><img src=“<?”=html::编码($model->名称)?>“><?=html::编码($model->id)?></DIV>
<DIV class=“footer”></DIV>
</DIV>
更新
如果您的要求是在每个项目或任何数量的项目之后绘制或添加一个元素,您可以使用
afteritem
>a>option which takes an anonymous function that is called once
after
rendering each data模型,它传递的参数集与
beforeitem
-
$model
:当前正在呈现的数据模型
-
$key
:与当前数据模型关联的键值
-
$index
:由$dataprovider返回的模型数组中数据模型的基于零的索引
-
$widget
:ListView对象
update2
下面的内容适用于给定的HTML
<div id=“ListOfitems”>
&?PHP
echo listview::小部件([
'id'=>'listoitems',
'dataprovider'=>$dataprovider,
'AfterItem='>函数($model,$key,$index,$widget){
//需要的解决方案
如果($index==12$index==12){
返回'some div here';
}
}
'itemView'=>函数($model,$key,$index,$widget){
//使用要绘制的HTML进行操作
返回'<div class=“list_item_wrapper”>
//单独文件中的项目
<div class=“list_item_wrapper”>
//使用$index检查某个数字时,将在此处生成代码。
</DIV>
</DIV>
;
}
)
?gt;
</DIV>
回拨function ($model , $key , $index , $widget) {在内部添加自定义HTML,并执行自定义操作,如检查每三个项目或使用id通过呼叫$model->id在它后面附加HTML标记属性,它提供了
$model:混合,数据模型
$key:mixed,与数据项关联的键值
$index:integer,$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
美元模型:正在呈现的当前数据模型
$KEY:与当前数据模型关联的键值
美元指数:由$dataprovider返回的模型数组中数据模型的从零开始的索引
$小部件: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>
|