yii 中CListView 的换行问题

ok,我们知道Yii 中的CGridview 很好用,但是有时候不能满足我们的高制定性,这个时候更加灵活的CListView 就可以帮助我们解决这些问题。

CListView 本身处理方式为,在一个总的页面中写入你需要的数据,之后在一个模板页中来制定你每一条数据怎么处理

比如这里

(案例中CListView是TbListView的super class)

<?php
$this->breadcrumbs=array(
    'Areas',
);
 
$this->menu=array(
    array('label'=>'Create Area','url'=>array('create')),
    array('label'=>'Manage Area','url'=>array('admin')),
);
?>
 
 
 
<h1>Areas</h1>
 
<?php $this->widget('bootstrap.widgets.TbListView',array(
    'dataProvider'=>$dataProvider,
    'itemView'=>'_view',
)); ?>

之后模板页上面代码说是   “_view”    也就是说是在相应文件夹下的_view

<div class="view">

    <b><?php echo CHtml::encode($data->getAttributeLabel('id')); ?>:</b>
    <?php echo CHtml::link(CHtml::encode($data->id),array('view','id'=>$data->id)); ?>
    <br />

    <b><?php echo CHtml::encode($data->getAttributeLabel('name')); ?>:</b>
    <?php echo CHtml::encode($data->name); ?>
    <br />

</div>

值得注意的是   比如说我需要每输出三次item后加一个<hr/>分开的话,我们就需要知道每个item是第几个输出的,

查API,看到Item 的属性

itemView property

public string $itemView;

the view used for rendering each data item. This property value will be passed as the first parameter to either CController::renderPartial or CWidget::render to render each data item. In the corresponding view template, the following variables can be used in addition to those declared in viewData:

  • $this: refers to the owner of this list view widget. For example, if the widget is in the view of a controller, then $this refers to the controller.
  • $data: refers to the data item currently being rendered.
  • $index: refers to the zero-based index of the data item currently being rendered.
  • $widget: refers to this list view widget instance.

发现$index 可以知道,所以,只需要在_index 中加入对index 的判断即可

<?php

if ($index%3===0){

        echo “<hr/>”;

}

?>

 
原文地址:https://www.cnblogs.com/jicheng1014/p/2759982.html