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

View file

@ -23,22 +23,46 @@
document.addEventListener("DOMContentLoaded", function() { document.addEventListener("DOMContentLoaded", function() {
$('#datatable').DataTable({ $('#datatable').DataTable({
language: { 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, processing: true,
serverSide: true, serverSide: true,
stateSave: true, stateSave: true,
ajax: "{{ route('admin.servers.datatable') }}{{ $filter ?? '' }}", ajax: "{{ route('admin.servers.datatable') }}{{ $filter ?? '' }}",
order: [[ 5, "desc" ]], order: [
columns: [ [6, "desc"]
{data: 'status' , name : 'servers.suspended'}, ],
{data: 'name'}, columns: [{
{data: 'user' , name : 'user.name'}, data: 'status',
{data: 'identifier'}, name: 'servers.suspended',
{data: 'resources' , name : 'product.name'}, sortable: false
{data: 'suspended'}, },
{data: 'created_at'}, {
{data: 'actions' , 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) { fnDrawCallback: function(oSettings) {
$('[data-toggle="popover"]').popover(); $('[data-toggle="popover"]').popover();