-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
68 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,10 +4,10 @@ | |
* | ||
* TablesIgniter 基於 CodeIgniter4 。它將可以幫助你在 使用 server side mode 中使用 jQuery Datatables。 | ||
* TablesIgniter based on CodeIgniter4. This library will help you use jQuery Datatables in server side mode. | ||
* @package CodeIgniter | ||
* @package CodeIgniter4 | ||
* @subpackage libraries | ||
* @category library | ||
* @version 1.0.0 | ||
* @version 1.1.0 | ||
* @author monkenWu <[email protected]> | ||
* @link https://github.com/monkenWu/TablesIgniter | ||
* | ||
|
@@ -17,32 +17,56 @@ | |
|
||
class TablesIgniter{ | ||
|
||
protected $db; | ||
protected $builder; | ||
protected $outputColumn; | ||
protected $defaultOrder = []; | ||
protected $searchLike = false; | ||
protected $searchLike = []; | ||
protected $order = []; | ||
|
||
public function __construct(&$db){ | ||
$this->db =& $db; | ||
public function __construct(array $init = []){ | ||
if(!empty($init)){ | ||
if(isset($init["setTable"])) | ||
$this->setTable($init["setTable"]); | ||
if(isset($init["setOutput"])) | ||
$this->setOutput($init["setOutput"]); | ||
if(isset($init["setDefaultOrder"])){ | ||
foreach ($init["setDefaultOrder"] as $value) { | ||
$this->setDefaultOrder($value[0],$value[1]); | ||
} | ||
} | ||
if(isset($init["setSearch"])) | ||
$this->setSearch($init["setSearch"]); | ||
if(isset($init["setOrder"])) | ||
$this->setOrder($init["setOrder"]); | ||
} | ||
} | ||
|
||
public function setTable(Closure $fun){ | ||
$this->builder = $fun($this->db); | ||
/** | ||
* 設定欄位 | ||
*/ | ||
public function setTable($builder){ | ||
$this->builder = clone $builder; | ||
return $this; | ||
} | ||
|
||
/** | ||
* 設定參與搜索欄位 | ||
*/ | ||
public function setSearch(array $like){ | ||
$this->searchLike = $like; | ||
return $this; | ||
} | ||
|
||
/** | ||
* 設定排序項目 | ||
*/ | ||
public function setOrder(array $order){ | ||
$this->order = $order; | ||
return $this; | ||
} | ||
|
||
/** | ||
* 設定預設排序項目 | ||
* | ||
* @param string $item | ||
* @param string $type | ||
* @return mixed | ||
*/ | ||
public function setDefaultOrder($item,$type="ASC"){ | ||
$this->defaultOrder[] = array($item, $type); | ||
|
@@ -51,30 +75,31 @@ public function setDefaultOrder($item,$type="ASC"){ | |
|
||
/** | ||
* 設定實際輸出的序列 | ||
* @param array $columns | ||
* @return mixed | ||
*/ | ||
public function setOutput(array $column){ | ||
$this->outputColumn = $column; | ||
return $this; | ||
} | ||
|
||
private function getBuilder(){ | ||
return clone $this->builder; | ||
} | ||
|
||
/** | ||
* 搜索總筆數 | ||
*/ | ||
private function getFiltered(){ | ||
$bui = $this->extraConfig($this->builder); | ||
$query = $bui->countAll(); | ||
$bui = $this->extraConfig($this->getBuilder()); | ||
$query = $bui->countAllResults(); | ||
return $query; | ||
} | ||
|
||
/** | ||
* 總筆數 | ||
*/ | ||
private function getTotal(){ | ||
//$this->extra_config(); | ||
$bui = $this->builder; | ||
$query = $bui->countAllResults(); | ||
$bui = $this->getBuilder(); | ||
$query = $bui->countAllResults(); | ||
return $query; | ||
} | ||
|
||
|
@@ -84,22 +109,21 @@ private function getTotal(){ | |
* @return array | ||
*/ | ||
private function getQuery(){ | ||
$bui = $this->extraConfig($this->builder); | ||
$bui = $this->extraConfig($this->getBuilder()); | ||
if(isset($_POST["length"])){ | ||
if($_POST["length"] != -1) { | ||
$bui->limit($_POST['length'], $_POST['start']); | ||
} | ||
} | ||
//print_r($bui); | ||
$query = $bui->get(); | ||
//print_r($this->getBuilder()); | ||
return $query; | ||
} | ||
|
||
/** | ||
* 合成每列資料的內容。 | ||
* | ||
* @param array $row | ||
* @return array | ||
*/ | ||
*/ | ||
private function getOutputData($row){ | ||
$subArray = array(); | ||
foreach ($this->outputColumn as $colKey => $data) { | ||
|
@@ -112,17 +136,33 @@ private function getOutputData($row){ | |
return $subArray; | ||
} | ||
|
||
/** | ||
* 查詢是否有有排序或搜索的要求 | ||
/** | ||
* 查詢是否有排序或搜索的要求 | ||
*/ | ||
private function extraConfig($bui){ | ||
if(!empty($_POST["search"]["value"])){ | ||
foreach ($this->searchLike as $field) { | ||
$bui->orLike($field,$_POST["search"]["value"]); | ||
} | ||
} | ||
if(isset($_POST["order"])){ | ||
$bui->orderby($_POST['order']['0']['column'], $_POST['order']['0']['dir']); | ||
if(isset($_POST["order"])){ | ||
if(!empty($this->order)){ | ||
if($this->order[$_POST['order']['0']['column']] != null){ | ||
$bui->orderby($this->order[$_POST['order']['0']['column']], $_POST['order']['0']['dir']); | ||
}else{ | ||
if(count($this->defaultOrder)!=0){ | ||
foreach ($this->defaultOrder as $value) { | ||
$bui->orderby($value[0], $value[1]); | ||
} | ||
} | ||
} | ||
}else{ | ||
if(count($this->defaultOrder)!=0){ | ||
foreach ($this->defaultOrder as $value) { | ||
$bui->orderby($value[0], $value[1]); | ||
} | ||
} | ||
} | ||
}else{ | ||
if(count($this->defaultOrder)!=0){ | ||
foreach ($this->defaultOrder as $value) { | ||
|
@@ -135,12 +175,10 @@ private function extraConfig($bui){ | |
|
||
/** | ||
* 取得完整的Datatable Json字串 | ||
* @return string | ||
*/ | ||
public function getDatatable(){ | ||
if($result = $this->getQuery()){ | ||
$data = array(); | ||
//print_r($result->getResult('array')); | ||
foreach ($result->getResult('array') as $row){ | ||
$data[] = $this->getOutputData($row); | ||
} | ||
|