Skip to content
This repository has been archived by the owner on Dec 9, 2022. It is now read-only.

Commit

Permalink
For implementing columnar filtering: added a function to check if any…
Browse files Browse the repository at this point in the history
… of the sSearch fields exist, added sSearch_x fields to where query. TODO: implement for containable behavior
  • Loading branch information
Jake Zieve committed Jun 24, 2014
1 parent 997dae3 commit 585c5ba
Showing 1 changed file with 46 additions and 28 deletions.
74 changes: 46 additions & 28 deletions Controller/Component/DataTableComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,25 @@
* @package DataTableComponent
* @link http://www.datatables.net/release-datatables/examples/server_side/server_side.html parts of code borrowed from dataTables example
* @since version 1.2.1
Copyright (c) 2013 Chris Nizzardini
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
* Copyright (c) 2013 Chris Nizzardini
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
class DataTableComponent extends Component{

Expand All @@ -46,6 +46,20 @@ public function initialize(Controller $controller){
$this->model = $this->controller->{$modelName};
}

/**
* returns true if get request has any searching conditions
* @param object $httpGet
* @return bool
*/
public function whereConditions($httpGet){
foreach($httpGet as $request){
if(strpos($request,'sSearch') === false && !empty($request)) {
return true;
}
}
return false;
}

/**
* returns dataTables compatible array - just json_encode the resulting aray
* @param object $controller optional
Expand Down Expand Up @@ -95,9 +109,8 @@ public function getResponse($controller = null, $model=null){
}

// check for WHERE statement in GET request
if(isset($httpGet) && !empty($httpGet['sSearch'])){
if(isset($httpGet) && $this->whereConditions($httpGet)){
$conditions = $this->getWhereConditions();

if( !empty($this->controller->paginate['contain']) ){
$this->controller->paginate = array_merge_recursive($this->controller->paginate, array('contain'=>$conditions));
}
Expand Down Expand Up @@ -133,7 +146,6 @@ public function getResponse($controller = null, $model=null){

// execute sql select
$data = $this->model->find('all', $parameters);

$this->setTimes('Find','stop');
$this->setTimes('Response','start','Formatting of response');
// dataTables compatible array
Expand Down Expand Up @@ -229,14 +241,21 @@ private function getWhereConditions(){
}

foreach($fields as $x => $column){

// only create conditions on bSearchable fields
if( $this->controller->request->query['bSearchable_'.$x] == 'true' ){

if($this->mDataProp == true){
$conditions['OR'][] = array(
$this->controller->request->query['mDataProp_'.$x].' LIKE' => '%'.$this->controller->request->query['sSearch'].'%'
);
if (!empty($this->controller->request->query['sSearch'])){
$conditions['OR'][] = array(
$this->controller->request->query['mDataProp_'.$x].' LIKE' => '%'.$this->controller->request->query['sSearch'].'%'
);
}
//check if specific column (i.e. sSearch_x) is empty, add it to the query if so
if(!empty($this->controller->request->query['sSearch_'.$x])){
$conditions['OR'][] = array(
$this->controller->request->query['mDataProp_'.$x].' LIKE' => '%'.$this->controller->request->query['sSearch_'.$x].'%'
);
}

}
else{

Expand All @@ -255,7 +274,6 @@ private function getWhereConditions(){
}
}
else{

if( !empty($this->controller->paginate['contain']) ){
if(array_key_exists($table, $this->controller->paginate['contain']) && in_array($field, $this->controller->paginate['contain'][$table]['fields'])){
$conditions[$table]['conditions'][] = $column.' LIKE "%'.$this->controller->request->query['sSearch'].'%"';
Expand Down

3 comments on commit 585c5ba

@cnizzardini
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you provide an example of how this feature is used? Specifically I am interested in what the datatables js code looks like. I was not aware (until now) that I could limit string searches to specific columns. Please show example code.

@jjzieve
Copy link

@jjzieve jjzieve commented on 585c5ba Jul 2, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup, here is the documentation -> http://datatables.net/examples/api/multi_filter.html. I just checked the console for the ajax calls and looked at the parameters to see the "sSearch_i" fields. Though for some reason I don't see anything in the console for the ajax calls on the datatables website, if you check your own examples you should see it.

@cnizzardini
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. This has been merged into the dev branch. It will need some more testing before going into master.

Please sign in to comment.