Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement $sortable property for TableColumn. #972

Merged
merged 2 commits into from
Mar 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion demos/database.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public function init()
$this->addField('name', ['actual' => 'nicename', 'required' => true, 'type' => 'string']);
$this->addField('sys_name', ['actual' => 'name', 'system' => true]);

$this->addField('iso', ['caption' => 'ISO', 'required' => true, 'type' => 'string']);
$this->addField('iso', ['caption' => 'ISO', 'required' => true, 'type' => 'string', 'ui'=>['table'=>['sortable'=>false]]]);
$this->addField('iso3', ['caption' => 'ISO3', 'required' => true, 'type' => 'string']);
$this->addField('numcode', ['caption' => 'ISO Numeric Code', 'type' => 'number', 'required' => true]);
$this->addField('phonecode', ['caption' => 'Phone Prefix', 'type' => 'number', 'required' => true]);
Expand Down
4 changes: 2 additions & 2 deletions docs/grid.rst
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,8 @@ Sorting
When grid is associated with a model that supports order, it will automatically make itself sortable. You can
override this behaviour by setting $sortable property to `true` or `false`.

Additionally you may set list of sortable fields to a sortable property if you wish that your grid would be
sortable only for those columns.
You can also set $sortable property for each table column decorator. That way you can enable/disable sorting
of particular columns.

See also :php:attr:`Table::$sortable`.

Expand Down
4 changes: 4 additions & 0 deletions public/agileui.less
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,10 @@ footer.atk-footer {
}
}

.ui.sortable.table thead th:not(.sortable) {
cursor: default;
}

// Components

.atk-overflow-auto {
Expand Down
4 changes: 2 additions & 2 deletions src/Grid.php
Original file line number Diff line number Diff line change
Expand Up @@ -601,7 +601,7 @@ public function addUserAction(Generic $action)
*/
public function getSortBy()
{
return isset($_GET[$this->sortTrigger]) ? $_GET[$this->sortTrigger] : null;
return $_GET[$this->sortTrigger] ?? null;
}

/**
Expand Down Expand Up @@ -639,7 +639,7 @@ public function applySort()

$this->table->on(
'click',
'thead>tr>th',
'thead>tr>th.sortable',
new jsReload($this->container, [$this->sortTrigger => (new jQuery())->data('sort')])
);
}
Expand Down
16 changes: 14 additions & 2 deletions src/TableColumn/Generic.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@ class Generic
*/
public $caption = null;

/**
* Is column sortable?
*
* @var boolean
*/
public $sortable = true;

/**
* The data-column attribute value for Table th tag.
*
Expand Down Expand Up @@ -309,7 +316,7 @@ public function getHeaderCellHTML(Field $field = null, $value = null)
$caption = $this->caption ?: $field->getCaption();

$attr = [
'data-column' => $this->columnData
'data-column' => $this->columnData,
];

$class = 'atk-table-column-header';
Expand All @@ -321,9 +328,14 @@ public function getHeaderCellHTML(Field $field = null, $value = null)
$caption = [$this->headerActionTag, $caption];
}

// If table is being sorted by THIS column, set the proper class
if ($this->table->sortable) {
$attr['data-sort'] = $field->short_name;

if ($this->sortable) {
$attr['class'] = ['sortable'];
}

// If table is being sorted by THIS column, set the proper class
if ($this->table->sort_by === $field->short_name) {
$class .= ' sorted '.$this->table->sort_order;

Expand Down
2 changes: 2 additions & 0 deletions src/TableColumn/Password.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
*/
class Password extends Generic
{
public $sortable = false;

public function getDataCellTemplate(\atk4\data\Field $f = null)
{
return '***';
Expand Down