add ListEloquentTable

This commit is contained in:
Билай Станислав 2024-08-06 16:13:19 +03:00
parent 1316f68469
commit 22898f4c07
3 changed files with 86 additions and 5 deletions

View File

@ -34,14 +34,29 @@ $capsule->bootEloquent();
$schema = $capsule->schema();
$dataProvider = new EloquentDataProvider(\Itguild\EloquentTable\models\User::class, [
//$dataProvider = new EloquentDataProvider(\Itguild\EloquentTable\models\User::class, [
// 'currentPage' => 1,
// 'perPage' => 3,
// 'params' => ["class" => "table table-bordered", "border" => "2"],
// 'baseUrl' => "/admin/user",
//]);
//$table = new ListJsonTable($dataProvider->getJson());
//
//$table->addAction(\Itguild\Tables\ActionColumn\EditActionColumn::class);
//$table->create();
//$table->render();
$table = new \Itguild\EloquentTable\ListEloquentTable([
'model' => \Itguild\EloquentTable\models\User::class,
'currentPage' => 1,
'perPage' => 3,
'params' => ["class" => "table table-bordered", "border" => "2"],
'baseUrl' => "/admin/user",
'actions' => [
\Itguild\Tables\ActionColumn\ViewActionColumn::class,
\Itguild\Tables\ActionColumn\EditActionColumn::class
]
]);
$table = new ListJsonTable($dataProvider->getJson());
$table->addAction(\Itguild\Tables\ActionColumn\EditActionColumn::class);
$table->create();
$table->render();

View File

@ -30,8 +30,8 @@ class EloquentDataProvider
$this->queryBuilder = $source::query();
$model = new $source();
} elseif (is_object($source)) {
echo "<pre>";
print_r($source);die();
// echo "<pre>";
// print_r($source);die();
$this->queryBuilder = $source;
$model = $source->getModel();
} else {

66
src/ListEloquentTable.php Normal file
View File

@ -0,0 +1,66 @@
<?php
namespace Itguild\EloquentTable;
use Exception;
use Itguild\Tables\ListJsonTable;
class ListEloquentTable
{
private array $options;
private string $model;
private ListJsonTable $table;
private EloquentDataProvider $dataProvider;
// public int $currentPage;
// public int $perPage;
// public array $params;
// public string $baseUrl;
// public array $actions;
/**
* @throws Exception
*/
public function __construct(array $options)
{
if (!isset ($options['model'])) {
throw new Exception('Model option cannot be empty');
}
$this->model = $options['model'];
// $this->currentPage = $options['currentPage'] ?? 1;
// $this->perPage = $options['perPage'] ?? 5;
// $this->params = $options['params'] ?? '';
// $this->baseUrl = $options['base_url'] ?? '';
$this->options['currentPage'] = $options['currentPage'] ?? 1;
$this->options['perPage'] = $options['perPage'] ?? 5;
$this->options['params'] = $options['params'] ?? [];
$this->options['baseUrl'] = $options['baseUrl'] ?? '';
$this->options['actions'] = $options['actions'] ?? [];
$this->dataProvider = new EloquentDataProvider($this->model, $this->options);
$this->table = new ListJsonTable($this->dataProvider->getJson());
}
/**
* @throws Exception
*/
public function create(): void
{
if (isset($this->options['actions']))
{
$this->getActions();
}
$this->table->create();
}
private function getActions(): void
{
foreach ($this->options['actions'] as $action)
{
$this->table->addAction($action);
}
}
public function render(): void
{
$this->table->render();
}
}