Skip to content

Commit

Permalink
Handle ListView sorting
Browse files Browse the repository at this point in the history
  • Loading branch information
mirovit committed Oct 11, 2016
1 parent c1bdca9 commit 300b030
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,18 @@ class LessonsController extends Controller
return Lesson::filter($filters)->get();
}
}
```

# [ListView](https://github.com/administrcms/listview) sorting

By default the base Filter class will handle the sorting for your main table. If you have a ListView that uses a column which is a relationship to the model, you'll have to handle that column for yourself. Let's imagine that you have User model and it has a relationship to UserType (one User has one UserType). So in your ListView you want to sort by the column that shows the name of the UserType. We'll assume that the column in the ListView is defined as `type.name`. In that case you need to define the custom sort like this:

```php
public function sortTypeName($dir)
{
return $this
->builder
->join('user_types', 'user_types.id', '=', 'users.type_id')
->orderBy('user_types.id', $dir);
}
```
34 changes: 34 additions & 0 deletions src/Filter.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,38 @@ public function apply(Builder $builder)

return $this->builder;
}

/**
* Handle sorting of ListView module. If the field being sorted
* is not in the main table, you have to define a sort method
* which follows the convention sortFieldName, so if the
* column is type.name it will be sortTypeName and it
* will accept the sort direction as parameter
* which will be equeal to 'asc' or 'desc'.
*
* @param array $sort
* @return Builder
*/
public function sort(array $sort)
{
$tableName = $this->builder->getModel()->getTable();

foreach($sort as $field => $dir)
{
$builder = $this->builder;

// Convert to method for sorting - sortId, sortName, sortFirstName ...
$method = 'sort' . studly_case(str_replace('.', '_', $field));

// If it is not a method, then try to do the orderBy query for the field
if(! method_exists($this, $method)) {
$builder = $builder->orderBy("{$tableName}.{$field}", $dir);
continue;
}

$builder = call_user_func([$this, $method], $dir);
}

return $builder;
}
}

0 comments on commit 300b030

Please sign in to comment.