Skip to content

Commit

Permalink
Sort update
Browse files Browse the repository at this point in the history
  • Loading branch information
deniskoronets committed May 25, 2020
1 parent 8408eef commit 8e6ba32
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 5 deletions.
2 changes: 1 addition & 1 deletion resources/views/renderers/default.blade.php
Expand Up @@ -20,7 +20,7 @@
<tr>
@foreach ($grid->columns as $column)
<th {!! $column->compileHeaderHtmlOptions() !!}>
<a href="#" @if ($column->sortable && is_scalar($column->value)) v-on:click="sort('{{ $column->value }}')" @endif>{{ $column->title }}</a>
<a href="#" @if ($column->getSortableName() !== false) v-on:click="sort('{{ $column->getSortableName() }}')" @endif>{{ $column->title }}</a>

@if ($column->sortable)
@if ($grid->getRequest()->sortColumn == $column->value)
Expand Down
26 changes: 23 additions & 3 deletions src/Columns/BaseColumn.php
Expand Up @@ -29,7 +29,7 @@ abstract class BaseColumn
public $filter;

/**
* @var boolean
* @var boolean|string
*/
public $sortable = true;

Expand Down Expand Up @@ -66,6 +66,26 @@ public function __construct(array $config)
$this->buildFilter();
}

/**
* Allows to get sortable column's name
*/
public function getSortableName()
{
if ($this->sortable === false) {
return false;
}

if (is_scalar($this->sortable)) {
return $this->sortable;
}

if (is_scalar($this->value)) {
return $this->value;
}

return false;
}

protected function buildFilter()
{
if (is_null($this->filter) || is_object($this->filter)) {
Expand Down Expand Up @@ -103,7 +123,7 @@ protected function configTests(): array
'contentHtmlOptions' => 'array',
'formatters' => 'array',
'emptyValue' => 'string',
'sortable' => 'bool',
'sortable' => 'any',
'filter' => BaseFilter::class . '|null',
];
}
Expand Down Expand Up @@ -151,4 +171,4 @@ public function renderValue($row)
return $value;
}

}
}
3 changes: 2 additions & 1 deletion src/DataProviders/EloquentDataProvider.php
Expand Up @@ -18,6 +18,7 @@ public function __construct(Builder $query)
$this->query = clone $query;
}


/**
* @param GridViewRequest $request
* @return Builder
Expand Down Expand Up @@ -60,4 +61,4 @@ public function getData(GridViewRequest $request)
->limit($request->perPage)
->get();
}
}
}

0 comments on commit 8e6ba32

Please sign in to comment.