Yii2 GridView过滤器按日期排序 [英] Yii2 GridView filter date by year

查看:599
本文介绍了Yii2 GridView过滤器按日期排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题很简单,但在Yii2中找不到解决方案。

给定生日字段,在gridview中如何逐年筛选?通常在一个index gridview中。

解决方案

也许你正在寻找像这样的东西:



添加到您的模型中

  public static function getYearsList()
{
$ (= {(%)) - > select('DISTINCT YEAR(`birthday`)as years') - > from('{{%yourTable}}') - > column();
return array_combine($ years,$ years);
}

然后在gridview中

  [
'属性'=> '生日',
'filter'=> YourModel :: getYearsList(),
]

然后在您的搜索模型中添加 andFilterWhere()来比较生日与年份。它可以看起来像这样:

$ $ p $ $ code $ query-> andFilterWhere('YEAR(`birthday`)'=> $ year );


This question is simple, but in Yii2 I cannot find the solution.

Given a birthday field, how is it possible in gridview to filter by year? Typically in an index gridview.

解决方案

Maybe you are looking for something like this:

add in to your model

public static function getYearsList()
{
    $years = (new Query())->select('DISTINCT YEAR(`birthday`) as years')->from('{{%yourTable}}')->column();
    return array_combine($years, $years);
}

and then in gridview

[
    'attribute' => 'birthday',
    'filter' => YourModel::getYearsList(),
]

And then in your search model add andFilterWhere() to compare birthday with year. It can look like this:

$query->andFilterWhere('YEAR(`birthday`)' => $year);

这篇关于Yii2 GridView过滤器按日期排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