user list table

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

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;
}
}