From cfe38232c32e759eb6d33827ad671a9513b00f2b Mon Sep 17 00:00:00 2001 From: pbao9 Date: Fri, 30 Jan 2026 13:56:32 +0700 Subject: [PATCH] update datatable --- .../Controllers/admin/package/Datacenter.php | 29 ++++- .../Datacenter/DatacenterCachedRepository.php | 35 ++++-- .../Datacenter/DatacenterRepository.php | 37 ++++-- .../DatacenterRepositoryInterface.php | 14 ++- config/admin-sidebar.php | 94 +++++++------- .../admin/package/datacenter/index.blade.php | 88 ++++---------- .../administrator/layout/master.blade.php | 2 +- .../layout/partials/sidebar.blade.php | 92 +++++++------- .../views/components/data-table.blade.php | 115 +++++++++++++----- routes/admin.php | 1 + 10 files changed, 292 insertions(+), 215 deletions(-) diff --git a/app/Http/Controllers/admin/package/Datacenter.php b/app/Http/Controllers/admin/package/Datacenter.php index 8d7d53b..c22b68e 100644 --- a/app/Http/Controllers/admin/package/Datacenter.php +++ b/app/Http/Controllers/admin/package/Datacenter.php @@ -10,6 +10,7 @@ use App\Repositories\vCenter\vCenterRepositoryInterface; use Exception; use Illuminate\Http\Request; use Illuminate\Support\Facades\DB; +use Yajra\DataTables\Facades\DataTables; class Datacenter extends Controller { @@ -41,7 +42,7 @@ class Datacenter extends Controller $request->validated(); $vcenter = $this->vCenterRepository->find($request->get('vcenter_id')); - if (! $vcenter) { + if (!$vcenter) { return back()->withErrors('Invalid request.'); } $isCreated = $this->datacenterRepository->findBy('datacenter_id', $request->get('datacenter_id')); @@ -62,7 +63,7 @@ class Datacenter extends Controller { $datacenter = $this->datacenterRepository->find($id); - if (! $datacenter) { + if (!$datacenter) { return back()->withErrors('Invalid request.'); } $title = 'Edit Datacenter'; @@ -72,7 +73,7 @@ class Datacenter extends Controller { $datacenter = $this->datacenterRepository->find($id); - if (! $datacenter) { + if (!$datacenter) { return back()->withErrors('Invalid request.'); } $request->validated(); @@ -96,7 +97,7 @@ class Datacenter extends Controller { $datacenter = $this->datacenterRepository->find($id); - if (! $datacenter) { + if (!$datacenter) { return back()->withErrors('Invalid request.'); } @@ -115,4 +116,24 @@ class Datacenter extends Controller dispatch($syncDatacenter)->delay(now()->addSeconds(5)); return back()->with('success', 'The process is being established, please wait'); } + + public function datatable(Request $request) + { + $query = $this->datacenterRepository->filter( + $request->all(), + ['vCenter'], + [], + ['*'], + true + ); + + return DataTables::eloquent($query) + ->addColumn( + 'action', + fn($d) => + 'Edit' + ) + ->make(true); + } + } diff --git a/app/Repositories/Module/Datacenter/DatacenterCachedRepository.php b/app/Repositories/Module/Datacenter/DatacenterCachedRepository.php index 01cc2ef..325c8a0 100644 --- a/app/Repositories/Module/Datacenter/DatacenterCachedRepository.php +++ b/app/Repositories/Module/Datacenter/DatacenterCachedRepository.php @@ -16,19 +16,32 @@ class DatacenterCachedRepository extends BaseCachedRepository implements Datacen { return $this->datacenterRepository->getModel(); } - public function filter(array $data = [], array $relationships = [], array $withCount = [], array $select = ['*']) - { + public function filter( + array $data = [], + array $relationships = [], + array $withCount = [], + array $select = ['*'], + bool $returnQuery = false + ) { + if ( + $returnQuery || + !empty($data['paginate']) + ) { + return $this->datacenterRepository + ->filter($data, $relationships, $withCount, $select, $returnQuery); + } + $cacheKey = $this->getCacheKey(md5(json_encode([ - 'filterItem' => [ - 'data' => $data, - 'relationships' => $relationships, - 'select' => $select, - 'withCount' => $withCount - ] + 'filter' => compact('data', 'relationships', 'withCount', 'select') ]))); - return Cache::remember($cacheKey, $this->cacheTime, function () use ($data, $relationships, $select, $withCount) { - return $this->datacenterRepository->filter($data, $relationships, $withCount, $select); - }); + + return Cache::remember( + $cacheKey, + $this->cacheTime, + fn() => + $this->datacenterRepository + ->filter($data, $relationships, $withCount, $select, $returnQuery) + ); } public function findBy(string $key = '', string|int|null $value = '') { diff --git a/app/Repositories/Module/Datacenter/DatacenterRepository.php b/app/Repositories/Module/Datacenter/DatacenterRepository.php index 22198f8..d699f4d 100644 --- a/app/Repositories/Module/Datacenter/DatacenterRepository.php +++ b/app/Repositories/Module/Datacenter/DatacenterRepository.php @@ -12,22 +12,39 @@ class DatacenterRepository extends BaseRepository implements DatacenterRepositor return ModuleDatacenter::class; } - public function filter(array $data = [], array $relationships = [], array $withCount = [], array $select = ['*']) - { - $model = $this->model->with($relationships)->select($select); + public function filter( + array $data = [], + array $relationships = [], + array $withCount = [], + array $select = ['*'], + bool $returnQuery = false + ) { + $query = $this->model + ->select($select) + ->with($relationships); - if (isset($data['vcenter_id']) && $data['vcenter_id']) { - $model = $model->where("vcenter_id", $data['vcenter_id']); + if (!empty($data['vcenter_id'])) { + $query->where('vcenter_id', $data['vcenter_id']); } - if (count($withCount) > 0) { - $model = $model->withCount($withCount); + + if (!empty($withCount)) { + $query->withCount($withCount); } - if (isset($data['page'])) { - return $model->paginate(15); + + if ($returnQuery) { + return $query; } - return $model->get(); + + if (!empty($data['paginate'])) { + return $query->paginate( + $data['per_page'] ?? 15 + ); + } + + return $query->get(); } + public function findBy(string $key = '', string|int|null $value = '') { if (empty($key) || empty($value)) { diff --git a/app/Repositories/Module/Datacenter/DatacenterRepositoryInterface.php b/app/Repositories/Module/Datacenter/DatacenterRepositoryInterface.php index c2588fa..e81d3a9 100644 --- a/app/Repositories/Module/Datacenter/DatacenterRepositoryInterface.php +++ b/app/Repositories/Module/Datacenter/DatacenterRepositoryInterface.php @@ -3,7 +3,15 @@ namespace App\Repositories\Module\Datacenter; use App\Repositories\BaseRepositoryInterface; -interface DatacenterRepositoryInterface extends BaseRepositoryInterface{ - public function filter(array $data = [],array $relationships = [],array $withCount = [],array $select = ['*']); - public function findBy(string $key = '',string|int|null $value = '' ); +interface DatacenterRepositoryInterface extends BaseRepositoryInterface +{ + public function filter( + array $data = [], + array $relationships = [], + array $withCount = [], + array $select = ['*'], + bool $returnQuery = false + ); + + public function findBy(string $key = '', string|int|null $value = ''); } \ No newline at end of file diff --git a/config/admin-sidebar.php b/config/admin-sidebar.php index 16ba909..ad8bed6 100644 --- a/config/admin-sidebar.php +++ b/config/admin-sidebar.php @@ -9,29 +9,29 @@ return [ 'type' => 'group', 'title' => 'Package vCenter', 'icon' => 'box', - 'active' => ['administrator/package/*'], + 'active' => ['package.datacenter.*', 'package.host.*', 'package.datastore.*', 'package.folder.*', 'package.network.*', 'package.group.*', 'package.version.*', 'package.ipaddress.*', 'package.vm.*'], 'children' => [ - ['title' => 'Datacenter', 'url' => '/administrator/package/datacenter/list-all-datacenter', 'active' => ['administrator/package/datacenter/*']], - ['title' => 'Host', 'url' => '/administrator/package/host/list-all-host', 'active' => ['administrator/package/host/*']], - ['title' => 'Datastore', 'url' => '/administrator/package/datastore/list-all-datastore', 'active' => ['administrator/package/datastore/*']], - ['title' => 'Folder', 'url' => '/administrator/package/folder/list-all-folder', 'active' => ['administrator/package/folder/*']], - ['title' => 'Network Adapter', 'url' => '/administrator/package/network/list-all-network', 'active' => ['administrator/package/network/*']], - ['title' => 'Group Os', 'url' => '/administrator/package/group/list-all-group', 'active' => ['administrator/package/group/*']], - ['title' => 'Os Version', 'url' => '/administrator/package/version/list-all-os-version', 'active' => ['administrator/package/version/*']], - ['title' => 'IP Mapping', 'url' => '/administrator/package/ipaddress/list-all-ip-address', 'active' => ['administrator/package/ipaddress/*']], - ['title' => 'Virtual Machines', 'url' => '/administrator/package/vm/list-all-vm', 'active' => ['administrator/package/vm/*']], + ['title' => 'Datacenter', 'route' => 'package.datacenter.index', 'active' => ['package.datacenter.*']], + ['title' => 'Host', 'route' => 'package.host.index', 'active' => ['package.host.*']], + ['title' => 'Datastore', 'route' => 'package.datastore.index', 'active' => ['package.datastore.*']], + ['title' => 'Folder', 'route' => 'package.folder.index', 'active' => ['package.folder.*']], + ['title' => 'Network Adapter', 'route' => 'package.network.index', 'active' => ['package.network.*']], + ['title' => 'Group Os', 'route' => 'package.group.index', 'active' => ['package.group.*']], + ['title' => 'Os Version', 'route' => 'package.version.index', 'active' => ['package.version.*']], + ['title' => 'IP Mapping', 'route' => 'package.ipaddress.index', 'active' => ['package.ipaddress.*']], + ['title' => 'Virtual Machines', 'route' => 'package.vm.index', 'active' => ['package.vm.*']], ], ], [ 'type' => 'group', 'title' => 'Module', 'icon' => 'server', - 'active' => ['administrator/list-all-server-*', 'administrator/list-all-vcenter*', 'administrator/list-all-server-ip-pool*'], + 'active' => ['module.dns.*', 'module.hosting.*', 'module.vcenter.*', 'module.ippool.*'], 'children' => [ - ['title' => 'Module DNS', 'url' => '/administrator/list-all-server-dns', 'active' => ['administrator/list-all-server-dns*']], - ['title' => 'Module Hosting', 'url' => '/administrator/list-all-server-hosting', 'active' => ['administrator/list-all-server-hosting*']], - ['title' => 'Module vCenter', 'url' => '/administrator/list-all-vcenter', 'active' => ['administrator/list-all-vcenter*']], - ['title' => 'Module IP Pool', 'url' => '/administrator/list-all-server-ip-pool', 'active' => ['administrator/list-all-server-ip-pool*']], + ['title' => 'Module DNS', 'route' => 'module.dns.index', 'active' => ['module.dns.*']], + ['title' => 'Module Hosting', 'route' => 'module.hosting.index', 'active' => ['module.hosting.*']], + ['title' => 'Module vCenter', 'route' => 'module.vcenter.index', 'active' => ['module.vcenter.*']], + ['title' => 'Module IP Pool', 'route' => 'module.ippool.index', 'active' => ['module.ippool.*']], ], ], [ @@ -42,57 +42,57 @@ return [ 'type' => 'group', 'title' => 'Manager Admin', 'icon' => 'users', - 'active' => ['administrator/list-all', 'administrator/list-role*'], + 'active' => ['admin.user.*', 'role.*'], 'children' => [ - ['title' => 'List Admin', 'url' => '/administrator/list-all', 'active' => ['administrator/list-all*']], - ['title' => 'Administrator Roles', 'url' => '/administrator/list-role', 'active' => ['administrator/list-role*']], + ['title' => 'List Admin', 'route' => 'admin.user.index', 'active' => ['admin.user.*']], + ['title' => 'Administrator Roles', 'route' => 'role.index', 'active' => ['role.*']], ], ], [ 'type' => 'group', 'title' => 'Client', 'icon' => 'users', - 'active' => ['administrator/list-customer*', 'administrator/add-client*'], + 'active' => ['customer.*'], 'children' => [ - ['title' => 'View All Client', 'url' => '/administrator/list-customer', 'active' => ['administrator/list-customer*']], - ['title' => 'Add new Client', 'url' => '/administrator/add-client', 'active' => ['administrator/add-client*']], + ['title' => 'View All Client', 'route' => 'customer.index', 'active' => ['customer.index', 'customer.info', 'customer.profile', 'customer.product', 'customer.domain', 'customer.invoice', 'customer.trans', 'customer.email', 'customer.log']], + ['title' => 'Add new Client', 'route' => 'customer.create', 'active' => ['customer.create']], ], ], [ 'type' => 'group', 'title' => 'Product', 'icon' => 'cloud', - 'active' => ['administrator/all-product*', 'administrator/list-addon*', 'administrator/list-domain*'], + 'active' => ['product.*', 'addon.*', 'domain.*'], 'children' => [ - ['title' => 'All Product/Services', 'url' => '/administrator/all-product', 'active' => ['administrator/all-product*']], - ['title' => 'Product Addon', 'url' => '/administrator/list-addon', 'active' => ['administrator/list-addon*']], - ['title' => 'Domain Pricing', 'url' => '/administrator/list-domain', 'active' => ['administrator/list-domain*']], + ['title' => 'All Product/Services', 'route' => 'product.index', 'active' => ['product.*']], + ['title' => 'Product Addon', 'route' => 'addon.index', 'active' => ['addon.*']], + ['title' => 'Domain Pricing', 'route' => 'domain.index', 'active' => ['domain.*']], ], ], [ 'type' => 'group', 'title' => 'Orders', 'icon' => 'shopping-cart', - 'active' => ['administrator/list-all-order*', 'administrator/add-new-order*'], + 'active' => ['admin.order.*'], 'children' => [ - ['title' => 'View All Orders', 'url' => '/administrator/list-all-order', 'active' => ['administrator/list-all-order']], - ['title' => 'Pending Orders', 'url' => '/administrator/list-all-order?status=pending', 'active' => []], - ['title' => 'Processing Orders', 'url' => '/administrator/list-all-order?status=processing', 'active' => []], - ['title' => 'Active Orders', 'url' => '/administrator/list-all-order?status=active', 'active' => []], - ['title' => 'Cancel Orders', 'url' => '/administrator/list-all-order?status=cancel', 'active' => []], - ['title' => 'Add New Orders', 'url' => '/administrator/add-new-order', 'active' => ['administrator/add-new-order*']], + ['title' => 'View All Orders', 'route' => 'admin.order.index', 'active' => ['admin.order.index'], 'params' => []], + ['title' => 'Pending Orders', 'route' => 'admin.order.index', 'active' => [], 'params' => ['status' => 'pending']], + ['title' => 'Processing Orders', 'route' => 'admin.order.index', 'active' => [], 'params' => ['status' => 'processing']], + ['title' => 'Active Orders', 'route' => 'admin.order.index', 'active' => [], 'params' => ['status' => 'active']], + ['title' => 'Cancel Orders', 'route' => 'admin.order.index', 'active' => [], 'params' => ['status' => 'cancel']], + ['title' => 'Add New Orders', 'route' => 'admin.order.create', 'active' => ['admin.order.create']], ], ], [ 'type' => 'group', 'title' => 'Invoices', 'icon' => 'file-text', - 'active' => ['administrator/list-all-invoice*'], + 'active' => ['admin.invoice.*'], 'children' => [ - ['title' => 'View All Invoice', 'url' => '/administrator/list-all-invoice', 'active' => ['administrator/list-all-invoice']], - ['title' => 'Pending Invoice', 'url' => '/administrator/list-all-invoice?status=unpaid', 'active' => []], - ['title' => 'Paid Invoice', 'url' => '/administrator/list-all-invoice?status=active', 'active' => []], - ['title' => 'Cancel Invoice', 'url' => '/administrator/list-all-invoice?status=cancel', 'active' => []], + ['title' => 'View All Invoice', 'route' => 'admin.invoice.index', 'active' => ['admin.invoice.index'], 'params' => []], + ['title' => 'Pending Invoice', 'route' => 'admin.invoice.index', 'active' => [], 'params' => ['status' => 'unpaid']], + ['title' => 'Paid Invoice', 'route' => 'admin.invoice.index', 'active' => [], 'params' => ['status' => 'active']], + ['title' => 'Cancel Invoice', 'route' => 'admin.invoice.index', 'active' => [], 'params' => ['status' => 'cancel']], ], ], [ @@ -103,37 +103,37 @@ return [ 'type' => 'group', 'title' => 'Menus', 'icon' => 'folder', - 'active' => ['administrator/list-menu*'], + 'active' => ['menu.*'], 'children' => [ - ['title' => 'View All Menu', 'url' => '/administrator/list-menu', 'active' => ['administrator/list-menu*']], + ['title' => 'View All Menu', 'route' => 'menu.index', 'active' => ['menu.*']], ], ], [ 'type' => 'item', 'title' => 'Coupon', 'icon' => 'tags', - 'url' => '/administrator/list-coupon', - 'active' => ['administrator/list-coupon*'], + 'route' => 'coupon.index', + 'active' => ['coupon.*'], ], [ 'type' => 'item', 'title' => 'Posts', 'icon' => 'pencil', - 'url' => '/administrator/list-post', - 'active' => ['administrator/list-post*'], + 'route' => 'post.index', + 'active' => ['post.*'], ], [ 'type' => 'item', 'title' => 'Support', 'icon' => 'ticket', - 'url' => '/administrator/list-all-support', - 'active' => ['administrator/list-all-support*'], + 'route' => 'support.index', + 'active' => ['support.*'], ], [ 'type' => 'item', 'title' => 'Payment Method', 'icon' => 'credit-card', - 'url' => '/administrator/list-payment', - 'active' => ['administrator/list-payment*'], + 'route' => 'payment.index', + 'active' => ['payment.*'], ], ]; diff --git a/resources/views/admin/package/datacenter/index.blade.php b/resources/views/admin/package/datacenter/index.blade.php index 3d9a87a..bdccd09 100644 --- a/resources/views/admin/package/datacenter/index.blade.php +++ b/resources/views/admin/package/datacenter/index.blade.php @@ -1,76 +1,24 @@ -@php use App\Http\Helpers\Helper; @endphp -@extends('admin.main') +@extends('administrator.layout.master') + +@push('plugin-styles') + +@endpush + @section('content') @if(auth()->guard('admins')->user()->can("manager package module"))
@include("admin.block.Sync.Datacenter")
-
- - - - - - - - - - - - - @if($datacenter->count()) - @foreach($datacenter->all() as $item) - - - - - - - - - @endforeach - @endif - -
- - Datacenter NamevCenter HostLast Updated
- - - {{ $item->name }} - - @isset($item->vCenter->host) - - {{ $item->vCenter->host }} - @endisset - - @time($item->created_at) - - - - - - @if(auth()->guard('admins')->user()->can("delete module vcenter")) -
- @method('delete') - @csrf - -
- @endif -
+
+
- -
@else
Access Denied - You are not assigned to any Module Package @@ -78,3 +26,13 @@
@endif @endsection + +@push('plugin-scripts') + + + + +@endpush + +@push('custom-scripts') +@endpush \ No newline at end of file diff --git a/resources/views/administrator/layout/master.blade.php b/resources/views/administrator/layout/master.blade.php index d7cfa9a..dc30f51 100644 --- a/resources/views/administrator/layout/master.blade.php +++ b/resources/views/administrator/layout/master.blade.php @@ -55,7 +55,7 @@ @include('administrator.layout.partials.sidebar')
@include('administrator.layout.partials.header') -
+
@yield('content')
diff --git a/resources/views/administrator/layout/partials/sidebar.blade.php b/resources/views/administrator/layout/partials/sidebar.blade.php index 2aa0911..f392c4c 100644 --- a/resources/views/administrator/layout/partials/sidebar.blade.php +++ b/resources/views/administrator/layout/partials/sidebar.blade.php @@ -6,53 +6,55 @@ -
- - + + - \ No newline at end of file + @endforeach + + +
+ \ No newline at end of file diff --git a/resources/views/components/data-table.blade.php b/resources/views/components/data-table.blade.php index c02fa65..66d18f3 100644 --- a/resources/views/components/data-table.blade.php +++ b/resources/views/components/data-table.blade.php @@ -1,35 +1,92 @@ @php $dtColumns = collect($columns)->map(function ($c) { - return [ - 'data' => $c['key'], - 'name' => $c['key'], - 'orderable' => $c['orderable'] ?? true, - 'searchable' => $c['searchable'] ?? true, - ]; + return [ + 'data' => $c['key'], + 'name' => $c['key'], + 'orderable' => $c['orderable'] ?? true, + 'searchable' => $c['searchable'] ?? true, + ]; })->values(); @endphp - -
- - - - @foreach($columns as $col) - - @endforeach - +
{{ $col['label'] }}
+ + + @foreach($columns as $col) + + @endforeach + + + @foreach($columns as $col) + + @endforeach + -
{{ $col['label'] }}
+ @if(($col['searchable'] ?? true)) + + @endif +
-
+ + @push('custom-scripts') + -@endpush + var table = $('#{{ $id }}').DataTable({ + processing: true, + serverSide: true, + ajax: '{{ $ajax }}', + columns: @json($dtColumns), + orderCellsTop: true, // Important for the second header row + fixedHeader: true, + pagingType: 'full_numbers', + language: { + searchPlaceholder: "Search records", + search: "", + lengthMenu: "_MENU_", + }, + dom: "<'row'<'col-sm-12'<'table-responsive'tr>>>" + + "<'row mt-3 align-items-center'<'col-sm-12 col-md-5'i><'col-sm-12 col-md-7 d-flex align-items-center justify-content-end gap-2'lp>>", + initComplete: function () { + // Initialize select2 for length menu + $('.dataTables_length select').select2({ + minimumResultsForSearch: Infinity, + width: '60px' + }); + + // Apply the search + this.api().columns().every(function () { + var that = this; + $('input', this.header()).on('keyup change clear', function () { + if (that.search() !== this.value) { + that + .search(this.value) + .draw(); + } + }); + }); + }, + drawCallback: function() { + if ($('.dataTables_length select').data('select2')) { + $('.dataTables_length select').select2('destroy'); + } + $('.dataTables_length select').select2({ + minimumResultsForSearch: Infinity, + width: '60px' + }); + } + }); + + // Prevent sorting when clicking on the input + $('#{{ $id }} thead tr:eq(1) th').on('click', function (e) { + e.stopPropagation(); + }); + + // Custom search logic for the individual inputs + $('#{{ $id }} .column-search').on('keyup change', function () { + var columnIndex = $(this).data('column'); + table.column(columnIndex).search(this.value).draw(); + }); + }); + + @endpush \ No newline at end of file diff --git a/routes/admin.php b/routes/admin.php index 842bde4..2996908 100644 --- a/routes/admin.php +++ b/routes/admin.php @@ -219,6 +219,7 @@ Route::prefix('administrator')->group(function () { Route::get('edit/{id}', 'edit')->name('edit'); Route::put('edit/{id}', 'update')->name('update'); Route::delete('remove/{id}', 'destroy')->name('destroy'); + Route::get('datatable', 'datatable')->name('datatable'); }); //Datastore Route::group([