改写yii2的listview功能
在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功能的更多相关文章
- Yii2之ListView小部件
ListView是yii框架中类似GridView,也是用于展示多条数据的小部件,相比GridView,ListView可以更加灵活地设置数据展示的格式. 下面以我自己做的一个使用ListView来展 ...
- android listView功能简介
本文参考连接:http://blog.csdn.net/kesenhoo/article/details/7196920 android中listView是非常常用的组建,下边就经常用到的功能做一下简 ...
- android——ListView功能的实现
1.main.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:a ...
- 简单分析下用yii2的yii\helpers\Html类和yii.js实现的post请求
yii2提供了很多帮助类,比如Html.Url.Json等,可以很方便的实现一些功能,下面简单说下这个Html.用yii2写view时时经常会用到它,今天在改写一个页面时又用到了它.它比较好用的地方就 ...
- Yii2.0登录详解(下)
在上一篇博文中,笔者讲述了yii2应用用户登陆的基本方法,但是这些方法到底是怎样实现登陆的呢?底层的原理到底是什么?在这篇博文笔者将从Yii的源码角度分析登陆的基本原理以及cookie自动登陆的原理, ...
- ListView中响应item的点击事件并且刷新界面
---恢复内容开始--- 最近在在实现listview功能中遇到了这个问题: 点击事件写在了adapter的item中,不知道如何在listview的点击事件中更新数据的显示: 总结:1.要使用not ...
- listView 分页加载数据
Android应用 开发中,采用ListView组件来展示数据是很常用的功能,当一个应用要展现很多的数据时,一般情况下都不会把所有的数据一次就展示出来,而是通过分页 的形式来展示数据,个人觉得这样会有 ...
- 基于Jquery UI的autocompelet改写,自动补全控件,增加下拉选项,动态设置样式,点击显示所有选项,并兼容ie6+
Jquery UI的autocompelete改写 注意:实现功能,除了原版的自动补全内容外,增加一个点击显示所有选项,样式能动态设置. 加载数据的来源为后台数据库读取. 具体代码如下: 引用 从官方 ...
- 面向对象的一小步:添加ActiveRecord的Scope功能
问题场景 我们用Yii2的ActiveRecord功能非常的方便,假如我们有个Model叫Student,那么ActiveQuery可以通过这种方式轻便地获得: $query = Student::f ...
随机推荐
- PHP中include()与require()的区别说明
require 的使用方法如 require("MyRequireFile.php"); .这个函数通常放在 PHP 程序的最前面,PHP 程序在执行前,就会先读入 require ...
- HDU 2202 计算几何
最大三角形 Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submi ...
- 八大排序算法Java
目录(?)[-] 概述 插入排序直接插入排序Straight Insertion Sort 插入排序希尔排序Shells Sort 选择排序简单选择排序Simple Selection Sort 选择 ...
- DoTween 应用设置
一.下载 官方下载地址:http://dotween.demigiant.com/download.php 二.安装 1.把下载到压缩包中的DOTween文件夹拷贝到项目文件中 2.安装DOTween ...
- Django实现注册
前言 对于web开来说,用户登陆.注册.文件上传等是最基础的功能,针对不同的web框架,相关的文章非常多,但搜索之后发现大多都不具有完整性,对于想学习web开发的新手来说不具有很强的操作性:对于web ...
- unity3D学习—坦克大战(一)
背景介绍 本人一名C#程序员,从事C#开发已经有四年有余了,目前在一家大型公司上班.鉴于公司的业务需要,现在需要学习unity3D游戏开发,好在unity支持C#脚本开发,无形中省下了许多重新学习新语 ...
- poj3122-Pie(二分法+贪心思想)
一,题意: 有f+1个人(包括自己),n块披萨pie,给你每块pie的半径,要你公平的把尽可能多的pie分给每一个人 而且每个人得到的pie来自一个pie,不能拼凑,多余的边角丢掉.二,思路: 1,输 ...
- mac远程桌面连接windows 8.1 update,提示: 远程桌面连接无法验证您希望连接的计算机的身份
在网上找到解决方案: SolutionEnable RDP security layer in Group Policy on the machine: Verify that the firewal ...
- 安装jdk
检查已安装jdk,如果有,先删除 rpm -qa|grep java rpm -e --nodeps filename 从oracle官方网站下载jdk安装包:jdk-8u111-linux-x64. ...
- 微软StockTrader应用程序
这是一个采用 .NET Enterprise Application Server 技术的端到端示例应用程序.应用程序代码可以从 这里 下载. 代码中演示了WCF服务和移动开发,包括用Xamarin ...