Filtering the data we have on our GridView by dates are sometimes very important. On this article I am going to show you how easy is to add a date range filter to your Yii2 GridViews.
The DateRange picker plugin
For this tutorial we are going to use jino5577/yii2-date-range-picker. One thing that I highly recommend to any developer is that, whenever he/she can, tests should be provided to increase the trust of other developers to your open source project. I know that sometimes that is practically impossible due to the small we have (I include my self here) but if we can, we should do it. Nevertheless, this is a widget, and for a widget, there is not much to do and this one have never gave me any issues so far. Enough talking, lets include the library in our composer:
1
2
./composer require jino5577/yii2-date-range-picker "*"
In case you are wondering why I am using ./composer command instead of php composer.phar is because I have composer installed globally.
The ModelSearch class
Now, lets imagine that your model User has a created_at attribute and we want to add a date range filtering on that value. In order to do that, we need first to add a public variable on its UserSearch class (the class used for filtering), an attribute that will hold the range values to filter our data with:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
class UserSearch extends User
{
// This attribute will hold the values to filter our database data
public $created_at_range; // ....
public function rules()
{
return ArrayHelper::merge(
[
[['created_at_range'], 'safe'] // add a rule to collect the values
],
parent::rules()
);
} public function search($params)
{
$query = $this->finder->getUserQuery();
$dataProvider = new ActiveDataProvider(
[
'query' => $query,
]);
if (!($this->load($params) && $this->validate())) {
return $dataProvider;
} // do we have values? if so, add a filter to our query
if(!empty($this->created_at_range) && strpos($this->created_at_range, '-') !== false) {
list($start_date, $end_date) = explode(' - ', $this->created_at_range);
$query->andFilterWhere(['between', 'user.created_at', strtotime($start_date), strtotime($end_date)]);
}
// ... more filters here ... return $dataProvider
}
}
That's all we have to do to be able to search by a range in our UserSearch class. Now, the next step is to configure the GridView column to add our date range picker.
Configuring GridView column
The last part is even easier than before, we simply configure the column of our GridView as follows:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
use jino5577\daterangepicker\DateRangePicker; // add widget
/* @var $searchModel common\models\UserSearch */
// ... lots of code here
<?= GridView::widget([
// ... more code here
'columns' => [
// ... other columns
[
// the attribute
'attribute' => 'created_at',
// format the value
'value' => function ($model) {
if (extension_loaded('intl')) {
return Yii::t('app', '{0, date, MMMM dd, YYYY HH:mm}', [$model->created_at]);
} else {
return date('Y-m-d G:i:s', $model->created_at);
}
},
// some styling?
'headerOptions' => [
'class' => 'col-md-2'
],
// here we render the widget
'filter' => DateRangePicker::widget([
'model' => $searchModel,
'attribute' => 'created_at_range',
'pluginOptions' => [
'format' => 'd-m-Y',
'autoUpdateInput' => false
]
])
],
]
]); ?>
Done, with three simple steps we have now a beautiful date range picker filtering our data by a range. - See more at: http://www.2amigos.us/blog/how-to-add-a-date-range-picker-to-filter-for-dates-on-a-gridview-for-yii2#sthash.pf77rP6O.dpuf

