user list table

This commit is contained in:
Kavalar 2024-07-16 13:37:34 +03:00
parent 0d077fb641
commit e07448550c
10 changed files with 11033 additions and 46 deletions

View File

@ -3,10 +3,14 @@ namespace app\controllers;
use app\foo; use app\foo;
use app\helpers\Debug;
use app\models\Question; use app\models\Question;
use app\models\User; use app\models\User;
use app\tables\columns\UserViewActionColumn;
use Itguild\Tables\ListJsonTable; use Itguild\Tables\ListJsonTable;
use kernel\Controller; use kernel\Controller;
use kernel\IGTabel\ListJsonTableEloquentCollection;
use Twig\TwigFunction;
class UserController extends Controller{ class UserController extends Controller{
public function actionCreate(): void public function actionCreate(): void
@ -27,24 +31,27 @@ class UserController extends Controller{
return Question::where('user_id', $user_id)->count(); return Question::where('user_id', $user_id)->count();
} }
/**
* @throws \Exception
*/
public function actionIndex(): void public function actionIndex(): void
{ {
// $loader = new \Twig\Loader\FilesystemLoader(__DIR__.'/../views'); $users = User::where(['role' => 1])->get();
// $twig = new \Twig\Environment($loader, ['cache' => 'app/views/cache']);
$i = 0; $this->twig->addFunction(new TwigFunction('table', function () use ($users){
foreach (User::all() as $user) $dataProvider = new ListJsonTableEloquentCollection($users, [
{ 'model' => User::class,
$userArr[$i++] = $user; 'perPage' => 5,
} 'params' => ["class" => "table table-bordered", "border" => "2"],
echo $this->twig->render('user_table.html.twig', ['userArr' => $userArr]); 'baseUrl' => "/admin/user",
]);
$json = new foo(); $table = new ListJsonTable($dataProvider->getJson());
$userArr = $json->createJsonArray(User::labels(), $userArr, "form1"); $table->addAction(UserViewActionColumn::class);
$table = new ListJsonTable($userArr); $table->create();
$table->create(); $table->render();
$table->render(); }));
echo $this->twig->render('user_table.html.twig');
} }
public function actionView($id): void public function actionView($id): void

View File

@ -9,6 +9,7 @@ class foo
/** /**
* @param array $columns * @param array $columns
* @param array $data * @param array $data
* @param string $title
* @return string|null * @return string|null
*/ */
public function createJsonArray(array $columns, array $data, string $title): ?string public function createJsonArray(array $columns, array $data, string $title): ?string

View File

@ -0,0 +1,16 @@
<?php
namespace app\tables\columns;
use Itguild\Tables\ActionColumn\ActionColumn;
class UserViewActionColumn extends ActionColumn
{
protected string $prefix = "/";
public function fetch(): string
{
$link = $this->baseUrl . $this->prefix . $this->id;
return " <a href='$link' class='btn btn-primary'>Просмотр</a> ";
}
}

View File

@ -7,7 +7,8 @@
"craft-group/phroute": "^2.1", "craft-group/phroute": "^2.1",
"vlucas/phpdotenv": "^5.6", "vlucas/phpdotenv": "^5.6",
"twig/twig": "^3.0", "twig/twig": "^3.0",
"itguild/tables": "^0.1.0" "twbs/bootstrap": "5.0.2",
"itguild/tables": "^0.1.3"
}, },
"autoload": { "autoload": {
"psr-4": { "psr-4": {

60
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": "479a64315f08eb4456be2e2425d4ba60", "content-hash": "1ab10f0d80bf16426c5a0cbba3924d9a",
"packages": [ "packages": [
{ {
"name": "brick/math", "name": "brick/math",
@ -725,11 +725,11 @@
}, },
{ {
"name": "itguild/tables", "name": "itguild/tables",
"version": "0.1", "version": "0.1.5",
"source": { "source": {
"type": "git", "type": "git",
"url": "https://git.itguild.info/ItGuild/tables", "url": "https://git.itguild.info/ItGuild/tables",
"reference": "42ab26a3db1cef8319985fc3a0f5d23d2dff4e3b" "reference": "85c4a850b190e20944dc5ee5e888b6ecd342f82f"
}, },
"type": "library", "type": "library",
"autoload": { "autoload": {
@ -747,7 +747,7 @@
"email": "apuc06@mail.ru" "email": "apuc06@mail.ru"
} }
], ],
"time": "2024-05-27T09:59:44+00:00" "time": "2024-07-16T10:26:29+00:00"
}, },
{ {
"name": "nesbot/carbon", "name": "nesbot/carbon",
@ -1710,6 +1710,56 @@
], ],
"time": "2024-04-18T09:32:20+00:00" "time": "2024-04-18T09:32:20+00:00"
}, },
{
"name": "twbs/bootstrap",
"version": "v5.0.2",
"source": {
"type": "git",
"url": "https://github.com/twbs/bootstrap.git",
"reference": "688bce4fa695cc360a0d084e34f029b0c192b223"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/twbs/bootstrap/zipball/688bce4fa695cc360a0d084e34f029b0c192b223",
"reference": "688bce4fa695cc360a0d084e34f029b0c192b223",
"shasum": ""
},
"replace": {
"twitter/bootstrap": "self.version"
},
"type": "library",
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Mark Otto",
"email": "markdotto@gmail.com"
},
{
"name": "Jacob Thornton",
"email": "jacobthornton@gmail.com"
}
],
"description": "The most popular front-end framework for developing responsive, mobile first projects on the web.",
"homepage": "https://getbootstrap.com/",
"keywords": [
"JS",
"css",
"framework",
"front-end",
"mobile-first",
"responsive",
"sass",
"web"
],
"support": {
"issues": "https://github.com/twbs/bootstrap/issues",
"source": "https://github.com/twbs/bootstrap/tree/v5.0.2"
},
"time": "2021-06-22T18:29:16+00:00"
},
{ {
"name": "twig/twig", "name": "twig/twig",
"version": "v3.10.3", "version": "v3.10.3",
@ -1956,5 +2006,5 @@
"prefer-lowest": false, "prefer-lowest": false,
"platform": [], "platform": [],
"platform-dev": [], "platform-dev": [],
"plugin-api-version": "2.6.0" "plugin-api-version": "2.3.0"
} }

