Skip to content
This repository has been archived by the owner on Jan 16, 2018. It is now read-only.

Commit

Permalink
Fix sub CellView building of CompoundColumns
Browse files Browse the repository at this point in the history
  • Loading branch information
sstok committed Oct 6, 2016
1 parent 7dde2ac commit 6fd9380
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 22 deletions.
4 changes: 1 addition & 3 deletions src/Extension/Core/Type/BaseType.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,7 @@ public function buildHeaderView(HeaderView $view, ColumnInterface $column, array
'unique_block_prefix' => $uniqueBlockPrefix,
'block_prefixes' => $blockPrefixes,
// The cache key is used for caching in the render-engine.
// multiple columns can share the same block-name but have a different type.
// To prevent rendering the wrong block use the type also, this is rather an
// edge-case but Symfony Form also does it this way, so lets not cause confusion.
// multiple columns can share the same block-name but have a different type (sub columns).
'cache_key' => $uniqueBlockPrefix.'_'.$column->getType()->getBlockPrefix(),
]);
}
Expand Down
44 changes: 42 additions & 2 deletions src/Extension/Core/Type/CompoundColumnType.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Rollerworks\Component\Datagrid\Column\CellView;
use Rollerworks\Component\Datagrid\Column\ColumnInterface;
use Rollerworks\Component\Datagrid\Column\CompoundColumn;
use Rollerworks\Component\Datagrid\Column\HeaderView;

/**
* CompoundColumn allows multiple sub-columns for advanced view building.
Expand All @@ -37,19 +38,58 @@ function ($data) {
);
}

/**
* @param HeaderView $view
* @param ColumnInterface|CompoundColumn $column
* @param array $options
*/
public function buildHeaderView(HeaderView $view, ColumnInterface $column, array $options)
{
parent::buildHeaderView($view, $column, $options);

// The header information contains the actual block information (and cache key)
$datagrid = $view->datagrid;

$headers = [];

foreach ($column->getColumns() as $subColumn) {
$headers[$subColumn->getName()] = $subColumn->createHeaderView($datagrid);
}

$view->vars['_sub_headers'] = $headers;
}

/**
* {@inheritdoc}
*/
public function buildCellView(CellView $view, ColumnInterface $column, array $options)
{
$parent = $view->column;

// Set shared information from the header.
// This information is not recomputed for better performance.
// Each header is created once, but this method will be called
// 5000 times for a grid with 500 rows!

$view->vars = array_replace($view->vars, [
'cell_attr' => $options['cell_attr'],
'unique_block_prefix' => $parent->vars['unique_block_prefix'],
'block_prefixes' => $parent->vars['block_prefixes'],
'cache_key' => $parent->vars['cache_key'],
]);

$cells = [];

$headers = $view->column->vars['_sub_headers'];

/** @var CompoundColumn $column */
foreach ($column->getColumns() as $subColumn) {
$subView = $subColumn->createCellView($view->column, $view->source, $view->vars['row']);
$name = $subColumn->getName();

$subView = $subColumn->createCellView($headers[$name], $view->source, $view->vars['row']);
$subView->vars['compound'] = true;

$cells[$subColumn->getName()] = $subView;
$cells[$name] = $subView;
}

return $view->value = $cells;
Expand Down
73 changes: 56 additions & 17 deletions tests/Extension/Core/Type/CompoundColumnTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,10 @@ public function testPassLabelToView()
$datagridView = $datagrid->createView();
$view = $rootColumn->createHeaderView($datagridView);

$this->assertSame('Ids', $view->label);
// Comparing this is to much work at this stage.
unset($view->vars['_sub_headers']);

self::assertSame('Ids', $view->label);
self::assertViewVarsEquals(
[
'label_attr' => [],
Expand Down Expand Up @@ -82,30 +85,66 @@ public function testSubCellsToView()

$datagridView = $datagrid->createView();

$view = $column->createCellView($datagridView->columns['actions'], $object, 0);
$row = $datagridView->rows[1]->cells;

// Actions (root) column
$view = $row['actions'];

self::assertDatagridCompoundCell('age', ['unique_block_prefix' => '_grid_actions', 'row' => 1], $view);
self::assertDatagridCompoundCell('name', ['unique_block_prefix' => '_grid_actions', 'row' => 1], $view);

$this->assertDatagridCell('age', $view);
$this->assertDatagridCell('name', $view);
self::assertArrayHasKey('age', $view->value);
self::assertArrayHasKey('name', $view->value);
self::assertArrayNotHasKey('key', $view->value);

$this->assertEquals('42', $view->value['age']->value);
$this->assertEquals(' sheldon ', $view->value['name']->value);
$this->assertArrayNotHasKey('key', $view->value);
// Internal cells
$cells = $view->value;

$headerView = $columns['age']->createHeaderView($datagridView);
self::assertDatagridCell(
'age',
'42',
[
'unique_block_prefix' => '_grid_actions_age',
'block_prefixes' => ['column', 'number', '_grid_actions_age'],
'cache_key' => '_grid_actions_age_number',
'row' => 1,
'compound' => true,
],
$cells['age']
);

$this->assertEquals('_grid_actions_age', $headerView->vars['unique_block_prefix']);
$this->assertEquals(['column', 'number', '_grid_actions_age'], $headerView->vars['block_prefixes']);
self::assertDatagridCell(
'name',
' sheldon ',
[
'unique_block_prefix' => '_my_named',
'block_prefixes' => ['column', 'text', '_my_named'],
'cache_key' => '_my_named_text',
'row' => 1,
'compound' => true,
],
$cells['name']
);
}

$headerView = $columns['name']->createHeaderView($datagridView);
private static function assertDatagridCell($name, $value, array $vars, CellView $view)
{
self::assertEquals($name, $view->name);
self::assertEquals($value, $view->value);

$this->assertEquals('_my_named', $headerView->vars['unique_block_prefix']);
$this->assertEquals(['column', 'text', '_my_named'], $headerView->vars['block_prefixes']);
if ([] !== $vars) {
self::assertViewVarsEquals($vars, $view);
}
}

private function assertDatagridCell($name, CellView $view)
private function assertDatagridCompoundCell($name, array $vars, CellView $view)
{
$this->assertInternalType('array', $view->value);
$this->assertArrayHasKey($name, $view->value);
$this->assertInstanceOf(CellView::class, $view->value[$name]);
self::assertInternalType('array', $view->value);
self::assertArrayHasKey($name, $view->value);
self::assertInstanceOf(CellView::class, $view->value[$name]);

if ([] !== $vars) {
self::assertViewVarsEquals($vars, $view);
}
}
}

0 comments on commit 6fd9380

Please sign in to comment.