Yii2 GridView filter date by year

╄→гoц情女王★ 提交于 2019-12-13 09:55:47

问题


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.


回答1:


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);


来源:https://stackoverflow.com/questions/31193794/yii2-gridview-filter-date-by-year

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!