在vendor\yiisoft\yii2\widgets路径下新增文件ListViewtest,将下列代码粘贴并保存
<?php
namespace yii\widgets;
use Yii;
use yii\helpers\ArrayHelper;
use yii\helpers\Html;
class ListViewtest extends ListView
{
/**
* Renders a single data model.
* @param mixed $model the data model to be rendered
* @param mixed $key the key value associated with the data model
* @param integer $index the zero-based index of the data model in the model array returned by dataProvider.
* @return string the rendering result
*/
public $options = [];//列表外侧的div,可有属性class,id 等
public $num;//计数器
public $itemView_top;//前面几个模板
public $real_itemView;//后面的后续模板
public $num_top;//使用前模板的个数
public $start_num;//起始数值
public $div_allowed;//是否允许用div框住
public $extra;
public $front_moban;
//构析函数,注销变量
public function __destruct() {
global $options;
global $num;
global $itemView_top;
global $real_itemView;
global $num_top;
global $start_num;
global $front_tag;
$options = [];
$num = 0;
$itemView_top = null;
$real_itemView = null;
$num_top = null;
$start_num = null;
$front_tag =null;
}
public function run()
{
if ($this->showOnEmpty || $this->dataProvider->getCount() > 0) {
$content = preg_replace_callback("/{\\w+}/", function ($matches) {
$content = $this->renderSection($matches[0]);
return $content === false ? $matches[0] : $content;
}, $this->layout);
} else {
$content = $this->renderEmpty();
}
if ($this->div_allowed === true) {
$options = $this->options;
$tag = ArrayHelper::remove($options, 'tag', 'div');
echo Html::tag($tag, $content, $options);
}else{
echo $content;
}
}
public function renderItems()
{
global $options;
global $num;
global $itemView_top;
global $real_itemView;
global $num_top;
//给初始计数赋值
$this->start_num = isset($this->start_num)?$this->start_num:0;
$num = $this->start_num;
//若只用一套模板,则上下两套模板一致
if (!isset($this->itemView_top)) {
$this->itemView_top = $this->itemView;
}
$back_tag = $this->front_moban['flag']===true?$this->front_moban['tag_back']:null;
$models = $this->dataProvider->getModels();
$keys = $this->dataProvider->getKeys();
$rows = [];
foreach (array_values($models) as $index => $model) {
$rows[] = $this->renderItem($model, $keys[$index], $index);
}
return implode($this->separator, $rows).$back_tag;
}
public function renderItem($model, $key, $index)
{
global $num;//计数
//global $itemView_top;//储存模板
global $num_top;
global $real_itemView;
global $front_tag;
if ($num==$this->start_num) {
$real_itemView = $this->itemView;//暂存模板
$this->itemView = $this->itemView_top;//赋予新模板
$front_tag = '';
}elseif($num == ($this->start_num+$this->num_top)){
$this->itemView = $real_itemView;//调用原模板
$front_tag = $this->front_moban['flag'] === true?$this->front_moban['tag_front']:'';
}elseif($num>($this->start_num+$this->num_top)){
$this->itemView = $real_itemView;//调用原模板
$front_tag = '';
}
if ($this->itemView === null) {
$content = $key;
} elseif (is_string($this->itemView)) {
$content = $this->getView()->render($this->itemView, array_merge([
'model' => $model,
'key' => $key,
'index' => $index,
'widget' => $this,
], $this->viewParams));
} else {
$content = call_user_func($this->itemView, $model, $key, $index, $this);
}
$options = $this->itemOptions;
$tag = ArrayHelper::remove($options, 'tag', 'li');
$num++;
if ($this->extra['flag'] === true) {
if (($num-$this->start_num)%$this->extra['num'] == 0) {
return $front_tag.str_replace('{num}', ($num<10?'0'.$num:$num), $content).$this->extra['tag'];
}else{
return $front_tag.str_replace('{num}', ($num<10?'0'.$num:$num), $content);
}
}else{
return $front_tag.str_replace('{num}', ($num<10?'0'.$num:$num), $content);
}
// }
}
}
在视图模板中调用这个ListViewtest
<?php
use yii\widgets\ListViewtest;
?>
<?=ListViewtest::widget([
'layout' => "{items}\n{pager}",
'dataProvider' => $dataProvider,
'itemView' => '_test',//子视图
'itemView_top' => '_test1',//top模板
'num_top' => 1,//使用top模板的数目
'start_num' => 10,//从第几开始计数?默认0
//调用原模板(itemView模板,非itemView_top模板)时前后包夹标签
'front_moban' => [
'flag' => true, //开关,默认关
'tag_front' => '<front>', //前标签
'tag_back' => '</front>', //后标签
],
//每打印多少条数据的末尾,出现一次html标签
'extra' => [
'flag' => false, //开关,默认关
'tag' => '<hr>', //该标签
'num' => 5, //条数
]
//是否允许用div框住,默认否
'div_allowed' => true,
'options' => [ //该div的属性
'class' => null,
'id' => 'eee',
]
]);?>