在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',
  ]

]);?>

改写yii2的listview功能的更多相关文章

  1. Yii2之ListView小部件

    ListView是yii框架中类似GridView,也是用于展示多条数据的小部件,相比GridView,ListView可以更加灵活地设置数据展示的格式. 下面以我自己做的一个使用ListView来展 ...

  2. android listView功能简介

    本文参考连接:http://blog.csdn.net/kesenhoo/article/details/7196920 android中listView是非常常用的组建,下边就经常用到的功能做一下简 ...

  3. android——ListView功能的实现

    1.main.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:a ...

  4. 简单分析下用yii2的yii\helpers\Html类和yii.js实现的post请求

    yii2提供了很多帮助类,比如Html.Url.Json等,可以很方便的实现一些功能,下面简单说下这个Html.用yii2写view时时经常会用到它,今天在改写一个页面时又用到了它.它比较好用的地方就 ...

  5. Yii2.0登录详解(下)

    在上一篇博文中,笔者讲述了yii2应用用户登陆的基本方法,但是这些方法到底是怎样实现登陆的呢?底层的原理到底是什么?在这篇博文笔者将从Yii的源码角度分析登陆的基本原理以及cookie自动登陆的原理, ...

  6. ListView中响应item的点击事件并且刷新界面

    ---恢复内容开始--- 最近在在实现listview功能中遇到了这个问题: 点击事件写在了adapter的item中,不知道如何在listview的点击事件中更新数据的显示: 总结:1.要使用not ...

  7. listView 分页加载数据

    Android应用 开发中,采用ListView组件来展示数据是很常用的功能,当一个应用要展现很多的数据时,一般情况下都不会把所有的数据一次就展示出来,而是通过分页 的形式来展示数据,个人觉得这样会有 ...

  8. 基于Jquery UI的autocompelet改写,自动补全控件,增加下拉选项,动态设置样式,点击显示所有选项,并兼容ie6+

    Jquery UI的autocompelete改写 注意:实现功能,除了原版的自动补全内容外,增加一个点击显示所有选项,样式能动态设置. 加载数据的来源为后台数据库读取. 具体代码如下: 引用 从官方 ...

  9. 面向对象的一小步:添加ActiveRecord的Scope功能

    问题场景 我们用Yii2的ActiveRecord功能非常的方便,假如我们有个Model叫Student,那么ActiveQuery可以通过这种方式轻便地获得: $query = Student::f ...

随机推荐

  1. centos7开机自动联网设置

    /etc/sysconfig/network-scripts/目录下ifcfg-eth0这个 文件,把ONBOOT="no"改为yes

  2. 深入浅出 妙用Javascript中apply、call、bind

    apply.call 在 javascript 中,call 和 apply 都是为了改变某个函数运行时的上下文(context)而存在的,换句话说,就是为了改变函数体内部 this 的指向. Jav ...

  3. CI框架,双层弹出框的样式实现

    在弹出的主页面上,写一个隐藏的悬浮的div 通过标记使他显示,通过计数器使他关闭 部分代码: <div id="common_msg"></div>//主页 ...

  4. 测试函数用Return 返回值和用函数名返回值的区别

    '*************************************************************************'**模 块 名:工程1 - Form1'**说   ...

  5. GPRS 接入外网的过程

    请问GPRS模块与Internet上主机的连接.数据传输过程 虽然按照GPRS模块的说明文档能够通过内嵌TCP/IP实现数据的传输,但是对GPRS模块和主机之间的连接关系了解得不是很多.有谁可以介绍一 ...

  6. snmp学习笔记

    snmp5.5 client 包含头文件 #include <net-snmp/net-snmp-config.h> #include <net-snmp/net-snmp-incl ...

  7. actor concurrency

    The hardware we rely on is changing rapidly as ever-faster chips are replaced by ever-increasing num ...

  8. SSDB:高性能数据库服务器

    SSDB是一个开源的高性能数据库服务器, 使用Google LevelDB作为存储引擎, 支持T级别的数据, 同时支持类似Redis中的zset和hash等数据结构, 在同时需求高性能和大数据的条件下 ...

  9. Android 知识杂记(MVP模式)

    MVP的模式在于将原来activity中业务逻辑的部分剥离出来,代码示例如下: Account public class Account { private String mUsername; pri ...

  10. 使用GDB调试Go语言

    用Go语言已经有一段时间了,总结一下如何用GDB来调试它! ps:网上有很多文章都有描述,但是都不是很全面,这里将那些方法汇总一下 GDB简介  GDB是GNU开源组织发布的⼀一个强⼤大的UNIX下的 ...