feat: 🐛 admin/servers sortable

This commit is contained in:
IceToast 2023-02-15 21:41:24 +01:00 committed by IceToast
parent d905171fcb
commit 459069c5ee
2 changed files with 60 additions and 58 deletions

View file

@ -6,6 +6,7 @@ use App\Classes\Pterodactyl;
use App\Http\Controllers\Controller;
use App\Models\Server;
use App\Models\User;
use App\Traits\DatatablesSortable;
use Exception;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Contracts\View\Factory;
@ -14,9 +15,12 @@ use Illuminate\Http\JsonResponse;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\Log;
class ServerController extends Controller
{
use DatatablesSortable;
/**
* Display a listing of the resource.
*
@ -27,38 +31,6 @@ class ServerController extends Controller
return view('admin.servers.index');
}
/**
* Show the form for creating a new resource.
*
* @return Response
*/
public function create()
{
//
}
/**
* Store a newly created resource in storage.
*
* @param Request $request
* @return Response
*/
public function store(Request $request)
{
//
}
/**
* Display the specified resource.
*
* @param Server $server
* @return Response
*/
public function show(Server $server)
{
//
}
/**
* Show the form for editing the specified resource.
*
@ -196,6 +168,7 @@ class ServerController extends Controller
public function dataTable(Request $request)
{
$query = Server::with(['user', 'product']);
if ($request->has('product')) {
$query->where('product_id', '=', $request->input('product'));
}
@ -204,6 +177,11 @@ class ServerController extends Controller
}
$query->select('servers.*');
if ($request->has('order')) {
$query = $this->sortByColumn($request->input('order'), $request->input('columns'), $query);
}
return datatables($query)
->addColumn('user', function (Server $server) {
return '<a href="' . route('admin.users.show', $server->user->id) . '">' . $server->user->name . '</a>';
@ -232,7 +210,7 @@ class ServerController extends Controller
';
})
->addColumn('status', function (Server $server) {
$labelColor = $server->isSuspended() ? 'text-danger' : 'text-success';
$labelColor = $server->suspended ? 'text-danger' : 'text-success';
return '<i class="fas ' . $labelColor . ' fa-circle mr-2"></i>';
})

View file

@ -1,15 +1,15 @@
<table id="datatable" class="table table-striped">
<thead>
<tr>
<th width="20"></th>
<th>{{__('Name')}}</th>
<th>{{__('User')}}</th>
<th>{{__('Server id')}}</th>
<th>{{__('Config')}}</th>
<th>{{__('Suspended at')}}</th>
<th>{{__('Created at')}}</th>
<th></th>
</tr>
<tr>
<th width="20"></th>
<th>{{ __('Name') }}</th>
<th>{{ __('User') }}</th>
<th>{{ __('Server id') }}</th>
<th>{{ __('Config') }}</th>
<th>{{ __('Suspended at') }}</th>
<th>{{ __('Created at') }}</th>
<th></th>
</tr>
</thead>
<tbody>
</tbody>
@ -17,30 +17,54 @@
<script>
function submitResult() {
return confirm("{{__('Are you sure you wish to delete?')}}") !== false;
return confirm("{{ __('Are you sure you wish to delete?') }}") !== false;
}
document.addEventListener("DOMContentLoaded", function () {
document.addEventListener("DOMContentLoaded", function() {
$('#datatable').DataTable({
language: {
url: '//cdn.datatables.net/plug-ins/1.11.3/i18n/{{config("SETTINGS::LOCALE:DATATABLES")}}.json'
url: '//cdn.datatables.net/plug-ins/1.11.3/i18n/{{ config('SETTINGS::LOCALE:DATATABLES') }}.json'
},
processing: true,
serverSide: true,
stateSave: true,
ajax: "{{route('admin.servers.datatable')}}{{$filter ?? ''}}",
order: [[ 5, "desc" ]],
columns: [
{data: 'status' , name : 'servers.suspended'},
{data: 'name'},
{data: 'user' , name : 'user.name'},
{data: 'identifier'},
{data: 'resources' , name : 'product.name'},
{data: 'suspended'},
{data: 'created_at'},
{data: 'actions' , sortable : false},
ajax: "{{ route('admin.servers.datatable') }}{{ $filter ?? '' }}",
order: [
[6, "desc"]
],
fnDrawCallback: function( oSettings ) {
columns: [{
data: 'status',
name: 'servers.suspended',
sortable: false
},
{
data: 'name'
},
{
data: 'user',
name: 'user.name',
sortable: false
},
{
data: 'identifier'
},
{
data: 'resources',
name: 'product.name',
sortable: false
},
{
data: 'suspended'
},
{
data: 'created_at'
},
{
data: 'actions',
sortable: false
},
],
fnDrawCallback: function(oSettings) {
$('[data-toggle="popover"]').popover();
}
});