在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. 谈BFC和haslayout

    今天提到BFC和haslayout,就顺带在网上查查资料,总结一下它们. CSS2我们再熟悉不过,当然它里面我们需要掌握的,就是CSS2的选择器和布局,选择器总共31种.避开这个不说,我们说布局. 布 ...

  2. iOS 添加中文支持的操作

    1.选择工程菜单,这里要选中Project,而不是Targets   2.点击Info菜单, 下拉到最后,看到Localizations. 点击+号.   3.选择中文 chinese-simplif ...

  3. Python数据分析

    一.安装Anaconda 1.下载:https://www.continuum.io/downloads 2.命令行创建和启动环境 conda create --name py35 python=3. ...

  4. ZK 使用jfreeChart

    前台: <?page title="Grid使用" contentType="text/html;charset=UTF-8"?> <zk x ...

  5. Javascript初学篇章_7(DOM)

    DOM 文档对象模型DOM (document object model) 文档对象模型,它定义了操作文档对象的接口.DOM 把一份html文档表示为一棵家谱树,使用parent(父), child( ...

  6. final 评论ii

    按照演讲顺序 1.约跑app         约跑app,从界面的单调,到最后的final发布,实现界面的友好性,有了很大的提高.约跑app,如果在约定地点可以显示出,所在位置,以及约定地址.就可以达 ...

  7. Unity_UGUI知识点思维导图

    转自 http://blog.csdn.net/qq_34134078/article/details/51772568 个人总结的UGUI学习知识要点思维导图,四张部分图及最后一张整体图 1.UI基 ...

  8. 使用java反射机制编写Student类并保存

    定义Student类 package org; public class Student { private String _name = null; ; ; public Student() { } ...

  9. Hadoop学习笔记—15.HBase框架学习(基础知识篇)

    HBase是Apache Hadoop的数据库,能够对大型数据提供随机.实时的读写访问.HBase的目标是存储并处理大型的数据.HBase是一个开源的,分布式的,多版本的,面向列的存储模型,它存储的是 ...

  10. 《Entity Framework 6 Recipes》中文翻译系列 (29) ------ 第五章 加载实体和导航属性之过滤预先加载的实体集合和修改外键关联

    翻译的初衷以及为什么选择<Entity Framework 6 Recipes>来学习,请看本系列开篇 5-13  过滤预先加载的实体集合 问题 你想过滤预先加载的实体集合,另外,你想使用 ...