-
Notifications
You must be signed in to change notification settings - Fork 222
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Updated main list page to allow sorting by description and searching also
- Loading branch information
Showing
7 changed files
with
110 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>' | ||
); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
}); | ||
} | ||
} |