Skip to content

Commit

Permalink
[ISSUE-7] Number range filter
Browse files Browse the repository at this point in the history
  • Loading branch information
Pedro Teixeira committed Apr 16, 2013
1 parent a83b346 commit 9873c2f
Show file tree
Hide file tree
Showing 7 changed files with 124 additions and 2 deletions.
5 changes: 3 additions & 2 deletions Grid/Filter/DateRange.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@ public function render()

} else {

$html = '<input name="' . $this->getIndex() . '[]" id="' . $this->getId() . 'from" type="date" value="' . $this->getValue() . '"> ';
$html .= '<input name="' . $this->getIndex() . '[]" id="' . $this->getId() . 'to" type="date" value="' . $this->getValue() . '">';
$html = '<input class="date-input" name="' . $this->getIndex() . '[]" id="' . $this->getId() . 'from" type="date" placeholder="' . $this->getPlaceholder() . '" value="' . $this->getValue() . '"> ';
$html .= $this->getInputSeparator();
$html .= '<input class="date-input" name="' . $this->getIndex() . '[]" id="' . $this->getId() . 'to" type="date" placeholder="' . $this->getPlaceholder() . '" value="' . $this->getValue() . '">';

}

Expand Down
51 changes: 51 additions & 0 deletions Grid/Filter/NumberRange.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

namespace PedroTeixeira\Bundle\GridBundle\Grid\Filter;

/**
* Filter NumberRange
*/
class NumberRange extends FilterAbstract
{
/**
* @var string
*/
protected $operatorType = 'number_range';

/**
* @var string
*/
protected $inputSeparator = ' : ';

/**
* @return string
*/
public function render()
{
$html = '<input class="number-input" name="' . $this->getIndex() . '[]" id="' . $this->getId() . 'from" type="text" placeholder="' . $this->getPlaceholder() . '" value="' . $this->getValue() . '"> ';
$html .= $this->getInputSeparator();
$html .= '<input class="number-input" name="' . $this->getIndex() . '[]" id="' . $this->getId() . 'to" type="text" placeholder="' . $this->getPlaceholder() . '" value="' . $this->getValue() . '">';

return $html;
}

/**
* @param string $inputSeparator
*
* @return DateRange
*/
public function setInputSeparator($inputSeparator)
{
$this->inputSeparator = $inputSeparator;

return $this;
}

/**
* @return string
*/
public function getInputSeparator()
{
return $this->inputSeparator;
}
}
48 changes: 48 additions & 0 deletions Grid/Filter/Operator/NumberRange.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace PedroTeixeira\Bundle\GridBundle\Grid\Filter\Operator;

/**
* NumberRange
*/
class NumberRange extends OperatorAbstract
{
/**
* @param array $value
*
* @throws \Exception
*/
public function execute($value)
{
if (!is_array($value) || count($value) != 2) {
throw new \Exception('Value for number range comparison should have to values');
}

$queryBuilder = $this->getQueryBuilder();

if (!empty($value[0])) {
$queryBuilder->andWhere(
$queryBuilder->expr()->gte(
$this->getIndex(),
":{$this->getIndexClean()}_1"
))
->setParameter(
":{$this->getIndexClean()}_1",
(float) $value[0]
);
}

if (!empty($value[1])) {

$queryBuilder->andWhere(
$queryBuilder->expr()->lte(
$this->getIndex(),
":{$this->getIndexClean()}_2"
))
->setParameter(
":{$this->getIndexClean()}_2",
(float) $value[1]
);
}
}
}
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,11 @@ Create your grid
->setField('name')
->setIndex('r.name');

$this->addColumn('Number')
->setField('number')
->setIndex('r.number')
->setFilterType('number_range');

$this->addColumn('Options')
->setField('option')
->setIndex('r.options')
Expand Down
10 changes: 10 additions & 0 deletions Resources/doc/column/filter.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ Operator Comparison Types:
* end_with
* not_end_with
* not_contain
* number_range

date
------------
Expand All @@ -77,6 +78,15 @@ Attributes:

* inputSeparator

number_range
------------

Two fields to define a number range.

Attributes:

* inputSeparator

select
------------

Expand Down
4 changes: 4 additions & 0 deletions Resources/public/css/grid.css
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@
width: 75px;
}

.pedroteixeira-grid-wrapper table thead tr th .number-input {
width: 75px;
}

.pedroteixeira-grid-wrapper table tfoot #pagination {
font-size: 0.8em;
}
Expand Down
3 changes: 3 additions & 0 deletions Resources/public/less/grid.less
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
.date-input {
width: 75px;
}
.number-input {
width: 75px;
}
}
}

Expand Down

0 comments on commit 9873c2f

Please sign in to comment.