From 385c03108c020b07a5f935209f25b414a6fe8dfb Mon Sep 17 00:00:00 2001 From: Pavel Janda Date: Sat, 7 Jan 2017 09:04:54 +0100 Subject: [PATCH] Column::getElementPrototype() update --- src/Column/Column.php | 50 ++++++++++++++++++++++++------------ src/templates/datagrid.latte | 14 +++++----- 2 files changed, 40 insertions(+), 24 deletions(-) diff --git a/src/Column/Column.php b/src/Column/Column.php index 175520ed..60c511ac 100644 --- a/src/Column/Column.php +++ b/src/Column/Column.php @@ -98,6 +98,11 @@ abstract class Column extends FilterableColumn */ protected $default_hide = FALSE; + /** + * @var array + */ + protected $elementCache = ['td' => NULL, 'th' => NULL]; + /** * Render row item into template @@ -643,14 +648,33 @@ public function addAttributes(array $attrs) /** * Get th/td column element - * @param string $tag th|td + * @param string $tag th|td + * @return Html + */ + public function getElementPrototype($tag) + { + if ($this->elementCache[$tag]) { + return $this->elementCache[$tag]; + } + + return $this->elementCache[$tag] = Html::el($tag); + } + + + /** + * Method called from datagrid template, set appropriate classes and another attributes + * @param string $tag * @param string $key * @param Row|NULL $row * @return Html */ - public function getElementPrototype($tag, $key = NULL, Row $row = NULL) + public function getElementForRender($tag, $key, Row $row = NULL) { - $el = Html::el($tag); + if ($this->elementCache[$tag]) { + $el = clone $this->elementCache[$tag]; + } else { + $el = Html::el($tag); + } /** * If class was set by user via $el->class = '', fix it @@ -663,23 +687,15 @@ public function getElementPrototype($tag, $key = NULL, Row $row = NULL) } $el->class[] = "text-{$this->getAlign()}"; + $el->class[] = "col-{$key}"; - /** - * Method called from datagrid template, set appropriate classes and another attributes - */ - if ($key !== NULL && $row !== NULL) { - $el->class[] = "col-{$key}"; + if ($row && $tag == 'td' && $this->isEditable()) { + $link = $this->grid->link('edit!', ['key' => $key, 'id' => $row->getId()]); - if ($tag == 'td') { - if ($this->isEditable()) { - $link = $this->grid->link('edit!', ['key' => $key, 'id' => $row->getId()]); + $el->data('datagrid-editable-url', $link); - $el->data('datagrid-editable-url', $link); - - $el->data('datagrid-editable-type', $this->editable_element[0]); - $el->data('datagrid-editable-attrs', json_encode($this->editable_element[1])); - } - } + $el->data('datagrid-editable-type', $this->editable_element[0]); + $el->data('datagrid-editable-attrs', json_encode($this->editable_element[1])); } return $el; diff --git a/src/templates/datagrid.latte b/src/templates/datagrid.latte index 184523b1..90cbc5d2 100644 --- a/src/templates/datagrid.latte +++ b/src/templates/datagrid.latte @@ -144,7 +144,7 @@ {foreach $columns as $key => $column} - {var $th = $column->getElementPrototype('th', $key)} + {var $th = $column->getElementForRender('th', $key)} {$th->startTag()|noescape} {var $col_header = 'col-' . $key . '-header'} @@ -200,7 +200,7 @@ {foreach $columns as $key => $column} - {var $th = $column->getElementPrototype('th', $key)} + {var $th = $column->getElementForRender('th', $key)} {$th->startTag()|noescape} {var $col_header = 'col-filter-' . $key . '-header'} {if !$control->hasOuterFilterRendering() && isset($filters[$key])} @@ -249,7 +249,7 @@ {foreach $columns as $key => $column} {var $col = 'col-' . $key} - {var $td = $column->getElementPrototype('td', $key, $row)} + {var $td = $column->getElementForRender('td', $key, $row)} {var $td->class[] = 'datagrid-inline-edit'} {$td->startTag()|noescape} {if isset($filter['inline_edit'][$key])} @@ -277,7 +277,7 @@ {foreach $columns as $key => $column} {php $column = $row->applyColumnCallback($key, clone $column)} - {var $td = $column->getElementPrototype('td', $key, $row)} + {var $td = $column->getElementForRender('td', $key, $row)} {$td->startTag()|noescape} {include column-value, column => $column, row => $row, key => $key} {$td->endTag()|noescape} @@ -419,7 +419,7 @@ {foreach $columns as $key => $column} {var $col = 'col-' . $key} - {var $td = clone $column->getElementPrototype('td', $key)} + {var $td = clone $column->getElementForRender('td', $key)} {var $td->class[] = 'datagrid-inline-edit'} {$td->startTag()|noescape} {if isset($filter['inline_add'][$key])} @@ -441,7 +441,7 @@ {foreach $columns as $key => $column} - {var $td = $column->getElementPrototype('td', $key)} + {var $td = $column->getElementForRender('td', $key)} {$td->startTag()|noescape} {$columnsSummary->render($key)} @@ -459,7 +459,7 @@ {foreach $columns as $key => $column} - {var $td = $column->getElementPrototype('td', $key)} + {var $td = $column->getElementForRender('td', $key)} {$td->startTag()|noescape} {if $aggregation_functions}