View File

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

View File

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

10837
resources/css/bootstrap.css vendored Normal file

File diff suppressed because it is too large Load Diff

View File

@ -3,12 +3,17 @@
<head> <head>
<meta charset="UTF-8"> <meta charset="UTF-8">
<title>Примеры шаблонизатора Twig</title> <title>Примеры шаблонизатора Twig</title>
<link rel="stylesheet" href="/resources/css/bootstrap.css">
</head> </head>
<body> <body>
<h1>HEADER</h1> <div class="container">
{% block content %} <div class="row">
{% endblock %} <h1>HEADER</h1>
<h1>FOOTER</h1> {% block content %}
{% endblock %}
<h1>FOOTER</h1>
</div>
</div>
</body> </body>
</html> </html>

View File

@ -1,26 +1,5 @@
{% extends "main_layout.html.twig" %} {% extends "main_layout.html.twig" %}
{% block content %} {% block content %}
<table border="1" style="width: 80%;"> {{ table() }}
<thead>
<tr>
<td>id</td>
<td>Username</td>
<td>Email</td>
<td>Created at</td>
<td>Updated at</td>
</tr>
</thead>
<tbody>
{% for users in userArr %}
<tr>
<td>{{ users.id }}</td>
<td>{{ users.username }}</td>
<td>{{ users.email }}</td>
<td>{{ users.created_at }}</td>
<td>{{ users.updated_at }}</td>
</tr>
{% endfor %}
</tbody>
</table>
{% endblock %} {% endblock %}