contributte / datagrid

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

get filter status #1053

Closed tonyp90 closed 1 year ago

tonyp90 commented 1 year ago

Hello, I'm using setRowCallback to distinguish odd and even weeks from date column.

$grid->setRowCallback(function($item, $tr) {
    if ($item->op_exp_date->format('W') % 2) {
        $tr->addClass('datagrid-even');
    }
});

Any tips how to make this active only when user not using any filter/sort?

if (!$grid->filterActive) {
.....
}
MrVoltz commented 1 year ago

You can try this, use (...) lets you access variables from the parent scope. But remember, issues are for bugs in Datagrid, not for teaching PHP syntax.

$grid->setRowCallback(function($item, $tr) use ($grid) {
  if (!$grid->filterActive && $item->op_exp_date->format('W') % 2) {
    $tr->addClass('datagrid-even');
  }
});
tonyp90 commented 1 year ago

It was not about syntax. "filterActive" property no not exists, it was just my example. It was about finding the correct property. Just found an array with active filter columns.

if (empty($grid->filter)) {

}

and It's fine now.

MrVoltz commented 1 year ago

There is DataGrid::isFilterActive() method (https://github.com/contributte/datagrid/blob/master/src/DataGrid.php#L1246). I thought that it would be exposed as DataGrid::filterActive virtual property by SmartObject, but I forgot that you'd need a @property-read annotation for that, which is not present.