Plugin to help on datables creation
This commit is contained in:
@@ -0,0 +1,110 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Core\Datatables;
|
||||||
|
|
||||||
|
class Datatables {
|
||||||
|
|
||||||
|
private $classObject;
|
||||||
|
|
||||||
|
public function __construct($objectType) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function getTable($class, $baseUrl, $list = true){
|
||||||
|
|
||||||
|
$header = "";
|
||||||
|
$headerItems = $class::_listable;
|
||||||
|
|
||||||
|
// If the class do not have a _datatables property.
|
||||||
|
// Avoid it from dying
|
||||||
|
try{
|
||||||
|
$headerItems = $class::_datatables;
|
||||||
|
}catch(\Error $e){
|
||||||
|
$headerItems = $class::_listable;
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach($headerItems as $element){
|
||||||
|
$header .= "<th data-field='$element' ";
|
||||||
|
if(!in_array($element, $class::_orderable)){
|
||||||
|
$header .= "data-orderable='false'";
|
||||||
|
}
|
||||||
|
$header .= ">".\Lang::getString($element, $baseUrl)."</th>\n";
|
||||||
|
}
|
||||||
|
$content = "";
|
||||||
|
|
||||||
|
if($list){
|
||||||
|
$content = <<<EOL
|
||||||
|
<table class='table datatable' data-src="/$baseUrl/table?selective=true" style="width: 100%">
|
||||||
|
<thead>
|
||||||
|
<tr>
|
||||||
|
$header
|
||||||
|
</tr>
|
||||||
|
</thead>
|
||||||
|
</table>
|
||||||
|
EOL;
|
||||||
|
}else{
|
||||||
|
$content = <<<EOL
|
||||||
|
<table class='table datatable' data-src="/$baseUrl/table?selective=false" style="width: 100%">
|
||||||
|
<thead>
|
||||||
|
<tr>
|
||||||
|
$header
|
||||||
|
</tr>
|
||||||
|
</thead>
|
||||||
|
</table>
|
||||||
|
EOL;
|
||||||
|
}
|
||||||
|
return $content;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function getActionMenu($id, $urlBase, $actions = []) {
|
||||||
|
|
||||||
|
$content = "";
|
||||||
|
|
||||||
|
if($actions){
|
||||||
|
$content = <<<EOL
|
||||||
|
<div class="dropdown">
|
||||||
|
<button class="btn btn-secondary dropdown-toggle btn-sm" type="button" data-bs-toggle="dropdown" aria-expanded="false">
|
||||||
|
Ações
|
||||||
|
</button>
|
||||||
|
<ul class="dropdown-menu">
|
||||||
|
EOL;
|
||||||
|
foreach($actions as $key => $action){
|
||||||
|
$content .= self::formatMenuItem($id, $urlBase, $key, $action, \Lang::getString($key, $urlBase));
|
||||||
|
}
|
||||||
|
$content.= <<<EOL
|
||||||
|
</ul>
|
||||||
|
</div>
|
||||||
|
EOL;
|
||||||
|
|
||||||
|
}else{
|
||||||
|
$content = <<<EOL
|
||||||
|
<div class="dropdown">
|
||||||
|
<button class="btn btn-secondary dropdown-toggle btn-sm" type="button" data-bs-toggle="dropdown" aria-expanded="false">
|
||||||
|
Ações
|
||||||
|
</button>
|
||||||
|
<ul class="dropdown-menu">
|
||||||
|
<li><a class="dropdown-item" href="$urlBase/$id/edit">Editar</a></li>
|
||||||
|
</ul>
|
||||||
|
</div>
|
||||||
|
EOL;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $content;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static function formatMenuItem($id, $urlBase, $key, $action, $label = ''){
|
||||||
|
$content = "<li><a class='dropdown-item' href='$action'>$label</a></li>";
|
||||||
|
$content = str_replace('{id}', $id, $content);
|
||||||
|
$content = str_replace('{base}', $urlBase, $content);
|
||||||
|
$content = str_replace('{action}', $key, $content);
|
||||||
|
return $content;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function getSelectMenu($id) {
|
||||||
|
$content = <<<EOL
|
||||||
|
<button class="btn btn-secondary btn-sm" data-id='$id'> Selecionar </button>
|
||||||
|
EOL;
|
||||||
|
return $content;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Core\Datatables;
|
||||||
|
|
||||||
|
class DatatablesController{
|
||||||
|
|
||||||
|
public static function searchTable(){
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Core\Datatables;
|
||||||
|
|
||||||
|
interface DatatablesListable{
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,2 @@
|
|||||||
|
<?php
|
||||||
|
$lang["datatables"]["module_name"] = "Datatables";
|
||||||
@@ -0,0 +1,2 @@
|
|||||||
|
<?php
|
||||||
|
$lang["datatables"]["module_name"] = "Datatables";
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use Routes\RouteCollection as RouteCollection;
|
||||||
|
|
||||||
|
RouteCollection::group("/datatables", function(){
|
||||||
|
RouteCollection::get("/table", "\App\Core\Datatables\DatatablesController@searchTable")->middlewareIgnore('auth');
|
||||||
|
RouteCollection::get("/tableData", "\App\Core\Datatables\DatatablesController@searchTable")->middlewareIgnore('auth');
|
||||||
|
});
|
||||||
@@ -0,0 +1,4 @@
|
|||||||
|
<a href='/datatables/form' >Adicionar </a>
|
||||||
|
<div class="table-responsive">
|
||||||
|
|
||||||
|
</div>
|
||||||
Reference in New Issue
Block a user