Skip to content

Commit

Permalink
added search single column support on datatable
Browse files Browse the repository at this point in the history
  • Loading branch information
emagombe committed Oct 2, 2020
1 parent e8a3a66 commit 315b19e
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 35 deletions.
73 changes: 40 additions & 33 deletions src/DataTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ public static function create($result) {
/* Print the response with app/json header */
public function stream() {
header("Content-type: Application/json");
http_response_code(200);
echo $this->build();
}

Expand Down Expand Up @@ -116,6 +115,7 @@ public function build() {
if($params["search"]["value"] != "") {
$this->db_result = $this->search($this->db_result);
}
$this->db_result = $this->search_column($this->db_result);

/* Updating filtered records number */
$response["recordsFiltered"] = count($this->db_result);
Expand All @@ -142,37 +142,6 @@ public function addColumn($column_name, $callback) {
return $this;
}

public function searchColumn($column_name, $query) {
$params = $this->getParams();

$request_columns = $params["columns"];

$filtered_result = [];
foreach($this->db_result as $index => $row) {
/* Filter columns */
foreach($row as $key => $value) {
$break = false;
/* Check if column is in the table and is searchable */
foreach($params["columns"] as $column) {
if(strtolower($column["data"]) == strtolower($column_name) && $column["searchable"] == "true") {
/* Check if contains search key */
if(strpos($value, $query) !== false) {
$filtered_result[] = $row;
$break = true;
break;
}
}
}
/* Breaking if key word found */
if($break) {
break;
}
}
}
$this->db_result = $filtered_result;
return $this;
}

/* Check if it's a select query */
private function isSelect($string) {
if(strtoupper(substr($string, 0, strlen("SELECT"))) === "SELECT") {
Expand Down Expand Up @@ -261,4 +230,42 @@ private function search($result) {
}
return $filtered_result;
}
}

private function search_column($result) {
$params = $this->getParams();

$filtered_result = [];

foreach($result as $index => $row) {
/* Check if there is search values */
$has_search_values = false;
foreach($params["columns"] as $column) {
if($column["searchable"] == "true" && isset($column["search"]["value"]) && $column["search"]["value"]) {
$has_search_values = true;
break;
}
}
if(!$has_search_values) {
return $result;
}

$found = false;
foreach($params["columns"] as $column) {
if($column["searchable"] == "true" && isset($row[$column["data"]]) && $row[$column["data"]]) {
if($column["search"]["value"]) {
if(strpos($row[$column["data"]], $column["search"]["value"]) !== false) {
$found = true;
} else {
$found = false;
break;
}
}
}
}
if($found) {
$filtered_result[] = $row;
}
}
return $filtered_result;
}
}
5 changes: 3 additions & 2 deletions test/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,12 @@ <h1>1527 Music gener table</h1>
url: "test.php",
method: "post",
data: function() {
var data = [];
var data = {};
that = this;
$(".columns-search").each(function() {
that[$(this).attr("name")] = $(this).val();
data[$(this).attr("name")] = $(this).val();
});
return data;
},
},
serverMethod: "post",
Expand Down

0 comments on commit 315b19e

Please sign in to comment.