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. 3dmax卡通渲染插件pencil+渲染线框

    转自:http://www.cr173.com/soft/179512.html http://www.psoft.co.jp/jp/ 官网和YTB有 2代的视频教程,平均每个2分钟长,无解说,是日文 ...

  2. Go - 指针简介 与 ++/--运算符以及控制语句

    指针 Go 语言中,对于指针有一些特殊约束: 1. 不在支持 “->” 符号,所有的指针使用“.” 来操作指针对象的成员变量 2. 指针的默认值为 “nil” ++ 与 -- 作为语句而非表达式 ...

  3. [CSAPP] Chapter 1 Overview of Computer

    1.1 information is bits + context All computer programs are just a sequence of bits, each with a val ...

  4. bigdata

    1.打开cygwin,启动hadoop,运行jps命令查看节点启动情况 2.切换到hadoop根目录,运行指令 echo "hello boy hei baby hello word hel ...

  5. [Z]QPS、PV和需要部署机器数量计算公式

    QPS = req/sec = 请求数/秒 [QPS计算PV和机器的方式] QPS统计方式 [一般使用 http_load 进行统计]QPS = 总请求数 / ( 进程总数 *   请求时间 )QPS ...

  6. 「小程序JAVA实战」springboot的后台搭建(31)

    转自:https://idig8.com/2018/08/29/xiaochengxujavashizhanspringbootdehoutaidajian31/ 根据下面的图,我们来建立下对应的sp ...

  7. CBV 验证装饰器的使用

    下面是3种方式: from django.shortcuts import render, redirect from django.views import View # Create your v ...

  8. Strem String Memory TStringStream

    System.SysUtils 一.TStringStream方法 Strem>String TMemoryStream to String stm: TStream; ss: TStringS ...

  9. Kibana安装(图文详解)(多节点的ELK集群安装在一个节点就好)

    对于Kibana ,我们知道,是Elasticsearch/Logstash/Kibana的必不可少成员. 前提: Elasticsearch-2.4.3的下载(图文详解) Elasticsearch ...

  10. JS中如何获取当前时间及让时间格式化

    JS中获取当前时间和JAVA里获取当前时间一样,都是直接new Date即可.不同的是,JS中用var date=new Date();JAVA中用Data data=new Date();注:JS中 ...