From 6bf4d48ddbb66dfb5f3fa0814220f71f9a79cc9b Mon Sep 17 00:00:00 2001 From: DarkSide Date: Sat, 29 Feb 2020 23:37:09 +0200 Subject: [PATCH 1/2] Implement $sortable property for TableColumn. --- demos/database.php | 2 +- docs/grid.rst | 4 ++-- public/agileui.less | 4 ++++ src/Grid.php | 4 ++-- src/TableColumn/Generic.php | 16 ++++++++++++++-- src/TableColumn/Password.php | 2 ++ 6 files changed, 25 insertions(+), 7 deletions(-) diff --git a/demos/database.php b/demos/database.php index 8cdc8c656f..bd06bafdad 100644 --- a/demos/database.php +++ b/demos/database.php @@ -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]); diff --git a/docs/grid.rst b/docs/grid.rst index 8518fa64ab..d3c92dbb15 100644 --- a/docs/grid.rst +++ b/docs/grid.rst @@ -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`. diff --git a/public/agileui.less b/public/agileui.less index 61bd916dca..ab18090682 100644 --- a/public/agileui.less +++ b/public/agileui.less @@ -125,6 +125,10 @@ footer.atk-footer { } } +.ui.sortable.table thead th:not(.sortable) { + cursor: default; +} + // Components .atk-overflow-auto { diff --git a/src/Grid.php b/src/Grid.php index a7b9ca0ea2..4b72a83efe 100644 --- a/src/Grid.php +++ b/src/Grid.php @@ -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; } /** @@ -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')]) ); } diff --git a/src/TableColumn/Generic.php b/src/TableColumn/Generic.php index ca88752c0f..f205edf02c 100644 --- a/src/TableColumn/Generic.php +++ b/src/TableColumn/Generic.php @@ -38,6 +38,13 @@ class Generic * @var string */ public $caption = null; + + /** + * Is column sortable? + * + * @var boolean + */ + public $sortable = true; /** * The data-column attribute value for Table th tag. @@ -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'; @@ -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; diff --git a/src/TableColumn/Password.php b/src/TableColumn/Password.php index 2cacacc8ba..32db7a10e7 100644 --- a/src/TableColumn/Password.php +++ b/src/TableColumn/Password.php @@ -7,6 +7,8 @@ */ class Password extends Generic { + public $sortable = false; + public function getDataCellTemplate(\atk4\data\Field $f = null) { return '***'; From 0884a40dc6164b6f98a09408e1840c3b6f033fce Mon Sep 17 00:00:00 2001 From: DarkSide Date: Sat, 29 Feb 2020 23:40:27 +0200 Subject: [PATCH 2/2] space --- src/TableColumn/Generic.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/TableColumn/Generic.php b/src/TableColumn/Generic.php index f205edf02c..51681c357e 100644 --- a/src/TableColumn/Generic.php +++ b/src/TableColumn/Generic.php @@ -38,7 +38,7 @@ class Generic * @var string */ public $caption = null; - + /** * Is column sortable? *