list eloquent table
This commit is contained in:
parent
22898f4c07
commit
a14f398532
32
index.php
32
index.php
@ -3,6 +3,9 @@ use Illuminate\Database\Capsule\Manager as Capsule;
|
||||
use Illuminate\Database\Schema\Builder;
|
||||
use Illuminate\Container\Container;
|
||||
use Itguild\EloquentTable\EloquentDataProvider;
|
||||
use Itguild\EloquentTable\ListEloquentTable;
|
||||
use Itguild\EloquentTable\models\User;
|
||||
use Itguild\Tables\ActionColumn\EditActionColumn;
|
||||
use Itguild\Tables\ListJsonTable;
|
||||
|
||||
ini_set("display_errors", true);
|
||||
@ -34,29 +37,16 @@ $capsule->bootEloquent();
|
||||
|
||||
$schema = $capsule->schema();
|
||||
|
||||
//$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,
|
||||
try {
|
||||
$table = new ListEloquentTable(new EloquentDataProvider(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
|
||||
]
|
||||
]);
|
||||
|
||||
'baseUrl' => "/admin/user"
|
||||
]));
|
||||
$table->addAction(EditActionColumn::class);
|
||||
$table->create();
|
||||
$table->render();
|
||||
} catch (Exception $e) {
|
||||
}
|
||||
|
||||
|
@ -30,8 +30,6 @@ class EloquentDataProvider
|
||||
$this->queryBuilder = $source::query();
|
||||
$model = new $source();
|
||||
} elseif (is_object($source)) {
|
||||
// echo "<pre>";
|
||||
// print_r($source);die();
|
||||
$this->queryBuilder = $source;
|
||||
$model = $source->getModel();
|
||||
} else {
|
||||
|
@ -3,64 +3,20 @@
|
||||
namespace Itguild\EloquentTable;
|
||||
use Exception;
|
||||
use Itguild\Tables\ListJsonTable;
|
||||
use JetBrains\PhpStorm\NoReturn;
|
||||
|
||||
class ListEloquentTable
|
||||
class ListEloquentTable extends ListJsonTable
|
||||
{
|
||||
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)
|
||||
#[NoReturn] public function __construct(EloquentDataProvider $dataProvider)
|
||||
{
|
||||
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();
|
||||
$this->dataProvider = $dataProvider;
|
||||
parent::__construct($this->dataProvider->getJson());
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user