From d03832738584afa5dc786019edec91c9859541b1 Mon Sep 17 00:00:00 2001 From: w1n2k Date: Tue, 24 Feb 2015 21:40:55 +0300 Subject: [PATCH] Fix #3 --- site/app/views/index.volt | 2 +- spec/suite/Adapters/ArraySpec.php | 27 ++++++++++++++++++++++ spec/suite/Adapters/ResultSetSpec.php | 27 ++++++++++++++++++++++ src/Adapters/ArrayAdapter.php | 32 +++++++++++++++++++-------- src/Adapters/ResultSet.php | 32 +++++++++++++++++++-------- 5 files changed, 101 insertions(+), 19 deletions(-) diff --git a/site/app/views/index.volt b/site/app/views/index.volt index 41d787b..5b60454 100644 --- a/site/app/views/index.volt +++ b/site/app/views/index.volt @@ -35,7 +35,7 @@ {{ partial('partials/column_search') }}


-

ResultSet Examples

+

Array&ResultSet Examples

Usage examples: Basic, Search-by-column

diff --git a/spec/suite/Adapters/ArraySpec.php b/spec/suite/Adapters/ArraySpec.php index 7219091..13186af 100644 --- a/spec/suite/Adapters/ArraySpec.php +++ b/spec/suite/Adapters/ArraySpec.php @@ -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 = [ diff --git a/spec/suite/Adapters/ResultSetSpec.php b/spec/suite/Adapters/ResultSetSpec.php index 9b2fa81..b5e45f3 100644 --- a/spec/suite/Adapters/ResultSetSpec.php +++ b/spec/suite/Adapters/ResultSetSpec.php @@ -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 = [ diff --git a/src/Adapters/ArrayAdapter.php b/src/Adapters/ArrayAdapter.php index 49c8da6..3f3086c 100644 --- a/src/Adapters/ArrayAdapter.php +++ b/src/Adapters/ArrayAdapter.php @@ -5,7 +5,8 @@ class ArrayAdapter extends AdapterInterface { protected $array = []; - protected $filter = []; + protected $column = []; + protected $global = []; protected $order = []; public function setArray(array $array) { @@ -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; + } } } diff --git a/src/Adapters/ResultSet.php b/src/Adapters/ResultSet.php index 9513b92..ef6dfa0 100644 --- a/src/Adapters/ResultSet.php +++ b/src/Adapters/ResultSet.php @@ -6,7 +6,8 @@ class ResultSet extends AdapterInterface { protected $resultSet; - protected $filter = []; + protected $column = []; + protected $global = []; protected $order = []; public function getResponse() { @@ -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; + } } }