contributte / datagrid

:muscle: DataGrid for Nette Framework: filtering, sorting, pagination, tree view, table view, translator, etc
https://contributte.org/packages/contributte/datagrid/
MIT License
292 stars 195 forks source link

Using setFilterDateRange() for filtering by year of birth #952

Open milsorm opened 3 years ago

milsorm commented 3 years ago

I am trying to solve limiting datagrid with people by range of their year of birth.

If I use $col->setFilterDateRange()->setFormat( 'Y', 'yyyy' ); for limiting filter to year on ColumnDateTime, I have to enter year+1 to "to" field, because internally in applyFilterDateRange() is used as DAY not YEAR with 23:59:59 time.

Is it possibility to add setCondition or formatting for YEAR( column ) to check of this filter? I didn't find a way.

Anyway applyFilterRange() cannot help because there is fix use of column <= int and I cannot pass YEAR( column) to column.

I am using Nextras ORM as data source.

milsorm commented 3 years ago

Solution:

The question is if:

Solution with new descendants is also possible (and I am using it) but duplicates some code which is bad for maintanance with new versions.

I am using DataGrid together with Nextras ORM havily in new project for non-government organization so this is why I am creating new issues :-( Sorry for that.

mskocik commented 3 years ago

You could simplify your solution by calling setCondition (docs) on your dateFilter and provide custom template with setTemplate (docs) like in this snippet:

$grid->addFilterDateRange('born', 'Year of birth')
    // when working with Nextras/ORM (Nextras/Dbal), DbalCollection is first argument.
    ->setCondition(function(DbalCollection $source, $value) {
        $qb = $source->getQueryBuilder();
        $qb->andWhere('YEAR(column) = %s', /** here handle $value as you need */);
    })
    // use setTemplate, or use tempate block as can be seen in documentaion linked above
    ->setTemplate(/** path_to_your_year_date_range_filter_template.latte **/)
milsorm commented 3 years ago

Good point, thanks. This is like my third bullet in question and it works. Thanks.