Skip to content

Commit e4ad909

Browse files
bijicurry684
authored andcommitted
fix global search for number and boolean
new column options: operator leftExpr rightExpr
1 parent f8e55cc commit e4ad909

File tree

4 files changed

+132
-3
lines changed

4 files changed

+132
-3
lines changed

src/Adapter/Doctrine/ORM/SearchCriteriaProvider.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Doctrine\ORM\QueryBuilder;
1717
use Omines\DataTablesBundle\Column\AbstractColumn;
1818
use Omines\DataTablesBundle\DataTableState;
19+
use Omines\DataTablesBundle\Adapter\Doctrine\ORM\QueryBuilderProcessorInterface;
1920

2021
/**
2122
* SearchCriteriaProvider.
@@ -60,8 +61,9 @@ private function processGlobalSearch(QueryBuilder $queryBuilder, DataTableState
6061
$expr = $queryBuilder->expr();
6162
$comparisons = $expr->orX();
6263
foreach ($state->getDataTable()->getColumns() as $column) {
63-
if ($column->isGlobalSearchable() && !empty($field = $column->getField())) {
64-
$comparisons->add($expr->like($field, $expr->literal("%{$globalSearch}%")));
64+
if ($column->isGlobalSearchable() && !empty($field = $column->getField()) && $column->isValidForSearch($globalSearch)) {
65+
$comparisons->add(new Comparison($column->getLeftExpr(), $column->getOperator(),
66+
$expr->literal($column->getRightExpr($globalSearch))));
6567
}
6668
}
6769
$queryBuilder->andWhere($comparisons);

src/Column/AbstractColumn.php

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,9 @@ protected function configureOptions(OptionsResolver $resolver)
123123
'filter' => null,
124124
'className' => null,
125125
'render' => null,
126+
'leftExpr' => null,
127+
'operator' => '=',
128+
'rightExpr' => null,
126129
])
127130
->setAllowedTypes('label', ['null', 'string'])
128131
->setAllowedTypes('data', ['null', 'string', 'callable'])
@@ -132,10 +135,13 @@ protected function configureOptions(OptionsResolver $resolver)
132135
->setAllowedTypes('orderable', ['null', 'boolean'])
133136
->setAllowedTypes('orderField', ['null', 'string'])
134137
->setAllowedTypes('searchable', ['null', 'boolean'])
135-
->setAllowedTypes('globalSearchable', ['null', 'boolean'])
138+
->setAllowedTypes('globalSearchable', ['null', 'boolean'])
136139
->setAllowedTypes('filter', ['null', 'array'])
137140
->setAllowedTypes('className', ['null', 'string'])
138141
->setAllowedTypes('render', ['null', 'string', 'callable'])
142+
->setAllowedTypes('operator', ['string'])
143+
->setAllowedTypes('leftExpr', ['null', 'string', 'callable'])
144+
->setAllowedTypes('rightExpr', ['null', 'string', 'callable'])
139145
;
140146

141147
return $this;
@@ -237,6 +243,40 @@ public function isGlobalSearchable(): bool
237243
return $this->options['globalSearchable'] ?? $this->isSearchable();
238244
}
239245

246+
/**
247+
* @return string
248+
*/
249+
public function getLeftExpr()
250+
{
251+
$leftExpr = $this->options['leftExpr'];
252+
if ($leftExpr === null) return $this->getField();
253+
if (is_callable($leftExpr)) {
254+
return call_user_func($leftExpr, $this->getField());
255+
}
256+
return $leftExpr;
257+
}
258+
259+
/**
260+
* @return mixed
261+
*/
262+
public function getRightExpr($value)
263+
{
264+
$rightExpr = $this->options['rightExpr'];
265+
if ($rightExpr === null) return $value;
266+
if (is_callable($rightExpr)) {
267+
return call_user_func($rightExpr, $value);
268+
}
269+
return $rightExpr;
270+
}
271+
272+
/**
273+
* @return string
274+
*/
275+
public function getOperator()
276+
{
277+
return $this->options['operator'];
278+
}
279+
240280
/**
241281
* @return string
242282
*/
@@ -264,4 +304,14 @@ public function setOption(string $name, $value): self
264304

265305
return $this;
266306
}
307+
308+
/**
309+
* @param string $value
310+
* @return bool
311+
*/
312+
public function isValidForSearch($value)
313+
{
314+
return true;
315+
}
316+
267317
}

src/Column/BoolColumn.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,14 @@ protected function configureOptions(OptionsResolver $resolver)
4040
{
4141
parent::configureOptions($resolver);
4242

43+
$resolver
44+
->setDefault(
45+
'rightExpr',
46+
function ($value) {
47+
return trim(strtolower($value)) == $this->getTrueValue();
48+
}
49+
);
50+
4351
$resolver
4452
->setDefault('trueValue', 'true')
4553
->setDefault('falseValue', 'false')
@@ -75,4 +83,14 @@ public function getNullValue(): string
7583
{
7684
return $this->options['nullValue'];
7785
}
86+
87+
/**
88+
* @param string $value
89+
* @return bool
90+
*/
91+
public function isValidForSearch($value)
92+
{
93+
$value = trim(strtolower($value));
94+
return ($value == $this->getTrueValue()) || ($value == $this->getFalseValue());
95+
}
7896
}

src/Column/NumberColumn.php

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Omines\DataTablesBundle\Column;
6+
7+
use Omines\DataTablesBundle\Column\AbstractColumn;
8+
use Symfony\Component\OptionsResolver\OptionsResolver;
9+
10+
/**
11+
* NumberColumn.
12+
*
13+
* @author Niels Keurentjes <[email protected]>
14+
*/
15+
class NumberColumn extends AbstractColumn
16+
{
17+
/**
18+
* {@inheritdoc}
19+
*/
20+
public function normalize($value): string
21+
{
22+
$value = (string) $value;
23+
if (is_numeric($value)) return $value;
24+
25+
return $this->isRaw() ? $value : (string) floatval($value);
26+
}
27+
28+
/**
29+
* {@inheritdoc}
30+
*/
31+
protected function configureOptions(OptionsResolver $resolver)
32+
{
33+
parent::configureOptions($resolver);
34+
35+
$resolver
36+
->setDefault('raw', false)
37+
->setAllowedTypes('raw', 'bool')
38+
;
39+
40+
return $this;
41+
}
42+
43+
/**
44+
* @return bool
45+
*/
46+
public function isRaw(): bool
47+
{
48+
return $this->options['raw'];
49+
}
50+
51+
/**
52+
* @param string $value
53+
* @return bool
54+
*/
55+
public function isValidForSearch($value)
56+
{
57+
return is_numeric($value);
58+
}
59+
}

0 commit comments

Comments
 (0)