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
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的更多相关文章
- JQuery包装集size,length,index,slice,find,filter,is,children,next,nextAll,parent,parents,closest,siblings,add,end,andSelf的用法
在使用Jquery包装集的知识之前首先要注意三个概念(当前包装集.新包装集.包装集内部元素)的区别. <!DOCTYPE html> <html xmlns="http:/ ...
- Gridview转发
首页 开源项目 问答 动弹 博客 翻译 资讯 专题 城市圈 [ 登录 | 注册 ] 博客专区 > Reya滴水心的博客详情 Asp.net中GridView使用详解(很全,很经典) Reya滴水 ...
- Asp.net中GridView使用详解(引)
GridView无代码分页排序GridView选中,编辑,取消,删除GridView正反双向排序GridView和下拉菜单DropDownList结合GridView和CheckBox结合鼠标移到Gr ...
- 【转】 GridView 72般绝技
说明:准备出一个系列,所谓精髓讲C#语言要点.这个系列没有先后顺序,不过尽量做到精.可能会不断增删整理,本系列最原始出处是csdn博客,谢谢关注. C#精髓 第四讲 GridView 72般绝技 作者 ...
- GridView的详细用法
l GridView无代码分页排序 l GridView选中,编辑,取消,删除 l GridView正反双向排序 l GridView和下拉菜单DropDownList结合 l GridView和Ch ...
- GridView点击排序
快速预览:GridView无代码分页排序GridView选中,编辑,取消,删除GridView正反双向排序GridView和下拉菜单DropDownList结合GridView和CheckBox结合鼠 ...
- 转:C#精髓 第四讲 GridView 72般绝技
说明:准备出一个系列,所谓精髓讲C#语言要点.这个系列没有先后顺序,不过尽量做到精.可能会不断增删整理,本系列最原始出处是csdn博客,谢谢关注. C#精髓 第四讲 GridView 72般绝技 作者 ...
- Asp.net GridView 72般绝技
快速预览:GridView无代码分页排序GridView选中,编辑,取消,删除GridView正反双向排序GridView和下拉菜单DropDownList结合GridView和CheckBox结合鼠 ...
- 关于开发C#中的asp.net中gridview控件的使用
原文网址:http://blog.sina.com.cn/s/blog_67f1b4b201017663.html 1.GridView无代码分页排序: 效果图: 1.AllowSorting设为Tr ...
随机推荐
- node.js 获取客户端信息
结果:
- HTTP请求与响应协议
HTTP(hypertext transport protocol),即超文本传输协议.这个协议详细规定了浏览器和万维网服务器之间互相通信的规则 HTTP就是一个通信规则,通信规则规定了客户端发送给服 ...
- mac环境下IDEA无法下载plugin或者自动下载Library
卧槽,原谅我这么晚还在写blog,明天早上还要上班. 问题,idea 新建springmvc无法自动下载lib,报这个: you have JVM property "https.proxy ...
- pycharm多行代码同时注释、去除注释
pycharm中同时注释多行代码快捷键: 代码选中的条件下,同时按住 Ctrl+/,被选中行被注释,再次按下Ctrl+/,注释被取消
- 【转】C#父类与子类的静态成员变量、实例成员变量、构造函数的执行顺序
原文地址:http://www.xuebuyuan.com/1092603.html Win7+VS2010测试的结果如下: ①子类静态成员变量②子类静态构造函数③子类实例成员变量④父类静态成员变量⑤ ...
- java.lang.ClassFormatError: Extra bytes at the end of class file
在精简JRE过程中,将rt.jar中类通过FileInputStream,FileOutputStream进行拷贝操作出错: java.lang.ClassFormatError: Extra byt ...
- HTTP与TCP/IP的区别
TPC/IP协议是传输层协议,主要解决数据如何在网络中传输,而HTTP是应用层协议,主要解决如何包装数据.关于TCP/IP和HTTP协议的关系,网络有一段比较容易理解的介绍:“我们在传输数据时,可以只 ...
- 前端基础——jQuery
一 jQuery 1 简介 jQuery是一个“写得更少,但做得更多”的轻量级JavaScript库.jQuery极大地简化了JavaScript编程. 它是轻量级的js库(压缩后只有21k) ,这是 ...
- “microsoft ace oledb 12.0 未注册”疑云
1. 有人说: 2015也是要安装32位的AccessDataengine,anycpu选32位优先才行,不然就是Microsoft.ACE.OLEDB.12.0未注册. hanstom,一个老调重弹 ...
- leetcode744
public class Solution { public char NextGreatestLetter(char[] letters, char target) { //a-97 z-122 v ...