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. [置顶] apt-get update 更新失败----如何清除破损而且陈旧的PPA仓库 (W: Failed to fetch http://ppa.launchpad.net/jonathonf/pyt)

    我使用sudo apt-get update之后,更新失败,遇到如下错误. W: Failed to fetch http://ppa.launchpad.net/jonathonf/python-3 ...

  2. DOM库及常用方法封装

    节点 nodeType nodeName nodeValue 元素节点 1 大写的标签名 null 文本节点 3 #text 文本内容 注释节点 8 #comment 注释内容 document 9 ...

  3. Julia - 匿名函数

    Julia 中的函数可以被匿名构造,成为匿名函数,匿名函数是没有函数名的函数 julia> x -> x + 1 #3 (generic function with 1 method) 这 ...

  4. 根据段落编号自动添加书签的VBA

    Sub 宏1() ' ' 宏1 宏 ' '    Dim myRange As Word.Range Dim num As String, content As String Selection.Ho ...

  5. npm 全局环境变量配置

      我们要先配置npm的全局模块的存放路径以及cache的路径,例如我希望将以上两个文件夹放在NodeJS的主目录下,便在NodeJs下建立”node_global”及”node_cache”两个文件 ...

  6. Make 命令教程(转载)

    代码变成可执行文件,叫做编译(compile):先编译这个,还是先编译那个(即编译的安排),叫做构建(build). Make是最常用的构建工具,诞生于1977年,主要用于C语言的项目.但是实际上 , ...

  7. UVALIVE 4556 The Next Permutation

    4556 The Next PermutationFor this problem, you will write a program that takes a (possibly long) str ...

  8. 我的MAXSCRIPT笔记

    getnodebyname "circle01" for o in objects do if o.name == "circle01" then select ...

  9. STL源码剖析之空间配置器

    本文大致对STL中的空间配置器进行一个简单的讲解,由于只是一篇博客类型的文章,无法将源码表现到面面俱到,所以真正感兴趣的码农们可以从源码中或者<STL源码剖析>仔细了解一下. 1,为什么S ...

  10. SData:优雅的数据交换方案

    SData的网址是https://github.com/knat/SData. 数据交换方案可以分为两类:有纲要(schema)的和无纲要的.有纲要的数据交换方案有Google的Protocol Bu ...