Fixed colspan issues with multiple render() calls
This commit is contained in:
parent
5f36605e0e
commit
b93bcc1153
@ -205,24 +205,26 @@ class Table
|
|||||||
public function render()
|
public function render()
|
||||||
{
|
{
|
||||||
$this->calculateNumberOfColumns();
|
$this->calculateNumberOfColumns();
|
||||||
$this->rows = $this->buildTableRows($this->rows);
|
$rows = $this->buildTableRows($this->rows);
|
||||||
$this->headers = $this->buildTableRows($this->headers);
|
$headers = $this->buildTableRows($this->headers);
|
||||||
|
|
||||||
|
$this->calculateColumnsWidth(array_merge($headers, $rows));
|
||||||
|
|
||||||
$this->renderRowSeparator();
|
$this->renderRowSeparator();
|
||||||
if (!empty($this->headers)) {
|
if (!empty($headers)) {
|
||||||
foreach ($this->headers as $header) {
|
foreach ($headers as $header) {
|
||||||
$this->renderRow($header, $this->style->getCellHeaderFormat());
|
$this->renderRow($header, $this->style->getCellHeaderFormat());
|
||||||
$this->renderRowSeparator();
|
$this->renderRowSeparator();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
foreach ($this->rows as $row) {
|
foreach ($rows as $row) {
|
||||||
if ($row instanceof TableSeparator) {
|
if ($row instanceof TableSeparator) {
|
||||||
$this->renderRowSeparator();
|
$this->renderRowSeparator();
|
||||||
} else {
|
} else {
|
||||||
$this->renderRow($row, $this->style->getCellRowFormat());
|
$this->renderRow($row, $this->style->getCellRowFormat());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (!empty($this->rows)) {
|
if (!empty($rows)) {
|
||||||
$this->renderRowSeparator();
|
$this->renderRowSeparator();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -246,7 +248,7 @@ class Table
|
|||||||
|
|
||||||
$markup = $this->style->getCrossingChar();
|
$markup = $this->style->getCrossingChar();
|
||||||
for ($column = 0; $column < $count; $column++) {
|
for ($column = 0; $column < $count; $column++) {
|
||||||
$markup .= str_repeat($this->style->getHorizontalBorderChar(), $this->getColumnWidth($column)).$this->style->getCrossingChar();
|
$markup .= str_repeat($this->style->getHorizontalBorderChar(), $this->columnWidths[$column]).$this->style->getCrossingChar();
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->output->writeln(sprintf($this->style->getBorderFormat(), $markup));
|
$this->output->writeln(sprintf($this->style->getBorderFormat(), $markup));
|
||||||
@ -292,11 +294,11 @@ class Table
|
|||||||
private function renderCell(array $row, $column, $cellFormat)
|
private function renderCell(array $row, $column, $cellFormat)
|
||||||
{
|
{
|
||||||
$cell = isset($row[$column]) ? $row[$column] : '';
|
$cell = isset($row[$column]) ? $row[$column] : '';
|
||||||
$width = $this->getColumnWidth($column);
|
$width = $this->columnWidths[$column];
|
||||||
if ($cell instanceof TableCell && $cell->getColspan() > 1) {
|
if ($cell instanceof TableCell && $cell->getColspan() > 1) {
|
||||||
// add the width of the following columns(numbers of colspan).
|
// add the width of the following columns(numbers of colspan).
|
||||||
foreach (range($column + 1, $column + $cell->getColspan() - 1) as $nextColumn) {
|
foreach (range($column + 1, $column + $cell->getColspan() - 1) as $nextColumn) {
|
||||||
$width += $this->getColumnSeparatorWidth() + $this->getColumnWidth($nextColumn);
|
$width += $this->getColumnSeparatorWidth() + $this->columnWidths[$nextColumn];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -509,21 +511,20 @@ class Table
|
|||||||
*
|
*
|
||||||
* @return int
|
* @return int
|
||||||
*/
|
*/
|
||||||
private function getColumnWidth($column)
|
private function calculateColumnsWidth($rows)
|
||||||
{
|
{
|
||||||
if (isset($this->columnWidths[$column])) {
|
for ($column = 0; $column < $this->numberOfColumns; $column++) {
|
||||||
return $this->columnWidths[$column];
|
$lengths = array();
|
||||||
}
|
foreach ($rows as $row) {
|
||||||
|
if ($row instanceof TableSeparator) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
foreach (array_merge($this->headers, $this->rows) as $row) {
|
$lengths[] = $this->getCellWidth($row, $column);
|
||||||
if ($row instanceof TableSeparator) {
|
|
||||||
continue;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$lengths[] = $this->getCellWidth($row, $column);
|
$this->columnWidths[$column] = max($lengths) + strlen($this->style->getCellRowContentFormat()) - 2;
|
||||||
}
|
}
|
||||||
|
|
||||||
return $this->columnWidths[$column] = max($lengths) + strlen($this->style->getCellRowContentFormat()) - 2;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -180,10 +180,10 @@ TABLE
|
|||||||
array(
|
array(
|
||||||
array('ISBN', 'Title', 'Author'),
|
array('ISBN', 'Title', 'Author'),
|
||||||
array(
|
array(
|
||||||
array("99921-58-10-7", "Divine\nComedy", "Dante Alighieri"),
|
array('99921-58-10-7', "Divine\nComedy", 'Dante Alighieri'),
|
||||||
array("9971-5-0210-2", "Harry Potter\nand the Chamber of Secrets", "Rowling\nJoanne K."),
|
array('9971-5-0210-2', "Harry Potter\nand the Chamber of Secrets", "Rowling\nJoanne K."),
|
||||||
array("9971-5-0210-2", "Harry Potter\nand the Chamber of Secrets", "Rowling\nJoanne K."),
|
array('9971-5-0210-2', "Harry Potter\nand the Chamber of Secrets", "Rowling\nJoanne K."),
|
||||||
array("960-425-059-0", "The Lord of the Rings", "J. R. R.\nTolkien"),
|
array('960-425-059-0', 'The Lord of the Rings', "J. R. R.\nTolkien"),
|
||||||
),
|
),
|
||||||
'default',
|
'default',
|
||||||
<<<TABLE
|
<<<TABLE
|
||||||
@ -427,7 +427,7 @@ TABLE
|
|||||||
array('ISBN', 'Author'),
|
array('ISBN', 'Author'),
|
||||||
array(
|
array(
|
||||||
array(
|
array(
|
||||||
new TableCell("9971-5-0210-0", array('rowspan' => 3, 'colspan' => 1)),
|
new TableCell('9971-5-0210-0', array('rowspan' => 3, 'colspan' => 1)),
|
||||||
'Dante Alighieri',
|
'Dante Alighieri',
|
||||||
),
|
),
|
||||||
array(new TableSeparator()),
|
array(new TableSeparator()),
|
||||||
@ -547,6 +547,33 @@ TABLE;
|
|||||||
| Bar3 |
|
| Bar3 |
|
||||||
+------+
|
+------+
|
||||||
|
|
||||||
|
TABLE;
|
||||||
|
|
||||||
|
$this->assertEquals($expected, $this->getOutputContent($output));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testRenderMultiCalls()
|
||||||
|
{
|
||||||
|
$table = new Table($output = $this->getOutputStream());
|
||||||
|
$table->setRows(array(
|
||||||
|
array(new TableCell('foo', array('colspan' => 2))),
|
||||||
|
));
|
||||||
|
$table->render();
|
||||||
|
$table->render();
|
||||||
|
$table->render();
|
||||||
|
|
||||||
|
$expected =
|
||||||
|
<<<TABLE
|
||||||
|
+---+--+
|
||||||
|
| foo |
|
||||||
|
+---+--+
|
||||||
|
+---+--+
|
||||||
|
| foo |
|
||||||
|
+---+--+
|
||||||
|
+---+--+
|
||||||
|
| foo |
|
||||||
|
+---+--+
|
||||||
|
|
||||||
TABLE;
|
TABLE;
|
||||||
|
|
||||||
$this->assertEquals($expected, $this->getOutputContent($output));
|
$this->assertEquals($expected, $this->getOutputContent($output));
|
||||||
|
Reference in New Issue
Block a user