Skip to content

Commit

Permalink
Feature/add sort and search (#93)
Browse files Browse the repository at this point in the history
* Updated main list page to allow sorting by description and searching also
  • Loading branch information
qschmick authored Mar 22, 2018
1 parent db7bab0 commit e4f30b0
Show file tree
Hide file tree
Showing 7 changed files with 110 additions and 9 deletions.
2 changes: 1 addition & 1 deletion resources/views/tasks/form.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@
<label class="uk-margin">
Auto Cleanup results after
<br>
<input type="number" name="auto_cleanup_num" id="auto_cleanup_num" value="{{ old('auto_cleanup_num', $task->auto_cleanup_num) }}" />
<input type="number" name="auto_cleanup_num" id="auto_cleanup_num" value="{{ old('auto_cleanup_num', $task->auto_cleanup_num) ?? 0 }}" />
<br>
<label>
<input type="radio" name="auto_cleanup_type" value="days" {{old('auto_cleanup_type', $task->auto_cleanup_type) !== 'results' ? '' : 'checked'}}> Days
Expand Down
16 changes: 10 additions & 6 deletions resources/views/tasks/index.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,24 @@
@section('title')
<div class="uk-flex uk-flex-between uk-flex-middle">
<h4 class="uk-card-title uk-margin-remove">Tasks</h4>
<form class="uk-display-inline uk-search uk-search-default">
<span class="uk-icon uk-search-icon">
{!! Form::open([
'id' => 'totem__search__form',
'url' => Request::fullUrl(),
'method' => 'GET',
'class' => 'uk-display-inline uk-search uk-search-default'
]) !!}
<span class="uk-icon uk-search-icon" style="cursor: pointer; pointer-events: auto;" onclick="totem__search__form.submit()">
<icon name="search" :scale="100"></icon>
</span>

<input class="uk-search-input" type="search" placeholder="Search...">
</form>
{!! Form::text('q', request('q'), ['class' => 'uk-search-input', 'placeholder' => 'Search...']) !!}
{!! Form::close() !!}
</div>
@stop
@section('main-panel-content')
<table class="uk-table uk-table-responsive" cellpadding="0" cellspacing="0" class="mb1">
<thead>
<tr>
<th>Description</th>
<th>{!! Html::columnSort('Description', 'description') !!}</th>
<th>Average Runtime</th>
<th>Last Run</th>
<th>Next Run</th>
Expand Down
10 changes: 9 additions & 1 deletion src/Http/Controllers/TasksController.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,15 @@ public function __construct(TaskInterface $tasks)
public function index()
{
return view('totem::tasks.index', [
'tasks' => $this->tasks->builder()->orderBy('description')->paginate(20),
'tasks' => $this->tasks
->builder()
->sortableBy([
'description',
], ['description'=>'asc'])
->when(request('q'), function ($query) {
$query->where('description', 'LIKE', '%'.request('q').'%');
})
->paginate(20),
]);
}

Expand Down
55 changes: 55 additions & 0 deletions src/Providers/TotemFormServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

namespace Studio\Totem\Providers;

use Illuminate\Support\ServiceProvider;

class TotemFormServiceProvider extends ServiceProvider
{
/**
* Bootstrap the application services.
*
* @return void
*/
public function boot()
{
$this->app['html']->macro('columnSort', function (string $label, string $columnKey, bool $isDefault = false) {
$icon = '';

if (request()->has('sort_by')) {
if (request()->input('sort_by') == $columnKey) {
$icon = ' <span class="fa fa-caret-'
.(request()->input('sort_direction', 'asc') == 'asc' ? 'up' : 'down')
.'"></span>';
}
} elseif ($isDefault) {
$icon = ' <span class="fa fa-caret-'
.(request()->input('sort_direction', 'asc') == 'asc' ? 'up' : 'down')
.'"></span>';
}

$order = 'asc';
if (request()->has('sort_direction')) {
$order = (request()->input('sort_direction') == 'desc' ? 'asc' : 'desc');
} elseif ($isDefault) {
$order = 'desc';
}

$url = request()->fullUrlWithQuery([
'sort_by' => $columnKey,
'sort_direction' => $order,
'filter' => request('filter'),
'limit' => request('limit'),
]);

return app('html')->toHtmlString(
'<a href="'
.$url
.'">'
.$label
.$icon
.'</a>'
);
});
}
}
1 change: 1 addition & 0 deletions src/Providers/TotemServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public function register()
$this->app->alias('totem.tasks', TaskInterface::class);
$this->app->register(TotemRouteServiceProvider::class);
$this->app->register(TotemEventServiceProvider::class);
$this->app->register(TotemFormServiceProvider::class);

$this->mergeConfigFrom(
__DIR__.'/../../config/totem.php',
Expand Down
3 changes: 2 additions & 1 deletion src/Task.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@
use Cron\CronExpression;
use Studio\Totem\Traits\HasFrequencies;
use Illuminate\Notifications\Notifiable;
use Studio\Totem\Traits\FrontendSortable;

class Task extends TotemModel
{
use Notifiable, HasFrequencies;
use Notifiable, HasFrequencies, FrontendSortable;

/**
* The attributes that are mass assignable.
Expand Down
32 changes: 32 additions & 0 deletions src/Traits/FrontendSortable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace Studio\Totem\Traits;

use Illuminate\Database\Eloquent\Builder;

trait FrontendSortable
{
/**
* @param \Illuminate\Database\Eloquent\Builder $builder
* @param array $sortableColumns
* @param array $defaultSort
*
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeSortableBy(Builder $builder, array $sortableColumns, array $defaultSort = ['name' => 'asc']) : Builder
{
$request = request();
$sorted = $request->has('sort_by') && in_array($request->input('sort_by'), $sortableColumns);

return $builder->when($sorted, function (Builder $query) use ($request) {
$query->orderBy(
$request->input('sort_by'),
(($request->input('sort_direction', 'asc') == 'desc') ? 'desc' : 'asc')
);
}, function (Builder $query) use ($defaultSort) {
foreach ($defaultSort as $key => $direction) {
$query->orderBy($key, $direction);
}
});
}
}

0 comments on commit e4f30b0

Please sign in to comment.