Skip to content

Commit

Permalink
Enable multi-sort capability; Closes #391
Browse files Browse the repository at this point in the history
  • Loading branch information
juniwalk committed Nov 2, 2016
2 parents 05ecebb + f6f1477 commit 515177c
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 37 deletions.
99 changes: 63 additions & 36 deletions src/DataGrid.php
Expand Up @@ -214,6 +214,11 @@ class DataGrid extends Nette\Application\UI\Control
*/
protected $sortable = FALSE;

/**
* @var bool
*/
protected $multiSort = FALSE;

/**
* @var string
*/
Expand Down Expand Up @@ -699,25 +704,46 @@ public function setSortable($sortable = TRUE)


/**
* Set sortable handle
* @param string $handler
* @return static
* Tell whether DataGrid is sortable
* @return bool
*/
public function setSortableHandler($handler = 'sort!')
public function isSortable()
{
$this->sortable_handler = (string) $handler;
return $this->sortable;
}

return $this;

/**
* Enable multi-sorting capability
* @param bool $multiSort
* @return void
*/
public function setMultiSortEnabled($multiSort = TRUE)
{
$this->multiSort = (bool) $multiSort;
}


/**
* Tell whether DataGrid is sortable
* Tell wether DataGrid can be sorted by multiple columns
* @return bool
*/
public function isSortable()
public function isMultiSortEnabled()
{
return $this->sortable;
return $this->multiSort;
}


/**
* Set sortable handle
* @param string $handler
* @return static
*/
public function setSortableHandler($handler = 'sort!')
{
$this->sortable_handler = (string) $handler;

return $this;
}

/**
Expand All @@ -730,12 +756,29 @@ public function getSortableHandler()
}


/**
* @param Column $column
* @return array
* @internal
*/
public function getSortNext(\Ublaboo\DataGrid\Column\Column $column)
{
$sort = $column->getSortNext();

if ($this->isMultiSortEnabled()) {
$sort = array_merge($this->sort, $sort);
}

return array_filter($sort);
}


/**
* @param array $sort
* @param callable|NULL $sort_callback
* @return void
*/
protected function createSorting(array $sort, $sort_callback)
protected function createSorting(array $sort, callable $sort_callback = NULL)
{
foreach ($sort as $key => $order) {
unset($sort[$key]);
Expand All @@ -750,8 +793,7 @@ protected function createSorting(array $sort, $sort_callback)
$sort[$column->getSortingColumn()] = $order;
}

// required for first request
if (isset($column) && $sort_callback === NULL) {
if (!$sort_callback && isset($column)) {
$sort_callback = $column->getSortableCallback();
}

Expand Down Expand Up @@ -1955,44 +1997,29 @@ public function handlePage($page)
* Handler for sorting
* @param array $sort
* @return void
* @throws DataGridColumnNotFoundException
*/
public function handleSort(array $sort)
{
$new_sort = [];

/**
* Find apropirate column
*/
foreach ($sort as $key => $value) {
if (empty($this->columns[$key])) {
throw new DataGridException("Column <$key> not found");
}
try {
$column = $this->getColumn($key);

$column = $this->columns[$key];
$new_sort = [$key => $value];
} catch (DataGridColumnNotFoundException $e) {
unset($sort[$key]);
continue;
}

/**
* Pagination may be reseted after sorting
*/
if ($column->sortableResetPagination()) {
$this->page = 1;
$this->saveSessionData('_grid_page', 1);
$this->saveSessionData('_grid_page', $this->page = 1);
}

/**
* Custom sorting callback may be applied
*/
if ($column->getSortableCallback()) {
$this->sort_callback = $column->getSortableCallback();
}
}

/**
* Session stuff
*/
$this->sort = $new_sort;
$this->saveSessionData('_grid_sort', $this->sort);

$this->saveSessionData('_grid_sort', $this->sort = $sort);
$this->reload(['table']);
}

Expand Down
2 changes: 1 addition & 1 deletion src/templates/datagrid.latte
Expand Up @@ -140,7 +140,7 @@
{include #$col_header, column => $column}
{else}
{if $column->isSortable()}
<a n:class="$column->isSortedBy() ? 'sort' : '', 'ajax'" {if $column->hasSortNext()}href="{link sort!, sort => $column->getSortNext()}"{else}href="{link sort!, sort => NULL}"{/if} id="datagrid-sort-{$key}">
<a n:class="$column->isSortedBy() ? 'sort' : '', 'ajax'" href="{link sort!, sort => $control->getSortNext($column)}" id="datagrid-sort-{$key}">
{include #column-header, column => $column}

{if $column->isSortedBy()}
Expand Down

0 comments on commit 515177c

Please sign in to comment.