add Eloquent Tables

This commit is contained in:
Билай Станислав 2024-08-07 15:30:45 +03:00
parent be8fea187b
commit 51e3613e40
11 changed files with 388 additions and 260 deletions

View File

@ -12,7 +12,8 @@
"itguild/tables": "^0.1.3", "itguild/tables": "^0.1.3",
"madesimple/php-form-validator": "^2.9", "madesimple/php-form-validator": "^2.9",
"itguild/forms": "^0.1.1", "itguild/forms": "^0.1.1",
"samejack/php-argv": "dev-master" "samejack/php-argv": "dev-master",
"itguild/eloquent-table": "^0.2.0"
}, },
"autoload": { "autoload": {
"psr-4": { "psr-4": {

101
composer.lock generated
View File

@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically" "This file is @generated automatically"
], ],
"content-hash": "05960b2d73dd4ee5a824e5a9416486b2", "content-hash": "f12e70f90d120b5b660f18c367fc7c7b",
"packages": [ "packages": [
{ {
"name": "brick/math", "name": "brick/math",
@ -337,16 +337,16 @@
}, },
{ {
"name": "illuminate/collections", "name": "illuminate/collections",
"version": "v11.19.0", "version": "v11.20.0",
"source": { "source": {
"type": "git", "type": "git",
"url": "https://github.com/illuminate/collections.git", "url": "https://github.com/illuminate/collections.git",
"reference": "2be24113fe25ef18be33bbd083ad36bf1e751eb5" "reference": "dc68a7ccad93f3c2baa0bc8f559431c06391aa75"
}, },
"dist": { "dist": {
"type": "zip", "type": "zip",
"url": "https://api.github.com/repos/illuminate/collections/zipball/2be24113fe25ef18be33bbd083ad36bf1e751eb5", "url": "https://api.github.com/repos/illuminate/collections/zipball/dc68a7ccad93f3c2baa0bc8f559431c06391aa75",
"reference": "2be24113fe25ef18be33bbd083ad36bf1e751eb5", "reference": "dc68a7ccad93f3c2baa0bc8f559431c06391aa75",
"shasum": "" "shasum": ""
}, },
"require": { "require": {
@ -388,11 +388,11 @@
"issues": "https://github.com/laravel/framework/issues", "issues": "https://github.com/laravel/framework/issues",
"source": "https://github.com/laravel/framework" "source": "https://github.com/laravel/framework"
}, },
"time": "2024-07-23T14:08:10+00:00" "time": "2024-08-01T18:54:27+00:00"
}, },
{ {
"name": "illuminate/conditionable", "name": "illuminate/conditionable",
"version": "v11.19.0", "version": "v11.20.0",
"source": { "source": {
"type": "git", "type": "git",
"url": "https://github.com/illuminate/conditionable.git", "url": "https://github.com/illuminate/conditionable.git",
@ -438,16 +438,16 @@
}, },
{ {
"name": "illuminate/container", "name": "illuminate/container",
"version": "v11.19.0", "version": "v11.20.0",
"source": { "source": {
"type": "git", "type": "git",
"url": "https://github.com/illuminate/container.git", "url": "https://github.com/illuminate/container.git",
"reference": "122c62229b209678013c0833793d7cf94d14bbbd" "reference": "f47be671981a4438257c4fbfc3ad257f4e3e929a"
}, },
"dist": { "dist": {
"type": "zip", "type": "zip",
"url": "https://api.github.com/repos/illuminate/container/zipball/122c62229b209678013c0833793d7cf94d14bbbd", "url": "https://api.github.com/repos/illuminate/container/zipball/f47be671981a4438257c4fbfc3ad257f4e3e929a",
"reference": "122c62229b209678013c0833793d7cf94d14bbbd", "reference": "f47be671981a4438257c4fbfc3ad257f4e3e929a",
"shasum": "" "shasum": ""
}, },
"require": { "require": {
@ -485,20 +485,20 @@
"issues": "https://github.com/laravel/framework/issues", "issues": "https://github.com/laravel/framework/issues",
"source": "https://github.com/laravel/framework" "source": "https://github.com/laravel/framework"
}, },
"time": "2024-07-26T06:12:27+00:00" "time": "2024-08-05T15:04:01+00:00"
}, },
{ {
"name": "illuminate/contracts", "name": "illuminate/contracts",
"version": "v11.19.0", "version": "v11.20.0",
"source": { "source": {
"type": "git", "type": "git",
"url": "https://github.com/illuminate/contracts.git", "url": "https://github.com/illuminate/contracts.git",
"reference": "ebe2b8d69b8fb1c07111e3500d464e77dfab3202" "reference": "34ead9385e0eab7e947807d77da66faf9bdf95ff"
}, },
"dist": { "dist": {
"type": "zip", "type": "zip",
"url": "https://api.github.com/repos/illuminate/contracts/zipball/ebe2b8d69b8fb1c07111e3500d464e77dfab3202", "url": "https://api.github.com/repos/illuminate/contracts/zipball/34ead9385e0eab7e947807d77da66faf9bdf95ff",
"reference": "ebe2b8d69b8fb1c07111e3500d464e77dfab3202", "reference": "34ead9385e0eab7e947807d77da66faf9bdf95ff",
"shasum": "" "shasum": ""
}, },
"require": { "require": {
@ -533,20 +533,20 @@
"issues": "https://github.com/laravel/framework/issues", "issues": "https://github.com/laravel/framework/issues",
"source": "https://github.com/laravel/framework" "source": "https://github.com/laravel/framework"
}, },
"time": "2024-07-29T06:48:51+00:00" "time": "2024-08-01T19:08:33+00:00"
}, },
{ {
"name": "illuminate/database", "name": "illuminate/database",
"version": "v11.19.0", "version": "v11.20.0",
"source": { "source": {
"type": "git", "type": "git",
"url": "https://github.com/illuminate/database.git", "url": "https://github.com/illuminate/database.git",
"reference": "db151e9a3221705cb4149c39dce2c51799708637" "reference": "dae4ab28c830a906a410a168d8ece8fb4ab7b2be"
}, },
"dist": { "dist": {
"type": "zip", "type": "zip",
"url": "https://api.github.com/repos/illuminate/database/zipball/db151e9a3221705cb4149c39dce2c51799708637", "url": "https://api.github.com/repos/illuminate/database/zipball/dae4ab28c830a906a410a168d8ece8fb4ab7b2be",
"reference": "db151e9a3221705cb4149c39dce2c51799708637", "reference": "dae4ab28c830a906a410a168d8ece8fb4ab7b2be",
"shasum": "" "shasum": ""
}, },
"require": { "require": {
@ -601,11 +601,11 @@
"issues": "https://github.com/laravel/framework/issues", "issues": "https://github.com/laravel/framework/issues",
"source": "https://github.com/laravel/framework" "source": "https://github.com/laravel/framework"
}, },
"time": "2024-07-30T07:00:12+00:00" "time": "2024-08-06T14:16:43+00:00"
}, },
{ {
"name": "illuminate/filesystem", "name": "illuminate/filesystem",
"version": "v11.19.0", "version": "v11.20.0",
"source": { "source": {
"type": "git", "type": "git",
"url": "https://github.com/illuminate/filesystem.git", "url": "https://github.com/illuminate/filesystem.git",
@ -672,7 +672,7 @@
}, },
{ {
"name": "illuminate/macroable", "name": "illuminate/macroable",
"version": "v11.19.0", "version": "v11.20.0",
"source": { "source": {
"type": "git", "type": "git",
"url": "https://github.com/illuminate/macroable.git", "url": "https://github.com/illuminate/macroable.git",
@ -718,16 +718,16 @@
}, },
{ {
"name": "illuminate/support", "name": "illuminate/support",
"version": "v11.19.0", "version": "v11.20.0",
"source": { "source": {
"type": "git", "type": "git",
"url": "https://github.com/illuminate/support.git", "url": "https://github.com/illuminate/support.git",
"reference": "f3c19ee61d875dca1045df2d90000ae97f1645f9" "reference": "40457b4694486e506bad1b5be6de8dc86afce46f"
}, },
"dist": { "dist": {
"type": "zip", "type": "zip",
"url": "https://api.github.com/repos/illuminate/support/zipball/f3c19ee61d875dca1045df2d90000ae97f1645f9", "url": "https://api.github.com/repos/illuminate/support/zipball/40457b4694486e506bad1b5be6de8dc86afce46f",
"reference": "f3c19ee61d875dca1045df2d90000ae97f1645f9", "reference": "40457b4694486e506bad1b5be6de8dc86afce46f",
"shasum": "" "shasum": ""
}, },
"require": { "require": {
@ -788,7 +788,42 @@
"issues": "https://github.com/laravel/framework/issues", "issues": "https://github.com/laravel/framework/issues",
"source": "https://github.com/laravel/framework" "source": "https://github.com/laravel/framework"
}, },
"time": "2024-07-30T06:57:25+00:00" "time": "2024-08-06T14:20:50+00:00"
},
{
"name": "itguild/eloquent-table",
"version": "0.2",
"source": {
"type": "git",
"url": "https://git.itguild.info/ItGuild/eloquent_table",
"reference": "59e065458d8db8690944751b0e2e8e394e250fe9"
},
"require": {
"itguild/tables": "^0.1.9"
},
"require-dev": {
"illuminate/database": "^12.0@dev",
"illuminate/filesystem": "^12.0@dev",
"twbs/bootstrap": "dev-main",
"vlucas/phpdotenv": "^5.6@dev"
},
"type": "library",
"autoload": {
"psr-4": {
"Itguild\\EloquentTable\\": "src/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Kavalar",
"email": "apuc06@mail.ru"
}
],
"time": "2024-08-07T10:27:11+00:00"
}, },
{ {
"name": "itguild/forms", "name": "itguild/forms",
@ -851,11 +886,11 @@
}, },
{ {
"name": "itguild/tables", "name": "itguild/tables",
"version": "0.1.8", "version": "0.1.13",
"source": { "source": {
"type": "git", "type": "git",
"url": "https://git.itguild.info/ItGuild/tables", "url": "https://git.itguild.info/ItGuild/tables",
"reference": "d6d703aaa6ea26005b029d1110b96b268ea30fe7" "reference": "d98ddded07131f602486783f5f016002ea7d7fb7"
}, },
"type": "library", "type": "library",
"autoload": { "autoload": {
@ -873,7 +908,7 @@
"email": "apuc06@mail.ru" "email": "apuc06@mail.ru"
} }
], ],
"time": "2024-07-31T09:48:26+00:00" "time": "2024-08-07T09:43:14+00:00"
}, },
{ {
"name": "madesimple/php-arrays", "name": "madesimple/php-arrays",
@ -2384,5 +2419,5 @@
"prefer-lowest": false, "prefer-lowest": false,
"platform": [], "platform": [],
"platform-dev": [], "platform-dev": [],
"plugin-api-version": "2.3.0" "plugin-api-version": "2.6.0"
} }

View File

@ -1,78 +1,78 @@
<?php <?php
//
namespace kernel\IGTabel; //namespace kernel\IGTabel;
//
use app\helpers\Debug; //use app\helpers\Debug;
use Exception; //use Exception;
use Illuminate\Database\Eloquent\Model; //use Illuminate\Database\Eloquent\Model;
//
class EloquentDataProvider //class EloquentDataProvider
{ //{
protected int $totalCount; // protected int $totalCount;
//
protected int $perPage = 10; // protected int $perPage = 10;
//
protected int $currentPage = 1; // protected int $currentPage = 1;
//
protected Model $model; // protected Model $model;
//
protected $queryBuilder = false; // protected $queryBuilder = false;
//
protected array $options = []; // protected array $options = [];
protected array $meta = []; // protected array $meta = [];
protected string $jsonStr = ''; // protected string $jsonStr = '';
//
/** // /**
* @throws Exception // * @throws Exception
*/ // */
public function __construct($source, array $options) // public function __construct($source, array $options)
{ // {
if (is_string($source)) { // if (is_string($source)) {
$this->queryBuilder = $source::query(); // $this->queryBuilder = $source::query();
$model = new $source(); // $model = new $source();
} elseif (is_object($source)) { // } elseif (is_object($source)) {
$this->queryBuilder = $source; // $this->queryBuilder = $source;
$model = $source->getModel(); // $model = $source->getModel();
} else { // } else {
throw new Exception(message: "source is not valid"); // throw new Exception(message: "source is not valid");
} // }
$this->options = $options; // $this->options = $options;
$this->currentPage = $this->options['currentPage'] ?? 1; // $this->currentPage = $this->options['currentPage'] ?? 1;
$this->perPage = $this->options['perPage'] ?? 10; // $this->perPage = $this->options['perPage'] ?? 10;
$this->meta['total'] = $model->count(); // $this->meta['total'] = $model->count();
$this->meta['totalWithFilters'] = $this->queryBuilder->count(); // $this->meta['totalWithFilters'] = $this->queryBuilder->count();
$this->meta['columns'] = $options['columns'] ?? $model->labels(); // $this->meta['columns'] = $options['columns'] ?? $model->labels();
$this->meta['perPage'] = $options['perPage'] ?? 10; // $this->meta['perPage'] = $options['perPage'] ?? 10;
$this->meta['currentPage'] = $options['currentPage'] ?? 1; // $this->meta['currentPage'] = $options['currentPage'] ?? 1;
$this->meta['baseUrl'] = $options['baseUrl'] ?? $model->table; // $this->meta['baseUrl'] = $options['baseUrl'] ?? $model->table;
$this->meta['params'] = $options['params'] ?? []; // $this->meta['params'] = $options['params'] ?? [];
$this->meta['actions'] = $options['actions'] ?? []; // $this->meta['actions'] = $options['actions'] ?? [];
$this->createQuery(); // $this->createQuery();
//
$this->jsonStr = (new JSONCreator($this->meta, $this->getCollection()->toArray()))->getJson(); // $this->jsonStr = (new JSONCreator($this->meta, $this->getCollection()->toArray()))->getJson();
//
} // }
//
public function createQuery(): void // public function createQuery(): void
{ // {
if ($this->currentPage > 1) { // if ($this->currentPage > 1) {
$this->queryBuilder->skip(($this->currentPage - 1) * $this->perPage)->take($this->perPage); // $this->queryBuilder->skip(($this->currentPage - 1) * $this->perPage)->take($this->perPage);
} else { // } else {
$this->queryBuilder->take($this->perPage); // $this->queryBuilder->take($this->perPage);
} // }
} // }
//
public function getCollection() // public function getCollection()
{ // {
return $this->queryBuilder->get(); // return $this->queryBuilder->get();
} // }
//
/** // /**
* @return string|null // * @return string|null
*/ // */
public function getJson(): string|null // public function getJson(): string|null
{ // {
return $this->jsonStr; // return $this->jsonStr;
} // }
//
} //}

View File

@ -1,40 +1,40 @@
<?php <?php
//
namespace kernel\IGTabel; //namespace kernel\IGTabel;
//
use app\helpers\Debug; //use app\helpers\Debug;
//
class JSONCreator //class JSONCreator
{ //{
protected array $informationArray = []; // protected array $informationArray = [];
//
public function __construct(array $meta, array $data) // public function __construct(array $meta, array $data)
{ // {
$params = empty($meta['params']) ? ["class" => "table table-bordered", "border" => "1"] : $meta['params']; // $params = empty($meta['params']) ? ["class" => "table table-bordered", "border" => "1"] : $meta['params'];
if ($meta) { // if ($meta) {
$this->informationArray = [ // $this->informationArray = [
"meta" => $meta, // "meta" => $meta,
"data" => $data ?? [] // "data" => $data ?? []
]; // ];
} // }
} // }
//
/** // /**
* @param array $infArr // * @param array $infArr
* @return string|null // * @return string|null
*/ // */
protected function toJson(array $infArr): ?string // protected function toJson(array $infArr): ?string
{ // {
if ($infArr) // if ($infArr)
return json_encode($infArr, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE); // return json_encode($infArr, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
return null; // return null;
} // }
//
/** // /**
* @return string|null // * @return string|null
*/ // */
public function getJson(): ?string // public function getJson(): ?string
{ // {
return $this->toJson($this->informationArray); // return $this->toJson($this->informationArray);
} // }
} //}

View File

@ -1,42 +1,42 @@
<?php <?php
//
namespace kernel\IGTabel; //namespace kernel\IGTabel;
//
use app\helpers\Debug; //use app\helpers\Debug;
use Illuminate\Database\Eloquent\Collection; //use Illuminate\Database\Eloquent\Collection;
//
class ListJsonTableEloquentCollection //class ListJsonTableEloquentCollection
{ //{
private string|null $jsonStr; // private string|null $jsonStr;
private array $meta = []; // private array $meta = [];
//
/** // /**
* @param Collection $collection // * @param Collection $collection
* @param array $params // * @param array $params
* @throws \Exception // * @throws \Exception
*/ // */
public function __construct(Collection $collection, array $params = []) // public function __construct(Collection $collection, array $params = [])
{ // {
if (!isset($params['model'])) { // if (!isset($params['model'])) {
throw new \Exception(message: "The parameter model must not be empty"); // throw new \Exception(message: "The parameter model must not be empty");
} // }
$model = new $params['model'](); // $model = new $params['model']();
$this->meta['columns'] = $params['columns'] ?? $model->labels(); // $this->meta['columns'] = $params['columns'] ?? $model->labels();
$this->meta['perPage'] = $params['perPage'] ?? 10; // $this->meta['perPage'] = $params['perPage'] ?? 10;
$this->meta['currentPage'] = $params['currentPage'] ?? 1; // $this->meta['currentPage'] = $params['currentPage'] ?? 1;
$this->meta['baseUrl'] = $params['baseUrl'] ?? $model->table; // $this->meta['baseUrl'] = $params['baseUrl'] ?? $model->table;
$this->meta['params'] = $params['params'] ?? []; // $this->meta['params'] = $params['params'] ?? [];
$this->meta['actions'] = $params['actions'] ?? []; // $this->meta['actions'] = $params['actions'] ?? [];
//
$this->jsonStr = (new JSONCreator($this->meta, $collection->toArray()))->getJson(); // $this->jsonStr = (new JSONCreator($this->meta, $collection->toArray()))->getJson();
} // }
//
/** // /**
* @return string|null // * @return string|null
*/ // */
public function getJson(): string|null // public function getJson(): string|null
{ // {
return $this->jsonStr; // return $this->jsonStr;
} // }
//
} //}

View File

@ -1,37 +1,37 @@
<?php <?php
//
namespace kernel\IGTabel; //namespace kernel\IGTabel;
//
use app\helpers\Debug; //use app\helpers\Debug;
use app\models\User; //use app\models\User;
use Illuminate\Database\Eloquent\Model; //use Illuminate\Database\Eloquent\Model;
//
class ViewJsonTableEloquentModel //class ViewJsonTableEloquentModel
{ //{
private string|null $jsonStr; // private string|null $jsonStr;
//
private array $meta = []; // private array $meta = [];
//
public function __construct(Model $model, array $params = []) // public function __construct(Model $model, array $params = [])
{ // {
$this->meta['rows'] = $params['rows'] ?? $model->labels(); // $this->meta['rows'] = $params['rows'] ?? $model->labels();
$this->meta['baseUrl'] = $params['baseUrl'] ?? $model->table; // $this->meta['baseUrl'] = $params['baseUrl'] ?? $model->table;
$this->meta['params'] = $params['params'] ?? []; // $this->meta['params'] = $params['params'] ?? [];
$this->meta['actions'] = $params['actions'] ?? []; // $this->meta['actions'] = $params['actions'] ?? [];
//
// $this->jsonStr = (new JSONCreator($this->meta, $model->toArray()))->getJson(); //// $this->jsonStr = (new JSONCreator($this->meta, $model->toArray()))->getJson();
$model = $model->toArray(); // $model = $model->toArray();
if(isset($model['user_id'])) // if(isset($model['user_id']))
{ // {
$model['user_id'] = User::find($model['user_id'])->username; // $model['user_id'] = User::find($model['user_id'])->username;
} // }
//
$this->jsonStr = (new JSONCreator($this->meta, $model))->getJson(); // $this->jsonStr = (new JSONCreator($this->meta, $model))->getJson();
} // }
//
public function getJson(): ?string // public function getJson(): ?string
{ // {
return $this->jsonStr; // return $this->jsonStr;
} // }
//
} //}

View File

@ -20,6 +20,7 @@ App::$collector->group(["prefix" => "admin"], function (RouteCollector $router){
}); });
App::$collector->group(["prefix" => "post"], function (RouteCollector $router){ App::$collector->group(["prefix" => "post"], function (RouteCollector $router){
App::$collector->get('/', [\app\controllers\PostController::class, 'actionIndex']); App::$collector->get('/', [\app\controllers\PostController::class, 'actionIndex']);
App::$collector->get('/page/{page_number}', [\app\controllers\PostController::class, 'actionIndex']);
App::$collector->get('/create', [\app\controllers\PostController::class, 'actionCreate']); App::$collector->get('/create', [\app\controllers\PostController::class, 'actionCreate']);
App::$collector->post("/", [\app\controllers\PostController::class, 'actionAdd']); App::$collector->post("/", [\app\controllers\PostController::class, 'actionAdd']);
App::$collector->get('/{id}', [\app\controllers\PostController::class, 'actionView']); App::$collector->get('/{id}', [\app\controllers\PostController::class, 'actionView']);

View File

@ -2,25 +2,44 @@
/** /**
* @var \Illuminate\Database\Eloquent\Collection $contents * @var \Illuminate\Database\Eloquent\Collection $contents
* @var int $page_number
*/ */
use app\models\Post; use app\models\Post;
use app\models\User;
use app\tables\columns\PostDeleteActionColumn; use app\tables\columns\PostDeleteActionColumn;
use app\tables\columns\PostEditActionColumn; use app\tables\columns\PostEditActionColumn;
use app\tables\columns\PostViewActionColumn; use app\tables\columns\PostViewActionColumn;
use Itguild\Tables\ListJsonTable; use Itguild\EloquentTable\EloquentDataProvider;
use Itguild\EloquentTable\ListEloquentTable;
use kernel\IGTabel\btn\PrimaryBtn; use kernel\IGTabel\btn\PrimaryBtn;
use kernel\IGTabel\ListJsonTableEloquentCollection;
$dataProvider = new ListJsonTableEloquentCollection($contents, [ $table = new ListEloquentTable(new EloquentDataProvider(Post::class, [
'model' => Post::class, 'currentPage' => $page_number,
'perPage' => 5, 'perPage' => 3,
'params' => ["class" => "table table-bordered", "border" => "2"], 'params' => ["class" => "table table-bordered", "border" => "2"],
'baseUrl' => "/admin/post", 'baseUrl' => "/admin/post"
]); ]));
$table->columns([
'created_at' => function ($data) {
if (!$data){
return null;
}
$table = new ListJsonTable($dataProvider->getJson()); return (new DateTimeImmutable($data))->format("d-m-Y");
$table->beforePrint(function (){ },
'updated_at' => function ($data) {
if (!$data){
return null;
}
return (new DateTimeImmutable($data))->format("d-m-Y");
},
'user_id' => (function ($data) {
return User::find($data)->username;
})
]);
$table->beforePrint(function () {
return PrimaryBtn::create("Создать", "/admin/post/create")->fetch(); return PrimaryBtn::create("Создать", "/admin/post/create")->fetch();
//return (new PrimaryBtn("Создать", "/admin/user/create"))->fetch(); //return (new PrimaryBtn("Создать", "/admin/user/create"))->fetch();
}); });

View File

@ -3,22 +3,54 @@
/** /**
* @var \Illuminate\Database\Eloquent\Collection $content * @var \Illuminate\Database\Eloquent\Collection $content
*/ */
use Itguild\Tables\ViewJsonTable;
use app\models\User;
use Itguild\EloquentTable\ViewEloquentTable;
use Itguild\EloquentTable\ViewJsonTableEloquentModel;
use kernel\IGTabel\btn\DangerBtn; use kernel\IGTabel\btn\DangerBtn;
use kernel\IGTabel\btn\PrimaryBtn; use kernel\IGTabel\btn\PrimaryBtn;
use kernel\IGTabel\btn\SuccessBtn; use kernel\IGTabel\btn\SuccessBtn;
use kernel\IGTabel\ViewJsonTableEloquentModel;
$dataProvider = new ViewJsonTableEloquentModel($content, [ //$dataProvider = new ViewJsonTableEloquentModel($content, [
// 'params' => ["class" => "table table-bordered", "border" => "2"],
// 'baseUrl' => "/admin/user",
//]);
//$table = new ViewJsonTable($dataProvider->getJson());
//$table->beforeTable(function () use ($content) {
// $btn = PrimaryBtn::create("Список", "/admin/post")->fetch();
// $btn .= SuccessBtn::create("Редактировать", "/admin/post/update/" . $content->id)->fetch();
// $btn .= DangerBtn::create("Удалить", "/admin/post/delete/" . $content->id)->fetch();
// return $btn;
//});
//$table->create();
//$table->render();
$table = new ViewEloquentTable(new ViewJsonTableEloquentModel($content, [
'params' => ["class" => "table table-bordered", "border" => "2"], 'params' => ["class" => "table table-bordered", "border" => "2"],
'baseUrl' => "/admin/user", 'baseUrl' => "/admin/post",
]); ]));
$table = new ViewJsonTable($dataProvider->getJson()); $table->beforePrint(function () use ($content) {
$table->beforeTable(function () use ($content) {
$btn = PrimaryBtn::create("Список", "/admin/post")->fetch(); $btn = PrimaryBtn::create("Список", "/admin/post")->fetch();
$btn .= SuccessBtn::create("Редактировать", "/admin/post/update/" . $content->id)->fetch(); $btn .= SuccessBtn::create("Редактировать", "/admin/post/update/" . $content->id)->fetch();
$btn .= DangerBtn::create("Удалить", "/admin/post/delete/" . $content->id)->fetch(); $btn .= DangerBtn::create("Удалить", "/admin/post/delete/" . $content->id)->fetch();
return $btn; return $btn;
}); });
$table->rows([
'created_at' => function ($data) {
if (!$data){
return null;
}
return (new DateTimeImmutable($data))->format("d-m-Y");
},
'updated_at' => function ($data) {
if (!$data){
return null;
}
return (new DateTimeImmutable($data))->format("d-m-Y");
},
'user_id' => (function ($data) {
return User::find($data)->username;
})
]);
$table->create(); $table->create();
$table->render(); $table->render();

View File

@ -8,28 +8,37 @@ use app\models\User;
use app\tables\columns\UserDeleteActionColumn; use app\tables\columns\UserDeleteActionColumn;
use app\tables\columns\UserEditActionColumn; use app\tables\columns\UserEditActionColumn;
use app\tables\columns\UserViewActionColumn; use app\tables\columns\UserViewActionColumn;
use Illuminate\Support\Facades\DB; use Itguild\EloquentTable\EloquentDataProvider;
use Itguild\Tables\ListJsonTable; use Itguild\EloquentTable\ListEloquentTable;
use kernel\IGTabel\btn\PrimaryBtn; use kernel\IGTabel\btn\PrimaryBtn;
use kernel\IGTabel\ListJsonTableEloquentCollection;
$table = new ListEloquentTable(new EloquentDataProvider(User::class, [
$dataProvider = new \kernel\IGTabel\EloquentDataProvider(User::where(['status' => 2]), [
'currentPage' => $page_number, 'currentPage' => $page_number,
'perPage' => 3, 'perPage' => 3,
'params' => ["class" => "table table-bordered", "border" => "2"], 'params' => ["class" => "table table-bordered", "border" => "2"],
'baseUrl' => "/admin/user", 'baseUrl' => "/admin/user",
'filters' => ['username', 'email'],
]));
$table->columns([
'created_at' => function ($data) {
if (!$data){
return null;
}
return (new DateTimeImmutable($data))->format("d-m-Y");
},
'updated_at' => function ($data) {
if (!$data){
return null;
}
return (new DateTimeImmutable($data))->format("d-m-Y");
}
]); ]);
$table = new ListJsonTable($dataProvider->getJson());
$table->beforePrint(function () { $table->beforePrint(function () {
return PrimaryBtn::create("Создать", "/admin/user/create")->fetch(); return PrimaryBtn::create("Создать", "/admin/user/create")->fetch();
//return (new PrimaryBtn("Создать", "/admin/user/create"))->fetch(); //return (new PrimaryBtn("Создать", "/admin/user/create"))->fetch();
}); });
$table->afterPrint(function ($meta) {
$pagination = new \Itguild\Tables\Pagination($meta['totalWithFilters'], $meta['perPage'], $meta['currentPage'], $meta['baseUrl'] . "/page");
$pagination->create();
return $pagination->fetch();
});
$table->addAction(UserViewActionColumn::class); $table->addAction(UserViewActionColumn::class);
$table->addAction(UserEditActionColumn::class); $table->addAction(UserEditActionColumn::class);
$table->addAction(UserDeleteActionColumn::class); $table->addAction(UserDeleteActionColumn::class);

View File

@ -3,22 +3,53 @@
/** /**
* @var \Illuminate\Database\Eloquent\Collection $user * @var \Illuminate\Database\Eloquent\Collection $user
*/ */
use Itguild\Tables\ViewJsonTable;
use app\models\User;
use Itguild\EloquentTable\ViewEloquentTable;
use Itguild\EloquentTable\ViewJsonTableEloquentModel;
use kernel\IGTabel\btn\DangerBtn; use kernel\IGTabel\btn\DangerBtn;
use kernel\IGTabel\btn\PrimaryBtn; use kernel\IGTabel\btn\PrimaryBtn;
use kernel\IGTabel\btn\SuccessBtn; use kernel\IGTabel\btn\SuccessBtn;
use kernel\IGTabel\ViewJsonTableEloquentModel;
$dataProvider = new ViewJsonTableEloquentModel($user, [ //$dataProvider = new ViewJsonTableEloquentModel($user, [
// 'params' => ["class" => "table table-bordered", "border" => "2"],
// 'baseUrl' => "/admin/user",
//]);
//$table = new ViewJsonTable($dataProvider->getJson());
//$table->beforeTable(function () use ($user) {
// $btn = PrimaryBtn::create("Список", "/admin/user")->fetch();
// $btn .= SuccessBtn::create("Редактировать", "/admin/user/update/" . $user->id)->fetch();
// $btn .= DangerBtn::create("Удалить", "/admin/user/delete/" . $user->id)->fetch();
// return $btn;
//});
//$table->create();
//$table->render();
$table = new ViewEloquentTable(new ViewJsonTableEloquentModel($user, [
'params' => ["class" => "table table-bordered", "border" => "2"], 'params' => ["class" => "table table-bordered", "border" => "2"],
'baseUrl' => "/admin/user", 'baseUrl' => "/admin/user",
]); ]));
$table = new ViewJsonTable($dataProvider->getJson()); $table->beforePrint(function () use ($user) {
$table->beforeTable(function () use ($user) {
$btn = PrimaryBtn::create("Список", "/admin/user")->fetch(); $btn = PrimaryBtn::create("Список", "/admin/user")->fetch();
$btn .= SuccessBtn::create("Редактировать", "/admin/user/update/" . $user->id)->fetch(); $btn .= SuccessBtn::create("Редактировать", "/admin/user/update/" . $user->id)->fetch();
$btn .= DangerBtn::create("Удалить", "/admin/user/delete/" . $user->id)->fetch(); $btn .= DangerBtn::create("Удалить", "/admin/user/delete/" . $user->id)->fetch();
return $btn; return $btn;
}); });
$table->rows([
'created_at' => function ($data) {
if (!$data){
return null;
}
return (new DateTimeImmutable($data))->format("d-m-Y");
},
'updated_at' => function ($data) {
if (!$data){
return null;
}
return (new DateTimeImmutable($data))->format("d-m-Y");
}
]);
$table->create(); $table->create();
$table->render(); $table->render();