Skip to content
This repository has been archived by the owner on Jun 22, 2022. It is now read-only.

Commit

Permalink
feat(client): if department is not set in client than show empty value (
Browse files Browse the repository at this point in the history
  • Loading branch information
MonikaInfyOm authored Apr 14, 2020
1 parent e929554 commit 0f49d61
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 8 deletions.
2 changes: 1 addition & 1 deletion app/Http/Controllers/ClientController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
14 changes: 12 additions & 2 deletions app/Queries/ClientDataTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Queries;

use App\Models\Client;
use Illuminate\Database\Eloquent\Builder as BuilderAlias;
use Illuminate\Database\Query\Builder;

/**
Expand All @@ -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;
}
Expand Down
18 changes: 16 additions & 2 deletions resources/assets/js/clients/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: [
{
Expand All @@ -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',
},
{
Expand Down Expand Up @@ -52,6 +61,11 @@ $('#clients_table').DataTable({
}, name: 'id',
},
],
'fnInitComplete': function () {
$('#filter_department').change(function () {
tbl.ajax.reload();
});
},
});

$('#addNewForm').submit(function (event) {
Expand Down
9 changes: 6 additions & 3 deletions resources/views/clients/index.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,12 @@
@include('flash::message')
<div class="page-header">
<h3 class="page__heading">Clients</h3>
<div style="display: flex;align-items: center">
<a href="#" class="btn btn-primary" data-toggle="modal" data-target="#AddModal"></i>New
Client</a>
<div class="filter-container">
<div class="mr-2">
<label class="lbl-block"><b>Department</b></label>
{!!Form::select('department_id', $departments, Auth::id(), ['id' => 'filter_department', 'class'=>'form-control', 'placeholder' => 'All']) !!}
</div>
<a href="#" class="btn btn-primary filter-container__btn" data-toggle="modal" data-target="#AddModal"></i>New Client</a>
</div>
</div>
<div class="row">
Expand Down

0 comments on commit 0f49d61

Please sign in to comment.