在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. Python语言中对于json数据的编解码——Usage of json a Python standard library

    一.概述 1.1 关于JSON数据格式 JSON (JavaScript Object Notation), specified by RFC 7159 (which obsoletes RFC 46 ...

  2. 使用Python中PIL图形库进行截屏

    目的:通过使用Python的一个图形库PIL(Python Image Library)对屏幕进行截图 步骤: 1.下载PIL(路径)并安装 2.新建文件“截屏.py”,右键Edit with IDL ...

  3. iOS开发查看手机app本地存储的文件

    开发过程中,有时会在本地存储一些文件,但是我们不确定有没有存上,可以通过以下方法来查看测试手机上本地存储的文件: 1.选择xcode上面的window下面的Devices 2.先在左边选中你当前的设备 ...

  4. Maven打包 报 Unable to locate the Javac Compiler in: C:\Program Files\Java\jre1.8.0_73\..\lib\tools.jar

    无法找到javac 编译环境 右键项目 --> properties -->Java Build Path -->选中JRE 点击右侧 Edit 编辑 --> 把你设置的JRE ...

  5. checkbox选中状态不被改变

    让它的状态只能看不能改变,加上onclick="return false;". 也可以disabled="true";但是这个颜色变淡了; <input ...

  6. IntelliJ添加Emacs编辑器

    Intellij只支持emacs as a external tool: https://www.jetbrains.com/help/idea/2016.2/tutorial-using-emacs ...

  7. Ring0隐藏进程的方法

    第一种在系统调用服务表HOOK ZwQuerySystemInformation函数地址 使用InterlockedExchange函数将ZwQuerySystemInformation在内核导出表K ...

  8. NLP常用工具

    1.统计类工具:可参见[统计学习常用Python扩展包] 2.linux自带工具:可参见[[整理]Linux常用文本处理命令] 3.简繁转换工具:opencc Open Chinese Convert ...

  9. 学习笔记:java并发编程学习之初识Concurrent

    一.初识Concurrent 第一次看见concurrent的使用是在同事写的一个抽取系统代码里,当时这部分代码没有完成,有许多的问题,另一个同事接手了这部分代码的功能开发,由于他没有多线程开发的经验 ...

  10. Microsoft开源跨平台的序列化库——Bond

    上个月Microsoft开源了Bond,一个跨平台的模式化数据处理框架.Bond支持跨语言的序列化/反序列化,支持强大的泛型机制能够对数据进行有效地处理.该框架在Microsoft公司内部的高扩展服务 ...