Skip to content

Commit

Permalink
version 0.4.0
Browse files Browse the repository at this point in the history
- cleaner code
- new feature postQuery
  • Loading branch information
hermawanramadhan committed May 5, 2021
1 parent 7fcb957 commit 062adcb
Show file tree
Hide file tree
Showing 6 changed files with 685 additions and 571 deletions.
14 changes: 14 additions & 0 deletions src/Column.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php
namespace Hermawan\DataTables;

class Column{

public $key;
public $alias;
public $type = 'column';
public $callback;
public $searchable = TRUE;
public $orderable = TRUE;

}

180 changes: 169 additions & 11 deletions src/DataTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,35 +6,193 @@
class DataTable
{

/**
* DataTableQuery object.
*
* @var \Hermawan\CodeIgniter4-DataTable\DataTableQuery
*/
private $query;


/**
* DataTablColumns object.
*
* @var \Hermawan\CodeIgniter4-DataTable\DataTableColumns
*/
private $columnDefs;


/**
* Builder from CodeIgniter Query Builder
* @param Builder $builder
*/
public function __construct($builder)
{
$this->query = new DataTableQuery($builder);
$this->columnDefs = new DataTableColumnDefs($builder);

return $this;
}

/**
* Make a DataTable instance from builder.
*
* Builder from CodeIgniter Query Builder
* @param Builder $builder
* @return DataTableServerSide
*/
public static function of($builder)
{
return new DataTableServerSide($builder);
return new self($builder);
}

/**
* postQuery
* @param Closure $postQuery
*/
public function postQuery($postQuery)
{
$this->query->postQuery($postQuery);
return $this;
}

/**
* Get DataTable Request
*
* @param String $requestName
* @return String|Array
* custom Filter
* @param Closure function
*/
public function filter($filterFunction)
{
$this->query->filter($filterFunction);
return $this;
}


public static function request($requestName = NULL)
/**
* Add numbering to first column
* @param String $column
*/
public function addNumbering($column = NULL)
{
$this->columnDefs->addNumbering($column);
return $this;
}


/**
* Add extra column
*
* @param String $column
* @param Closure $callback
* @param String|int $position
*/
public function add($column, $callback, $position = 'last')
{
$request = Services::request();
if($requestName !== NULL)
return $request->getGetPost($requestName);
$this->columnDefs->add($column, $callback, $position);
return $this;
}

/**
* Edit column
*
* @param String $column
* @param Closure $callback
*/
public function edit($column, $callback)
{
$this->columnDefs->edit($column, $callback);
return $this;
}

/**
* Format column
*
* @param String $column
* @param Closure $callback
*/
public function format($column, $callback)
{
$this->columnDefs->format($column, $callback);
return $this;
}

/**
* Hide column
*
* @param String $column
*/
public function hide($column)
{
$this->columnDefs->remove($column);
return $this;
}

/**
* Set Searchable columns
* @param String|Array
*/
public function setSearchableColumns($columns)
{
$this->columnDefs->setSearchable($columns);
return $this;
}

/**
* Add Searchable columns
* @param String|Array
*/
public function addSearchableColumns($columns)
{
$this->columnDefs->addSearchable($columns);
return $this;
}



/**
* Return JSON output
*
* @param bool $returnAsObject
* @return JSON
*/
public function toJson($returnAsObject = NULL)
{

if(! Request::get('draw'))
{
return self::throwError('no datatable request detected');
}

if($returnAsObject !== NULL)
$this->columnDefs->returnAsObject($returnAsObject);

$this->query->setColumnDefs($this->columnDefs);

return (Object) (($request->getMethod() == 'get') ? $request->getGet() : $request->getPost());
$response = Services::response();

return $response->setJSON([
'draw' => Request::get('draw'),
'recordsTotal' => $this->query->countAll(),
'recordsFiltered' => $this->query->countFiltered(),
'data' => $this->query->getDataResult(),

]);
}


/**
* Throw Error
*
* @param String $message
* @return Error
*/
public static function throwError($message)
{
$response = Services::response();
return $response->setJSON([
'error' => $message,

]);
}



} // End of DataTables Library Class.
Loading

0 comments on commit 062adcb

Please sign in to comment.