Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add permanent filters feature

  • Loading branch information...
commit 8bf02f69c9dd1c1f205c80acbf84721b53c300c0 1 parent f8f8714
@Abhoryo authored
View
96 Grid/Grid.php
@@ -211,6 +211,13 @@ class Grid
protected $dataJunction = Column::DATA_CONJUNCTION;
/**
+ * Permanent filters
+ *
+ * @var array
+ */
+ protected $permanentFilters = array();
+
+ /**
* Default filters
*
* @var array
@@ -266,7 +273,7 @@ public function __construct($container, $id = '')
$this->columns = new Columns($this->securityContext);
$this->routeParameters = $this->request->attributes->all();
- foreach ($this->routeParameters as $key => $param) {
+ foreach (array_keys($this->routeParameters) as $key) {
if (substr($key, 0, 1) == '_') {
unset($this->routeParameters[$key]);
}
@@ -327,8 +334,6 @@ public function isReadyForRedirect()
if (!$this->executeExports()) {
$this->processRequestData();
-
- $this->saveSession();
}
$this->redirect = true;
@@ -337,10 +342,10 @@ public function isReadyForRedirect()
if ($this->redirect === null || ($this->request->isXmlHttpRequest() && !$this->isReadyForExport)) {
if ($this->newSession) {
$this->setDefaultSessionData();
-
- $this->saveSession();
}
+ $this->processPermanentFilters();
+
//Configures the grid with the data read from the session.
$this->processSessionData();
@@ -421,7 +426,7 @@ protected function processRequestData()
$this->set($ColumnId, $data);
// Filtering ?
- if ($data !== null) {
+ if (!$filtering && $data !== null) {
$filtering = true;
}
}
@@ -453,15 +458,44 @@ protected function processRequestData()
if (isset($this->limits[$limit])) {
$this->set(self::REQUEST_QUERY_LIMIT, $limit);
}
+
+ $this->saveSession();
}
- protected function setDefaultSessionData()
+ /**
+ * Store permanent filters to the session and disable the filter capability for the column if there are permanent filters
+ */
+ protected function processFilters($permanent = true)
{
- // Default filters
- foreach($this->defaultFilters as $columnId => $value) {
- $this->columns->getColumnById($columnId);
+ foreach(($permanent ? $this->permanentFilters : $this->defaultFilters) as $columnId => $value) {
+ /* @var column Column */
+ $column = $this->columns->getColumnById($columnId);
+
+ if ($permanent) {
+ // Disable the filter capability for the column
+ $column->setFilterable(false);
+ }
+
+ // Store in the session
$this->set($columnId, $value);
}
+ }
+
+ protected function processPermanentFilters()
+ {
+ $this->processFilters();
+ $this->saveSession();
+ }
+
+ protected function processDefaultFilters()
+ {
+ $this->processFilters(false);
+ }
+
+ protected function setDefaultSessionData()
+ {
+ // Default filters
+ $this->processDefaultFilters();
// Default page
if ($this->defaultPage !== null) {
@@ -476,7 +510,7 @@ protected function setDefaultSessionData()
if ($this->defaultOrder !== null) {
list($columnId, $columnOrder) = explode('|', $this->defaultOrder);
- $column = $this->columns->getColumnById($columnId);
+ $this->columns->getColumnById($columnId);
if (in_array(strtolower($columnOrder), array('asc', 'desc'))) {
$this->set(self::REQUEST_QUERY_ORDER, $this->defaultOrder);
} else {
@@ -495,6 +529,8 @@ protected function setDefaultSessionData()
throw new \InvalidArgumentException('Limit must be a positive number');
}
}
+
+ $this->saveSession();
}
/**
@@ -560,7 +596,7 @@ protected function prepare()
//add row actions column
if (count($this->rowActions) > 0) {
foreach ($this->rowActions as $column => $rowActions) {
- if ($actionColumn = $this->columns->hasColumnById($column, true)) {
+ if (($actionColumn = $this->columns->hasColumnById($column, true))) {
$actionColumn->setRowActions($rowActions);
} else {
$actionColumn = new ActionsColumn($column, 'Actions', $rowActions);
@@ -1014,13 +1050,14 @@ public function isReadyForExport()
}
/**
- * Set default value for filters
+ * Set value for filters
*
* @param array Hash of columnName => initValue
+ * @param boolean permanent filters ?
*
* @return self
*/
- public function setDefaultFilters(array $filters)
+ protected function setFilters(array $filters, $permanent = true)
{
foreach ($filters as $columnId => $ColumnValue) {
if (is_array($ColumnValue)){
@@ -1033,13 +1070,42 @@ public function setDefaultFilters(array $filters)
$value['from'] = $value['from'] ? '1' : '0';
}
- $this->defaultFilters[$columnId] = $value;
+ if ($permanent) {
+ $this->permanentFilters[$columnId] = $value;
+ } else {
+ $this->defaultFilters[$columnId] = $value;
+ }
}
return $this;
}
/**
+ * Set permanent value for filters
+ *
+ * @param array Hash of columnName => initValue
+ * @param boolean fixed filters ?
+ *
+ * @return self
+ */
+ public function setPermanentFilters(array $filters)
+ {
+ return $this->setFilters($filters);
+ }
+
+ /**
+ * Set default value for filters
+ *
+ * @param array Hash of columnName => initValue
+ *
+ * @return self
+ */
+ public function setDefaultFilters(array $filters)
+ {
+ return $this->setFilters($filters, false);
+ }
+
+ /**
* Set the default grid order.
*
* @param array Hash of columnName => initValue
View
2  LICENSE
@@ -1,7 +1,5 @@
Copyright (c) 2011-2013 Stanislav Turza - Abhoryo
-The MIT License
-
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
View
72 Resources/doc/grid_configuration/set_permanent_filters.md
@@ -0,0 +1,72 @@
+Set permanent filters of the grid
+===============================
+
+You can define permanent filters. These values will be used every time and the filter part will be disable for columns which have a permanent filter.
+
+## Usage
+
+```php
+<?php
+...
+// Set the source
+$grid->setSource($source);
+
+// Set permanent filters of the grid
+$grid->setPermanentFilters($filters);
+...
+```
+
+## Grid::setPermanentFilters parameters
+
+|parameter|Type|Default value|Description|
+|:--:|:--|:--|:--|:--|
+|filters|array|array()|Array of array or string pair|
+
+## Values for the filters parameter
+
+|parameter|Type|Default value|Description|
+|:--:|:--|:--|:--|:--|
+|operator|string|default operator of the column|Operator used to filter|
+|from|string|null|The value of the filter or the _from_ value for the between operators|
+|to|string|null|The _to_ value for the between operators|
+
+**Note**: If these three parameters are not defined and only a string value is defined, this value will be attributed to the _from_ value of the filter and will use the default operator of the column.
+
+## Available Operators
+
+|Operator|Meaning|
+|:--:|:--|
+|eq|Equals|
+|neq|Not equal to|
+|lt|Lower than|
+|lte|Lower than or equal to|
+|gt|Greater than|
+|gte|Greater than or equal to|
+|like|Contains|
+|nlike|Not contain|
+|rlike|Starts with|
+|llike|Ends with|
+|btw|Between exclusive|
+|btwe|Between inclusive|
+|isNull|Is not defined|
+|isNotNull|Is defined|
+
+## Example
+
+```php
+<?php
+...
+// Set the source
+$grid->setSource($source);
+
+// Set default filters of the grid
+$grid->setPermanentFilters(array(
+ 'your_column_to_filter1' => 'your_init_value1', // Use the default operator of the column
+ 'your_column_to_filter1' => array('from' => 'your_init_value1'), // Use the default operator of the column
+ 'your_column_to_filter2' => array('operator' => 'eq', 'from' => 'your_init_value_from2'), // Define an operator
+ 'your_column_to_filter3' => array('from' => 'your_init_value_from3', 'to' => 'your_init_value_to3'), // Range filter with the default operator 'btw'
+ 'your_column_to_filter4' => array('operator' => 'btw', 'from' => 'your_init_value_from4', 'to' => 'your_init_value_to4') // Range filter with the operator 'tbw'
+ 'your_column_to_filter4' => array('operator' => 'isNull') // isNull operator
+));
+...
+```
View
1  Resources/doc/summary.md
@@ -69,6 +69,7 @@ SUMMARY
1. [Set a default order](grid_configuration/set_default_order.md)
1. [Set a default items per page](grid_configuration/set_default_limit.md)
1. [Set default filters](grid_configuration/set_default_filters.md)
+ 1. [Set permanent filters](grid_configuration/set_permanent_filters.md)
1. [Set the no data message](grid_configuration/set_no_data_message.md)
1. [Set the no result message](grid_configuration/set_no_result_message.md)
1. [Always show the grid](grid_configuration/always_show_grid.md)
Please sign in to comment.
Something went wrong with that request. Please try again.