在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. ajax教程

    本文来自w3school 简介: AJAX = Asynchronous JavaScript and XML 异步的javascript和xml ajax不是新的编程语言,而是一种使用现有标准的新方 ...

  2. 最小生成树 prime poj1258

    题意:给你一个矩阵M[i][j]表示i到j的距离 求最小生成树 思路:裸最小生成树 prime就可以了 最小生成树专题 AC代码: #include "iostream" #inc ...

  3. nginx 各类网站设置 (laravel , thinkphp , nodejs , https)

    基础部分设置 [root@centos ~]# vim /opt/nginx/conf/nginx.conf user www www;worker_processes auto;pid logs/n ...

  4. Insert or Merge && Insertion or Heap Sort

    原题连接:https://pta.patest.cn/pta/test/1342/exam/4/question/27102 题目如下: According to Wikipedia: Inserti ...

  5. http长轮询&短轮询

    http 协议介绍: http 协议是请求/响应范式的, 每一个 http 响应都是由一个对应的 http 请求产生的; http 协议是无状态的, 多个 http 请求之间是没有关系的. http ...

  6. 打造程序员的高效生产力工具-mac篇

    打造程序员的高效生产力工具-mac篇 1   概述 古语有云:“工欲善其事,必先利其器” [1] ,作为一个程序员,他最重要的生产资源是脑力知识,最重要的生产工具是什么?电脑. 在进行重要的脑力成果输 ...

  7. Android学习第一天-adb常用命令

    平时开发android应用 的时候,我们都会用到包含在Android SDK中一系列的工具,或许我们通过Eclipse去调用,又或许,我们自己通过打开终端进行手动输入并且执行,下面我们来一起学习下这些 ...

  8. web项目ajax技术一些总结

    WEB项目中,最主要的就是前后端间的联络.有时需要不进行页面跳转的前提下请求后端方法(action),就需要用到ajax. 在这个博客中,我用到的都是原生的js的ajax,不是很喜欢用jquery的a ...

  9. iOS 代码规范

    1 目的 统一规范XCode编辑环境下Objective-C.swift的编码风格和标准 2 适用范围 适用于所有用Objective-C,swift语言开发的项目. 3 编码规范 3.1 文件 项目 ...

  10. Java针对数据库增删改查代码

    package com.bank.abc; import java.beans.PropertyVetoException; import java.sql.Connection; import ja ...