Yii2自定义DetailView的显示内容

假设model中有name,images(图片链接:用;分开)

1.在DetailView可以直接这样输出:

1
2
3
4
5
6
7
<?= DetailView::widget([
'model' => $model,
'attributes' => [
'name',
'images',
],
]) ?>

2.可以通过下述方式进行字符串操作(分割、拼接、组装html标签等):

1
2
3
4
5
6
7
8
9
10
11
<?= DetailView::widget([
'model' => $model,
'attributes' => [
'name',
[
'attribute' => 'images',
'format' => 'raw',
'value' => count(explode(";", $model->images))
],
],
]) ?>

3.自定义函数,处理数组(比如把images中的所有图片显示出来)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<?php
function showImages($array){
$result='';
foreach($array as $item){
$result=$result.'<img src="'.$item.'" width="80" height="80">';
}
return $result;
}
?>


<?= DetailView::widget([
'model' => $model,
'attributes' => [
'name',
[
'attribute' => 'images',
'format' => 'raw',
'value' => showImages(explode(";", $model->images))
],
],
]) ?>

八宝粥 wechat
欢迎您扫一扫上面的微信公众号,订阅我的博客!