user list table
This commit is contained in:
parent
0d077fb641
commit
e07448550c
@ -3,10 +3,14 @@ namespace app\controllers;
|
||||
|
||||
|
||||
use app\foo;
|
||||
use app\helpers\Debug;
|
||||
use app\models\Question;
|
||||
use app\models\User;
|
||||
use app\tables\columns\UserViewActionColumn;
|
||||
use Itguild\Tables\ListJsonTable;
|
||||
use kernel\Controller;
|
||||
use kernel\IGTabel\ListJsonTableEloquentCollection;
|
||||
use Twig\TwigFunction;
|
||||
|
||||
class UserController extends Controller{
|
||||
public function actionCreate(): void
|
||||
@ -27,24 +31,27 @@ class UserController extends Controller{
|
||||
return Question::where('user_id', $user_id)->count();
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function actionIndex(): void
|
||||
{
|
||||
// $loader = new \Twig\Loader\FilesystemLoader(__DIR__.'/../views');
|
||||
// $twig = new \Twig\Environment($loader, ['cache' => 'app/views/cache']);
|
||||
$users = User::where(['role' => 1])->get();
|
||||
|
||||
$i = 0;
|
||||
foreach (User::all() as $user)
|
||||
{
|
||||
$userArr[$i++] = $user;
|
||||
}
|
||||
echo $this->twig->render('user_table.html.twig', ['userArr' => $userArr]);
|
||||
|
||||
$json = new foo();
|
||||
$userArr = $json->createJsonArray(User::labels(), $userArr, "form1");
|
||||
$table = new ListJsonTable($userArr);
|
||||
$this->twig->addFunction(new TwigFunction('table', function () use ($users){
|
||||
$dataProvider = new ListJsonTableEloquentCollection($users, [
|
||||
'model' => User::class,
|
||||
'perPage' => 5,
|
||||
'params' => ["class" => "table table-bordered", "border" => "2"],
|
||||
'baseUrl' => "/admin/user",
|
||||
]);
|
||||
$table = new ListJsonTable($dataProvider->getJson());
|
||||
$table->addAction(UserViewActionColumn::class);
|
||||
$table->create();
|
||||
$table->render();
|
||||
}));
|
||||
|
||||
echo $this->twig->render('user_table.html.twig');
|
||||
}
|
||||
|
||||
public function actionView($id): void
|
||||
|
@ -9,6 +9,7 @@ class foo
|
||||
/**
|
||||
* @param array $columns
|
||||
* @param array $data
|
||||
* @param string $title
|
||||
* @return string|null
|
||||
*/
|
||||
public function createJsonArray(array $columns, array $data, string $title): ?string
|
||||
|
16
app/tables/columns/UserViewActionColumn.php
Normal file
16
app/tables/columns/UserViewActionColumn.php
Normal 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> ";
|
||||
}
|
||||
}
|
@ -7,7 +7,8 @@
|
||||
"craft-group/phroute": "^2.1",
|
||||
"vlucas/phpdotenv": "^5.6",
|
||||
"twig/twig": "^3.0",
|
||||
"itguild/tables": "^0.1.0"
|
||||
"twbs/bootstrap": "5.0.2",
|
||||
"itguild/tables": "^0.1.3"
|
||||
},
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
|
60
composer.lock
generated
60
composer.lock
generated
@ -4,7 +4,7 @@
|
||||
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
|
||||
"This file is @generated automatically"
|
||||
],
|
||||
"content-hash": "479a64315f08eb4456be2e2425d4ba60",
|
||||
"content-hash": "1ab10f0d80bf16426c5a0cbba3924d9a",
|
||||
"packages": [
|
||||
{
|
||||
"name": "brick/math",
|
||||
@ -725,11 +725,11 @@
|
||||
},
|
||||
{
|
||||
"name": "itguild/tables",
|
||||
"version": "0.1",
|
||||
"version": "0.1.5",
|
||||
"source": {
|
||||
"type": "git",
|
||||
"url": "https://git.itguild.info/ItGuild/tables",
|
||||
"reference": "42ab26a3db1cef8319985fc3a0f5d23d2dff4e3b"
|
||||
"reference": "85c4a850b190e20944dc5ee5e888b6ecd342f82f"
|
||||
},
|
||||
"type": "library",
|
||||
"autoload": {
|
||||
@ -747,7 +747,7 @@
|
||||
"email": "apuc06@mail.ru"
|
||||
}
|
||||
],
|
||||
"time": "2024-05-27T09:59:44+00:00"
|
||||
"time": "2024-07-16T10:26:29+00:00"
|
||||
},
|
||||
{
|
||||
"name": "nesbot/carbon",
|
||||
@ -1710,6 +1710,56 @@
|
||||
],
|
||||
"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",
|
||||
"version": "v3.10.3",
|
||||
@ -1956,5 +2006,5 @@
|
||||
"prefer-lowest": false,
|
||||
"platform": [],
|
||||
"platform-dev": [],
|
||||
"plugin-api-version": "2.6.0"
|
||||
"plugin-api-version": "2.3.0"
|
||||
}
|
||||
|
49
kernel/IGTabel/JSONCreator.php
Normal file
49
kernel/IGTabel/JSONCreator.php
Normal 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);
|
||||
}
|
||||
}
|
42
kernel/IGTabel/ListJsonTableEloquentCollection.php
Normal file
42
kernel/IGTabel/ListJsonTableEloquentCollection.php
Normal 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
10837
resources/css/bootstrap.css
vendored
Normal file
File diff suppressed because it is too large
Load Diff
@ -3,12 +3,17 @@
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>Примеры шаблонизатора Twig</title>
|
||||
<link rel="stylesheet" href="/resources/css/bootstrap.css">
|
||||
</head>
|
||||
<body>
|
||||
<div class="container">
|
||||
<div class="row">
|
||||
<h1>HEADER</h1>
|
||||
{% block content %}
|
||||
{% endblock %}
|
||||
<h1>FOOTER</h1>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
</body>
|
||||
</html>
|
@ -1,26 +1,5 @@
|
||||
{% extends "main_layout.html.twig" %}
|
||||
|
||||
{% block content %}
|
||||
<table border="1" style="width: 80%;">
|
||||
<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>
|
||||
{{ table() }}
|
||||
{% endblock %}
|
Loading…
Reference in New Issue
Block a user