Skip to content

Commit bd570f3

Browse files
committed
Fix a global and column search in Array & ResultSet
2 parents 9200970 + d038327 commit bd570f3

File tree

5 files changed

+101
-19
lines changed

5 files changed

+101
-19
lines changed

site/app/views/index.volt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
{{ partial('partials/column_search') }}
3636
<hr>
3737
<br><br>
38-
<h3>ResultSet Examples</h3>
38+
<h3>Array&ResultSet Examples</h3>
3939
<p>
4040
Usage examples: Basic, Search-by-column<br>
4141
</p>

spec/suite/Adapters/ArraySpec.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,33 @@
120120

121121
});
122122

123+
it("should search a int", function() {
124+
125+
$_GET = [
126+
'search' => ['value' => '200'],
127+
'columns' => [
128+
[
129+
'data' => 'name',
130+
'searchable' => "true"
131+
],
132+
[
133+
'data' => 'balance',
134+
'searchable' => "true"
135+
]
136+
]
137+
];
138+
139+
$dataTables = new ArrayAdapter(20);
140+
$dataTables->setArray($this->array);
141+
$dataTables->setColumns(['name', 'email', 'balance']);
142+
$dataTables->setParser(new ParamsParser(10));
143+
144+
$response = $dataTables->getResponse();
145+
expect(count($response['data']))->toBe(10);
146+
expect($response['recordsFiltered'])->toBe(41);
147+
148+
});
149+
123150
it("should work with a column search", function() {
124151

125152
$_GET = [

spec/suite/Adapters/ResultSetSpec.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,33 @@
152152

153153
});
154154

155+
it("should search a int", function() {
156+
157+
$_GET = [
158+
'search' => ['value' => '200'],
159+
'columns' => [
160+
[
161+
'data' => 'name',
162+
'searchable' => "true"
163+
],
164+
[
165+
'data' => 'balance',
166+
'searchable' => "true"
167+
]
168+
]
169+
];
170+
171+
$dataTables = new ResultSet(20);
172+
$dataTables->setResultSet($this->query);
173+
$dataTables->setColumns(['name', 'email', 'balance']);
174+
$dataTables->setParser(new ParamsParser(10));
175+
176+
$response = $dataTables->getResponse();
177+
expect(count($response['data']))->toBe(10);
178+
expect($response['recordsFiltered'])->toBe(41);
179+
180+
});
181+
155182
it("should work with a column&global search", function() {
156183

157184
$_GET = [

src/Adapters/ArrayAdapter.php

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
class ArrayAdapter extends AdapterInterface {
66

77
protected $array = [];
8-
protected $filter = [];
8+
protected $column = [];
9+
protected $global = [];
910
protected $order = [];
1011

1112
public function setArray(array $array) {
@@ -18,25 +19,38 @@ public function getResponse() {
1819
$total = count($this->array);
1920

2021
$this->bind('global_search', function($column, $search) {
21-
$this->filter[$column][] = $search;
22+
$this->global[$column][] = $search;
2223
});
2324

2425
$this->bind('column_search', function($column, $search) {
25-
$this->filter[$column][] = $search;
26+
$this->column[$column][] = $search;
2627
});
2728

2829
$this->bind('order', function($order) {
2930
$this->order = $order;
3031
});
3132

32-
if(count($this->filter)) {
33+
if(count($this->global) || count($this->column)) {
3334
$items = array_filter($this->array, function($item) {
34-
$check = true;
35+
$check = false;
36+
37+
if (count($this->global)) {
38+
foreach($this->global as $column=>$filters) {
39+
foreach($filters as $search) {
40+
$check = (strpos($item[$column], $search) !== false);
41+
if ($check) break 2;
42+
}
43+
}
44+
} else {
45+
$check = true;
46+
}
3547

36-
foreach($this->filter as $column=>$filters) {
37-
foreach($filters as $search) {
38-
$check = (strpos($item[$column], $search) !== false);
39-
if (!$check) break 2;
48+
if (count($this->column) && $check) {
49+
foreach($this->column as $column=>$filters) {
50+
foreach($filters as $search) {
51+
$check = (strpos($item[$column], $search) !== false);
52+
if (!$check) break 2;
53+
}
4054
}
4155
}
4256

src/Adapters/ResultSet.php

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
class ResultSet extends AdapterInterface {
77

88
protected $resultSet;
9-
protected $filter = [];
9+
protected $column = [];
10+
protected $global = [];
1011
protected $order = [];
1112

1213
public function getResponse() {
@@ -15,25 +16,38 @@ public function getResponse() {
1516
$total = $this->resultSet->count();
1617

1718
$this->bind('global_search', function($column, $search) {
18-
$this->filter[$column][] = $search;
19+
$this->global[$column][] = $search;
1920
});
2021

2122
$this->bind('column_search', function($column, $search) {
22-
$this->filter[$column][] = $search;
23+
$this->column[$column][] = $search;
2324
});
2425

2526
$this->bind('order', function($order) {
2627
$this->order = $order;
2728
});
2829

29-
if(count($this->filter)) {
30+
if(count($this->global) || count($this->column)) {
3031
$filter = $this->resultSet->filter(function($item){
31-
$check = true;
32+
$check = false;
33+
34+
if (count($this->global)) {
35+
foreach($this->global as $column=>$filters) {
36+
foreach($filters as $search) {
37+
$check = (strpos($item->$column, $search) !== false);
38+
if ($check) break 2;
39+
}
40+
}
41+
} else {
42+
$check = true;
43+
}
3244

33-
foreach($this->filter as $column=>$filters) {
34-
foreach($filters as $search) {
35-
$check = (strpos($item->$column, $search) !== false);
36-
if (!$check) break 2;
45+
if (count($this->column) && $check) {
46+
foreach($this->column as $column=>$filters) {
47+
foreach($filters as $search) {
48+
$check = (strpos($item->$column, $search) !== false);
49+
if (!$check) break 2;
50+
}
3751
}
3852
}
3953

0 commit comments

Comments
 (0)