Skip to content

Commit

Permalink
Hideable columns remember state fix, closes #934
Browse files Browse the repository at this point in the history
  • Loading branch information
paveljanda committed Dec 10, 2020
1 parent e16a0aa commit 09a73fd
Showing 1 changed file with 28 additions and 15 deletions.
43 changes: 28 additions & 15 deletions src/DataGrid.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@ class DataGrid extends Control

use TDataGridAggregationFunction;

private const HIDEABLE_COLUMNS_SESSION_KEYS = [
'_grid_hidden_columns',
'_grid_hidden_columns_manipulated',
];

/**
* @var array|callable[]
*/
Expand Down Expand Up @@ -334,20 +339,17 @@ class DataGrid extends Control
/**
* @var bool
*/
protected $refreshURL = true;
protected $rememberHideableColumnsState = true;

/**
* @var SessionSection
* @var bool
*/
protected $gridSession;
protected $refreshURL = true;

/**
* @var array
* @var SessionSection
*/
private $hideColumnSessionKeys = [
'_grid_hidden_columns',
'_grid_hidden_columns_manipulated',
];
protected $gridSession;

/**
* @var ItemDetail|null
Expand Down Expand Up @@ -2745,9 +2747,10 @@ public function getSessionSectionName(): string
/**
* @return static
*/
public function setRememberState(bool $remember = true): self
public function setRememberState(bool $remember = true, bool $rememberHideableColumnsState = false): self
{
$this->rememberState = $remember;
$this->rememberHideableColumnsState = $rememberHideableColumnsState;

return $this;
}
Expand All @@ -2770,13 +2773,21 @@ public function setRefreshUrl(bool $refresh = true): self
*/
public function getSessionData(?string $key = null, $defaultValue = null)
{
if (!$this->rememberState && !in_array($key, $this->hideColumnSessionKeys, true)) {
return $key === null
? []
: $defaultValue;
$getValue = function() use ($key, $defaultValue) {
return ($key !== null ? $this->gridSession[$key] : $this->gridSession) ?: $defaultValue;
};

if ($this->rememberState) {
return ($getValue)();
}

return ($key !== null ? $this->gridSession[$key] : $this->gridSession) ?: $defaultValue;
if ($this->rememberHideableColumnsState && in_array($key, self::HIDEABLE_COLUMNS_SESSION_KEYS, true)) {
return ($getValue)();
}

return $key === null
? []
: $defaultValue;
}


Expand All @@ -2785,7 +2796,9 @@ public function getSessionData(?string $key = null, $defaultValue = null)
*/
public function saveSessionData(string $key, $value): void
{
if ($this->rememberState || in_array($key, $this->hideColumnSessionKeys, true)) {
if ($this->rememberState) {
$this->gridSession[$key] = $value;
} elseif ($this->rememberHideableColumnsState && in_array($key, self::HIDEABLE_COLUMNS_SESSION_KEYS, true)) {
$this->gridSession[$key] = $value;
}
}
Expand Down

0 comments on commit 09a73fd

Please sign in to comment.