Skip to content

Commit

Permalink
Merge pull request #204 from ThaDafinser/feature/dynamicLabel
Browse files Browse the repository at this point in the history
dynamic action button column label
  • Loading branch information
ThaDafinser committed Aug 31, 2015
2 parents 6462985 + 72821c9 commit 2f173d7
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 7 deletions.
3 changes: 3 additions & 0 deletions docs/03. Columns.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ $viewAction = new Column\Action\Button();
$viewAction->setLabel('View');
$viewAction->setLink('view/url/id');

//or set a dynamic column/row value as label
$viewAction->setLabel($oneColumn);

$actions = new Column\Action();
$actions->setLabel('');
$actions->addAction($viewAction);
Expand Down
25 changes: 21 additions & 4 deletions src/ZfcDatagrid/Column/Action/Button.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?php
namespace ZfcDatagrid\Column\Action;

use ZfcDatagrid\Column\AbstractColumn;

class Button extends AbstractAction
{
protected $label = '';
Expand All @@ -15,16 +17,16 @@ public function __construct()

/**
*
* @param string $name
* @param string|AbstractColumn $name
*/
public function setLabel($name)
{
$this->label = (string) $name;
$this->label = $name;
}

/**
*
* @return string
* @return string|AbstractColumn
*/
public function getLabel()
{
Expand All @@ -36,11 +38,26 @@ public function getLabel()
* @return string
*/
protected function getHtmlType()
{
throw new \Exception('not needed...since we have toHtml() here directly!');
}

/**
*
* @param array $row
* @return string
*/
public function toHtml(array $row)
{
if ($this->getLabel() == '') {
throw new \InvalidArgumentException('A label is required for this action type, please call $action->setLabel()!');
}

return $this->getLabel();
$label = $this->getLabel();
if ($label instanceof AbstractColumn) {
$label = $row[$label->getUniqueId()];
}

return '<a ' . $this->getAttributesString($row) . '>' . $label . '</a>';
}
}
20 changes: 17 additions & 3 deletions tests/ZfcDatagridTest/Column/Action/ButtonTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,35 @@ public function testConstruct()

$this->assertEquals([
'href' => '#',
'class' => 'btn',
'class' => 'btn btn-default',
], $button->getAttributes());
}

public function testLabel()
public function testLabelAndToHtml()
{
$button = new Button();

$button->setLabel('My label');
$this->assertEquals('My label', $button->getLabel());

$html = '<a href="#" class="btn">My label</a>';
$html = '<a href="#" class="btn btn-default">My label</a>';
$this->assertEquals($html, $button->toHtml([]));
}

public function testColumnLabelAndToHtml()
{
$col = $this->getMockForAbstractClass('ZfcDatagrid\Column\AbstractColumn');
$col->setUniqueId('myCol');

$button = new Button();

$button->setLabel($col);
$this->assertInstanceOf('ZfcDatagrid\Column\AbstractColumn', $button->getLabel());

$html = '<a href="#" class="btn btn-default">Blubb</a>';
$this->assertEquals($html, $button->toHtml(['myCol' => 'Blubb']));
}

public function testHtmlException()
{
$button = new Button();
Expand Down

0 comments on commit 2f173d7

Please sign in to comment.