Skip to content

Commit

Permalink
Fix a global and column search in Array & ResultSet
Browse files Browse the repository at this point in the history
  • Loading branch information
m1ome committed Feb 24, 2015
2 parents 9200970 + d038327 commit bd570f3
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 19 deletions.
2 changes: 1 addition & 1 deletion site/app/views/index.volt
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
{{ partial('partials/column_search') }}
<hr>
<br><br>
<h3>ResultSet Examples</h3>
<h3>Array&ResultSet Examples</h3>
<p>
Usage examples: Basic, Search-by-column<br>
</p>
Expand Down
27 changes: 27 additions & 0 deletions spec/suite/Adapters/ArraySpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,33 @@

});

it("should search a int", function() {

$_GET = [
'search' => ['value' => '200'],
'columns' => [
[
'data' => 'name',
'searchable' => "true"
],
[
'data' => 'balance',
'searchable' => "true"
]
]
];

$dataTables = new ArrayAdapter(20);
$dataTables->setArray($this->array);
$dataTables->setColumns(['name', 'email', 'balance']);
$dataTables->setParser(new ParamsParser(10));

$response = $dataTables->getResponse();
expect(count($response['data']))->toBe(10);
expect($response['recordsFiltered'])->toBe(41);

});

it("should work with a column search", function() {

$_GET = [
Expand Down
27 changes: 27 additions & 0 deletions spec/suite/Adapters/ResultSetSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,33 @@

});

it("should search a int", function() {

$_GET = [
'search' => ['value' => '200'],
'columns' => [
[
'data' => 'name',
'searchable' => "true"
],
[
'data' => 'balance',
'searchable' => "true"
]
]
];

$dataTables = new ResultSet(20);
$dataTables->setResultSet($this->query);
$dataTables->setColumns(['name', 'email', 'balance']);
$dataTables->setParser(new ParamsParser(10));

$response = $dataTables->getResponse();
expect(count($response['data']))->toBe(10);
expect($response['recordsFiltered'])->toBe(41);

});

it("should work with a column&global search", function() {

$_GET = [
Expand Down
32 changes: 23 additions & 9 deletions src/Adapters/ArrayAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
class ArrayAdapter extends AdapterInterface {

protected $array = [];
protected $filter = [];
protected $column = [];
protected $global = [];
protected $order = [];

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

$this->bind('global_search', function($column, $search) {
$this->filter[$column][] = $search;
$this->global[$column][] = $search;
});

$this->bind('column_search', function($column, $search) {
$this->filter[$column][] = $search;
$this->column[$column][] = $search;
});

$this->bind('order', function($order) {
$this->order = $order;
});

if(count($this->filter)) {
if(count($this->global) || count($this->column)) {
$items = array_filter($this->array, function($item) {
$check = true;
$check = false;

if (count($this->global)) {
foreach($this->global as $column=>$filters) {
foreach($filters as $search) {
$check = (strpos($item[$column], $search) !== false);
if ($check) break 2;
}
}
} else {
$check = true;
}

foreach($this->filter as $column=>$filters) {
foreach($filters as $search) {
$check = (strpos($item[$column], $search) !== false);
if (!$check) break 2;
if (count($this->column) && $check) {
foreach($this->column as $column=>$filters) {
foreach($filters as $search) {
$check = (strpos($item[$column], $search) !== false);
if (!$check) break 2;
}
}
}

Expand Down
32 changes: 23 additions & 9 deletions src/Adapters/ResultSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
class ResultSet extends AdapterInterface {

protected $resultSet;
protected $filter = [];
protected $column = [];
protected $global = [];
protected $order = [];

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

$this->bind('global_search', function($column, $search) {
$this->filter[$column][] = $search;
$this->global[$column][] = $search;
});

$this->bind('column_search', function($column, $search) {
$this->filter[$column][] = $search;
$this->column[$column][] = $search;
});

$this->bind('order', function($order) {
$this->order = $order;
});

if(count($this->filter)) {
if(count($this->global) || count($this->column)) {
$filter = $this->resultSet->filter(function($item){
$check = true;
$check = false;

if (count($this->global)) {
foreach($this->global as $column=>$filters) {
foreach($filters as $search) {
$check = (strpos($item->$column, $search) !== false);
if ($check) break 2;
}
}
} else {
$check = true;
}

foreach($this->filter as $column=>$filters) {
foreach($filters as $search) {
$check = (strpos($item->$column, $search) !== false);
if (!$check) break 2;
if (count($this->column) && $check) {
foreach($this->column as $column=>$filters) {
foreach($filters as $search) {
$check = (strpos($item->$column, $search) !== false);
if (!$check) break 2;
}
}
}

Expand Down

0 comments on commit bd570f3

Please sign in to comment.