How to add a date range picker to filter for dates on a GridView for Yii2 - See more at: http://www.2amigos.us/blog/how-to-add-a-date-range-picker-to-filter-for-dates-on-a-gridview-for-yii2#sthash.pf7的更多相关文章

  1. JQuery包装集size,length,index,slice,find,filter,is,children,next,nextAll,parent,parents,closest,siblings,add,end,andSelf的用法

    在使用Jquery包装集的知识之前首先要注意三个概念(当前包装集.新包装集.包装集内部元素)的区别. <!DOCTYPE html> <html xmlns="http:/ ...

  2. Gridview转发

    首页 开源项目 问答 动弹 博客 翻译 资讯 专题 城市圈 [ 登录 | 注册 ] 博客专区 > Reya滴水心的博客详情 Asp.net中GridView使用详解(很全,很经典) Reya滴水 ...

  3. Asp.net中GridView使用详解(引)

    GridView无代码分页排序GridView选中,编辑,取消,删除GridView正反双向排序GridView和下拉菜单DropDownList结合GridView和CheckBox结合鼠标移到Gr ...

  4. 【转】 GridView 72般绝技

    说明:准备出一个系列,所谓精髓讲C#语言要点.这个系列没有先后顺序,不过尽量做到精.可能会不断增删整理,本系列最原始出处是csdn博客,谢谢关注. C#精髓 第四讲 GridView 72般绝技 作者 ...

  5. GridView的详细用法

    l GridView无代码分页排序 l GridView选中,编辑,取消,删除 l GridView正反双向排序 l GridView和下拉菜单DropDownList结合 l GridView和Ch ...

  6. GridView点击排序

    快速预览:GridView无代码分页排序GridView选中,编辑,取消,删除GridView正反双向排序GridView和下拉菜单DropDownList结合GridView和CheckBox结合鼠 ...

  7. 转:C#精髓 第四讲 GridView 72般绝技

    说明:准备出一个系列,所谓精髓讲C#语言要点.这个系列没有先后顺序,不过尽量做到精.可能会不断增删整理,本系列最原始出处是csdn博客,谢谢关注. C#精髓 第四讲 GridView 72般绝技 作者 ...

  8. Asp.net GridView 72般绝技

    快速预览:GridView无代码分页排序GridView选中,编辑,取消,删除GridView正反双向排序GridView和下拉菜单DropDownList结合GridView和CheckBox结合鼠 ...

  9. 关于开发C#中的asp.net中gridview控件的使用

    原文网址:http://blog.sina.com.cn/s/blog_67f1b4b201017663.html 1.GridView无代码分页排序: 效果图: 1.AllowSorting设为Tr ...

随机推荐

  1. 20181122_C#中AOP_使用Unity实现AOP

    一.   使用Unity的AOP实现 a)         整体项目截图: b) 添加Unity的Nuget包, 直接使用最新版就行, 需要添加两个 Unity 和 Unity.Interceptio ...

  2. ListView下拉刷新,上拉自动加载更多

    下拉刷新,Android中非常普遍的功能.为了方便便重写的ListView来实现下拉刷新,同时添加了上拉自动加载更多的功能.设计最初是参考开源中国的Android客户端源码.先看示例图.       ...

  3. Masking operations

    Using a mask, multiple bits in a nibble, byte, words can be set either on, off or inverted from on t ...

  4. SuggestFrameWork js代码结构

    关于suggestFrameWork的使用教程网上很多,如果您仅仅想知道如何使用请移步.这里展现一下js代码实现结构 下载地址 http://sourceforge.net/projects/sugg ...

  5. Netty心跳简单Demo

    前面简单地了解了一下IdleStateHandler,我们现在写一个简单的心跳demo: 1)服务器端每隔5秒检测服务器端的读超时,如果5秒没有接受到客户端的写请求,也就说服务器端5秒没有收到读事件, ...

  6. 「小程序JAVA实战」小程序头像图片上传(下)(45)

    转自:https://idig8.com/2018/09/09/xiaochengxujavashizhanxiaochengxutouxiangtupianshangchuan44/ 接下来,我们应 ...

  7. Spring 中的 LocalSessionFactoryBean和LocalContainerEntityManagerFactoryBean

    Spring和Hibernate整合的时候我们经常会有如下的配置代码 1,非JPA支持的配置 <!-- 配置 Hibernate 的 SessionFactory 实例: 通过 Spring 提 ...

  8. Elasticsearch-PHP 处理JSON数组和对象

    PHP中处理JSON数组和对象 客户端有一些混淆的资源是围绕着JSON的数组和对象,以及如何在PHP中指定它们.特别是,问题是由空对象和空数组导致的.这篇文章回告诉你一些在Elasticsearch ...

  9. flask 常见关系模板代码

    以下罗列了使用关系型数据库中常见关系定义模板代码 一对多示例场景:用户与其发布的帖子(用户表与帖子表)角色与所属于该角色的用户(角色表与多用户表)示例代码class Role(db.Model): & ...

  10. MySQL数据库篇之pymysql模块的使用

    主要内容: 一.pymysql模块的使用 二.pymysq模块增删改查 1️⃣  pymsql模块的使用 1.前言:之前我们都是通过MySQL自带的命令行客户端工具mysql来操作数据库, 那如何在p ...