Skip to content

Commit

Permalink
Column::getElementPrototype() update
Browse files Browse the repository at this point in the history
  • Loading branch information
paveljanda committed Jan 7, 2017
1 parent 857ef7e commit 385c031
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 24 deletions.
50 changes: 33 additions & 17 deletions src/Column/Column.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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;
Expand Down
14 changes: 7 additions & 7 deletions src/templates/datagrid.latte
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@
<input n:class="$control->useHappyComponents() ? 'happy gray-border' , primary" name="{$control->getName()|lower}-toggle-all" type="checkbox" data-check="{$control->getName()}" data-check-all="{$control->getName()}">
</th>
{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'}

Expand Down Expand Up @@ -200,7 +200,7 @@
</tr>
<tr n:block="header-filters" n:if="!empty($filters) && !$control->hasOuterFilterRendering()">
{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])}
Expand Down Expand Up @@ -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])}
Expand Down Expand Up @@ -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}
Expand Down Expand Up @@ -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])}
Expand All @@ -441,7 +441,7 @@
<td n:if="$control->hasGroupActions()" class="col-checkbox"></td>

{foreach $columns as $key => $column}
{var $td = $column->getElementPrototype('td', $key)}
{var $td = $column->getElementForRender('td', $key)}

{$td->startTag()|noescape}
{$columnsSummary->render($key)}
Expand All @@ -459,7 +459,7 @@
<td n:if="$control->hasGroupActions()" class="col-checkbox"></td>

{foreach $columns as $key => $column}
{var $td = $column->getElementPrototype('td', $key)}
{var $td = $column->getElementForRender('td', $key)}

{$td->startTag()|noescape}
{if $aggregation_functions}
Expand Down

0 comments on commit 385c031

Please sign in to comment.