Created
September 19, 2016 17:38
-
-
Save greabock/ed4663d9c78a82ec92cfe8b218f70028 to your computer and use it in GitHub Desktop.
datatables
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
<?php | |
namespace App\Backend\Http\Datatables; | |
use Datatables; | |
abstract class AbstractTable | |
{ | |
/** | |
* @var Builder | |
*/ | |
protected $html; | |
/** | |
* @var bool | |
*/ | |
protected $isBuilt; | |
/** | |
* @var \Illuminate\Database\Query\Builder | |
*/ | |
protected $query; | |
/** | |
* AbstractTable конструктор. | |
* | |
* @param Builder $html | |
* | |
*/ | |
public function __construct(Builder $html) | |
{ | |
$this->html = $html; | |
$this->html->parameters([ | |
'language' => trans('datatables'), | |
]); | |
} | |
/** | |
* Возвращает html-код таблицы. | |
* | |
* @param array $attributes | |
* @return string | |
*/ | |
public function show(array $attributes = []) | |
{ | |
$this->buildIfNotBuilt(); | |
return $this->html->table($attributes); | |
} | |
public function clientSide() | |
{ | |
$this->html->parameters([ | |
'serverSide' => false, | |
]); | |
return $this; | |
} | |
/** | |
* Устанавливает url источника данных для таблицы. | |
* (по умолчанию, это текущий url). | |
* @param string $attr | |
* @return $this | |
*/ | |
public function ajax($attr) | |
{ | |
$this->html->ajax($attr); | |
return $this; | |
} | |
/** | |
* Возвращет javascript-код инициализации таблицы. | |
* | |
* @param null $id | |
* | |
* @return string | |
*/ | |
public function script($id = null) | |
{ | |
$this->buildIfNotBuilt(); | |
return $this->html->scripts(null, ['type' => 'text/javascript'], $id); | |
} | |
/** | |
* Строит таблицу, если она еще не построена. | |
* | |
* @return void | |
*/ | |
public function buildIfNotBuilt() | |
{ | |
if (!$this->isBuilt()) { | |
$this->build(); | |
} | |
} | |
/** | |
* Проверяет построена ли данная таблица. | |
* | |
* @return bool | |
*/ | |
private function isBuilt() | |
{ | |
return $this->isBuilt; | |
} | |
/** | |
* Устанавливет флаг "построен" в истинное значение. | |
* | |
*/ | |
private function setBuilt() | |
{ | |
$this->isBuilt = true; | |
} | |
/** | |
* Строит таблицу. | |
* | |
* @return void | |
*/ | |
private function build() | |
{ | |
foreach ($this->getTableColumns() as $column) { | |
$this->html->addColumn($column); | |
} | |
$this->setBuilt(); | |
} | |
/** | |
* Возвращает данные для наполнения таблицы. | |
* | |
* @return \Illuminate\Http\JsonResponse | |
*/ | |
public function data() | |
{ | |
return $this->addColumns( | |
$this->morphColumns( | |
Datatables::of( | |
$this->prepareQuery() | |
)))->make(true); | |
} | |
/** | |
* Запускает все модификаторы колонок. | |
* | |
* @param object $table | |
* @return object | |
*/ | |
private function morphColumns($table) | |
{ | |
foreach ($this->getColumnMutators() as $column => $editor) { | |
$table->editColumn($column, $editor); | |
} | |
return $table; | |
} | |
/** | |
* Добавляет дополнительные колнки. | |
* | |
* @param object $table | |
* @return object | |
*/ | |
private function addColumns($table) | |
{ | |
foreach ($this->getColumnCreators() as $title => $creator) { | |
$table->addColumn($title, $creator); | |
} | |
return $table; | |
} | |
/** | |
* Устанавливает поля таблицы бд для выборки. | |
* | |
* @return \Illuminate\Database\Query\Builder | |
*/ | |
protected function prepareQuery() | |
{ | |
$query = $this->query()->select($this->getDatabaseFields()); | |
foreach ($this->getJoins() as $join) { | |
call_user_func_array([$query, array_shift($join)], $join); | |
} | |
return $query; | |
} | |
/** | |
* Возвращает массив модификаторов join. | |
* | |
* ['join'|'leftJoin'|'rightJoin', 'table_name', 'первый.операнд', 'оператор сопоставления', 'второй.операнд'] | |
* и/или | |
* ['join'|'leftJoin'|'rightJoin', 'table_name', $замыкание($join)] | |
* | |
* @see https://laravel.com/docs/5.2/queries#joins | |
*/ | |
protected function getJoins() | |
{ | |
return []; | |
} | |
/** | |
* @return \Illuminate\Database\Query\Builder | |
*/ | |
public function query() | |
{ | |
return $this->query ?: $this->query = $this->getQuery(); | |
} | |
/** | |
* Возвращает список полей для получения из бд. | |
* ['поле_1', 'поле_2', DB::raw('выражение поля 3'), 'поле_4'] | |
* | |
* @return array | |
*/ | |
protected function getDatabaseFields() | |
{ | |
return ['*']; | |
} | |
/** | |
* Возвращает список создатедей (замыканий) дополнительных колонок. | |
* | |
* @return array | callable[]; | |
* | |
* | |
*/ | |
protected function getColumnCreators() | |
{ | |
return []; | |
} | |
public function attributes($data) | |
{ | |
$this->html->attributes($data); | |
return $this; | |
} | |
/** | |
* Возвращает список замыканий конфигурирующих поля. | |
* ['название_поля' => $замыкание] | |
* | |
* @return array|callable[] | |
*/ | |
protected function getColumnMutators() | |
{ | |
return []; | |
} | |
/** | |
* Возвращает сконфигурированный список колонок таблицы. | |
* | |
* @return array | |
*/ | |
abstract protected function getTableColumns(); | |
/** | |
* Возвращает новый объект запроса в бд, для конфигурации. | |
* | |
* @return \Illuminate\Database\Query\Builder | |
*/ | |
abstract protected function getQuery(); | |
} |
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
<?php | |
namespace App\Backend\Http\Datatables; | |
use Yajra\Datatables\Html\Builder as BaseBuilder; | |
class Builder extends BaseBuilder | |
{ | |
public function scripts($script = null, array $attributes = ['type' => 'text/javascript'], $id = null) | |
{ | |
if( ! is_null($id)) | |
{ | |
$this->tableAttributes['id'] = $id; | |
} | |
$script = $script ?: $this->generateScripts(); | |
return '<script' . $this->html->attributes($attributes) . '>' . $script . '</script>' . PHP_EOL; | |
} | |
public function attributes(array $attributes = []) | |
{ | |
$this->attributes = array_merge($this->attributes,$attributes); | |
return $this; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Пример реализации