From 0459ed561c90d8815d23ad3ff08e0b7cbeec570e Mon Sep 17 00:00:00 2001 From: stasbilay02 Date: Tue, 30 Jul 2024 15:49:07 +0300 Subject: [PATCH] user pagination --- app/controllers/UserController.php | 5 +++-- rout.php | 1 + views/admin/user/index.php | 9 ++++++--- 3 files changed, 10 insertions(+), 5 deletions(-) diff --git a/app/controllers/UserController.php b/app/controllers/UserController.php index 4e2b20b..8ceb36c 100644 --- a/app/controllers/UserController.php +++ b/app/controllers/UserController.php @@ -12,6 +12,7 @@ use app\tables\columns\UserEditActionColumn; use app\tables\columns\UserViewActionColumn; use Exception; use http\Message; +use Illuminate\Support\Facades\DB; use Itguild\Tables\ListJsonTable; use Itguild\Tables\ViewJsonTable; use JetBrains\PhpStorm\NoReturn; @@ -59,11 +60,11 @@ class UserController extends Controller{ /** * @throws \Exception */ - public function actionIndex(): void + public function actionIndex($page_id = 1): void { $users = User::all(); - $this->cgView->render("user/index.php", ['users' => $users]); + $this->cgView->render("user/index.php", ['users' => $users, 'page_id' => $page_id]); } /** diff --git a/rout.php b/rout.php index 243ce53..3a59606 100644 --- a/rout.php +++ b/rout.php @@ -10,6 +10,7 @@ App::$collector->get('/example', [MainController::class, 'actionExample']); App::$collector->group(["prefix" => "admin"], function (RouteCollector $router){ App::$collector->group(["prefix" => "user"], callback: function (RouteCollector $router){ App::$collector->get('/', [\app\controllers\UserController::class, 'actionIndex']); + App::$collector->get('/page/{page_id}', [\app\controllers\UserController::class, 'actionIndex']); App::$collector->get('/create', [\app\controllers\UserController::class, 'actionCreate']); App::$collector->post("/", [\app\controllers\UserController::class, 'actionAdd']); App::$collector->get('/{id}', [\app\controllers\UserController::class, 'actionView']); diff --git a/views/admin/user/index.php b/views/admin/user/index.php index 5ec6f5c..ec28b90 100644 --- a/views/admin/user/index.php +++ b/views/admin/user/index.php @@ -2,6 +2,7 @@ /** * @var \Illuminate\Database\Eloquent\Collection $users + * @var int $page_id */ use app\models\User; @@ -12,7 +13,9 @@ use Itguild\Tables\ListJsonTable; use kernel\IGTabel\btn\PrimaryBtn; use kernel\IGTabel\ListJsonTableEloquentCollection; -$dataProvider = new ListJsonTableEloquentCollection($users, [ +$perPage = 3; +$chunks = $users->forPage($page_id, $perPage); +$dataProvider = new ListJsonTableEloquentCollection($chunks, [ 'model' => User::class, 'perPage' => 5, 'params' => ["class" => "table table-bordered", "border" => "2"], @@ -23,8 +26,8 @@ $table->beforePrint(function () { return PrimaryBtn::create("Создать", "/admin/user/create")->fetch(); //return (new PrimaryBtn("Создать", "/admin/user/create"))->fetch(); }); -$table->afterPrint(function () use ($users) { - $pagination = new \Itguild\Tables\Pagination($users->count(), 5, 1, "/admin/user/"); +$table->afterPrint(function () use ($page_id, $users, $perPage) { + $pagination = new \Itguild\Tables\Pagination($users->count(), $perPage, $page_id, "/admin/user/page"); $pagination->create(); return $pagination->fetch(); });