From 0f49d619f19e362d87447c1784265516bac761b4 Mon Sep 17 00:00:00 2001 From: Monika Vaghasiya <51854283+MonikaInfyOm@users.noreply.github.com> Date: Tue, 14 Apr 2020 11:05:38 +0530 Subject: [PATCH] feat(client): if department is not set in client than show empty value (#447) --- app/Http/Controllers/ClientController.php | 2 +- app/Queries/ClientDataTable.php | 14 ++++++++++++-- resources/assets/js/clients/client.js | 18 ++++++++++++++++-- resources/views/clients/index.blade.php | 9 ++++++--- 4 files changed, 35 insertions(+), 8 deletions(-) diff --git a/app/Http/Controllers/ClientController.php b/app/Http/Controllers/ClientController.php index 9d7dd176d..21ba06eb1 100644 --- a/app/Http/Controllers/ClientController.php +++ b/app/Http/Controllers/ClientController.php @@ -46,7 +46,7 @@ public function __construct(ClientRepository $clientRepo) public function index(Request $request) { if ($request->ajax()) { - return Datatables::of((new ClientDataTable())->get())->make(true); + return Datatables::of((new ClientDataTable())->get($request->only(['filter_department'])))->make(true); } $departments = Department::all()->pluck('name', 'id')->toArray(); diff --git a/app/Queries/ClientDataTable.php b/app/Queries/ClientDataTable.php index b84005d2b..853d7d303 100644 --- a/app/Queries/ClientDataTable.php +++ b/app/Queries/ClientDataTable.php @@ -3,6 +3,7 @@ namespace App\Queries; use App\Models\Client; +use Illuminate\Database\Eloquent\Builder as BuilderAlias; use Illuminate\Database\Query\Builder; /** @@ -11,12 +12,21 @@ class ClientDataTable { /** + * @param array $input + * * @return Client|Builder */ - public function get() + public function get($input = []) { /** @var Client $query */ - $query = Client::with('department'); + $query = Client::with('department')->select('clients.*'); + + $query->when( + isset($input['filter_department']) && !empty($input['filter_department']), + function (BuilderAlias $q) use ($input) { + $q->where('department_id', '=', $input['filter_department']); + } + ); return $query; } diff --git a/resources/assets/js/clients/client.js b/resources/assets/js/clients/client.js index 324f3c2ea..0ab97f72b 100644 --- a/resources/assets/js/clients/client.js +++ b/resources/assets/js/clients/client.js @@ -3,12 +3,19 @@ $('#department_id,#edit_department_id').select2({ placeholder: 'Select Department', }); -$('#clients_table').DataTable({ +$('#filter_department').select2(); + +let tbl = $('#clients_table').DataTable({ processing: true, serverSide: true, 'order': [[0, 'asc']], ajax: { url: clientUrl, + data: function (data) { + data.filter_department = $('#filter_department'). + find('option:selected'). + val(); + }, }, columnDefs: [ { @@ -24,7 +31,9 @@ $('#clients_table').DataTable({ name: 'name', }, { - data: 'department.name', + data: function (row) { + return (row.department !== null) ? row.department.name : ''; + }, name: 'department.name', }, { @@ -52,6 +61,11 @@ $('#clients_table').DataTable({ }, name: 'id', }, ], + 'fnInitComplete': function () { + $('#filter_department').change(function () { + tbl.ajax.reload(); + }); + }, }); $('#addNewForm').submit(function (event) { diff --git a/resources/views/clients/index.blade.php b/resources/views/clients/index.blade.php index 8a184e835..dfaf2fe99 100644 --- a/resources/views/clients/index.blade.php +++ b/resources/views/clients/index.blade.php @@ -9,9 +9,12 @@ @include('flash::message')