Skip to content

Commit

Permalink
Merge pull request #9 from biscofil/master
Browse files Browse the repository at this point in the history
Order direction can also be specified with a separate "order" parameter
  • Loading branch information
santigarcor authored Mar 9, 2021
2 parents 2b16b04 + 61edf85 commit a974ee7
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 3 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,15 @@ Your request to the controller should have this data:
}
```

You can also specify the sorting order using the "order" attribute (required by https://mannyyang.github.io/vuetable-3/ ):
```javascript
{
sort: '', // column_name
order: '', // asc or desc
}
```


So for example lets create the table for the users with their companies. Then in the javascript we should have:

```javascript
Expand Down
10 changes: 8 additions & 2 deletions src/Builders/CollectionVuetableBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,16 @@ public function sort()
return $this;
}

list($field, $direction) = explode('|', $this->request->input('sort'));
$sort_parts = explode('|', $this->request->input('sort'));

$field = $sort_parts[0];

$direction = count($sort_parts) > 1
? $sort_parts[1]
: $this->request->input('order', 'desc');

if ($field) {
$comparer = function ($a, $b) use ($field,$direction) {
$comparer = function ($a, $b) use ($field, $direction) {
if ($direction === 'desc') {
$first = $b;
$second = $a;
Expand Down
8 changes: 7 additions & 1 deletion src/Builders/EloquentVuetableBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,15 @@ public function sort()
if (!$this->request->input('sort')) {
return $this;
}

$sort_parts = explode('|', $this->request->input('sort'));

list($field, $direction) = explode("|", $this->request->input("sort"));
$field = $sort_parts[0];

$direction = count($sort_parts) > 1
? $sort_parts[1]
: $this->request->input('order', 'desc');

$this->query->orderBy($field, $direction);

return $this;
Expand Down

0 comments on commit a974ee7

Please sign in to comment.