#149: Ability to add table inside a cell (nested table)

This commit is contained in:
Ivan Lanin 2014-05-11 17:30:23 +07:00
parent 17e2f02817
commit ded651d947
4 changed files with 25 additions and 10 deletions

View File

@ -13,6 +13,7 @@ This release changed PHPWord license from LGPL 2.1 to LGPL 3.
- TextBox: Ability to add textbox in section, header, and footer - @basjan @ivanlanin GH-228 - TextBox: Ability to add textbox in section, header, and footer - @basjan @ivanlanin GH-228
- TextBox: Ability to add table inside textbox - @basjan GH-231 - TextBox: Ability to add table inside textbox - @basjan GH-231
- HTML: Ability to add elements to PHPWord object via html - @basjan GH-231 - HTML: Ability to add elements to PHPWord object via html - @basjan GH-231
- Table: Ability to add table inside a cell (nested table) - @ivanlanin GH-149
### Bugfixes ### Bugfixes

View File

@ -84,6 +84,16 @@ $table->addCell(2000, $cellVCentered)->addText('C', null, $cellHCentered);
$table->addCell(2000, $cellVCentered)->addText('D', null, $cellHCentered); $table->addCell(2000, $cellVCentered)->addText('D', null, $cellHCentered);
$table->addCell(null, $cellRowContinue); $table->addCell(null, $cellRowContinue);
// 4. Nested table
$section->addTextBreak(2);
$section->addText('Nested table', $header);
$cell = $section->addTable()->addRow()->addCell();
$cell->addText('This cell contains nested table.');
$innerCell = $cell->addTable()->addRow()->addCell();
$innerCell->addText('Inside nested table');
// Save file // Save file
echo write($phpWord, basename(__FILE__, '.php'), $writers); echo write($phpWord, basename(__FILE__, '.php'), $writers);
if (!CLI) { if (!CLI) {

View File

@ -354,7 +354,7 @@ abstract class AbstractContainer extends AbstractElement
'Object' => $allContainers, 'Object' => $allContainers,
'TextRun' => array('section', 'header', 'footer', 'cell', 'textbox'), 'TextRun' => array('section', 'header', 'footer', 'cell', 'textbox'),
'ListItem' => array('section', 'header', 'footer', 'cell', 'textbox'), 'ListItem' => array('section', 'header', 'footer', 'cell', 'textbox'),
'Table' => array('section', 'header', 'footer', 'textbox'), 'Table' => array('section', 'header', 'footer', 'cell', 'textbox'),
'CheckBox' => array('section', 'header', 'footer', 'cell'), 'CheckBox' => array('section', 'header', 'footer', 'cell'),
'TextBox' => array('section', 'header', 'footer', 'cell'), 'TextBox' => array('section', 'header', 'footer', 'cell'),
'Footnote' => array('section', 'textrun', 'cell'), 'Footnote' => array('section', 'textrun', 'cell'),

View File

@ -43,19 +43,23 @@ class Container extends AbstractElement
$containerClass = substr(get_class($container), strrpos(get_class($container), '\\') + 1); $containerClass = substr(get_class($container), strrpos(get_class($container), '\\') + 1);
$withoutP = in_array($containerClass, array('TextRun', 'Footnote', 'Endnote')) ? true : false; $withoutP = in_array($containerClass, array('TextRun', 'Footnote', 'Endnote')) ? true : false;
// Loop through subelements // Loop through elements
$subelements = $container->getElements(); $elements = $container->getElements();
if (count($subelements) > 0) { $elementClass = '';
foreach ($subelements as $subelement) { if (count($elements) > 0) {
$writerClass = str_replace('PhpOffice\\PhpWord\\Element', $this->namespace, get_class($subelement)); foreach ($elements as $element) {
$elementClass = get_class($element);
$writerClass = str_replace('PhpOffice\\PhpWord\\Element', $this->namespace, $elementClass);
if (class_exists($writerClass)) { if (class_exists($writerClass)) {
$writer = new $writerClass($xmlWriter, $subelement, $withoutP); $writer = new $writerClass($xmlWriter, $element, $withoutP);
$writer->write(); $writer->write();
} }
} }
} else { }
// Special case for Cell: They have to contain a TextBreak at least
if ($containerClass == 'Cell') { // Special case for Cell: They have to contain a w:p element at the end
if ($containerClass == 'Cell') {
if ($elementClass == '' || $elementClass == 'PhpOffice\\PhpWord\\Element\\Table') {
$writerClass = "{$this->namespace}\\TextBreak"; $writerClass = "{$this->namespace}\\TextBreak";
$writer = new $writerClass($xmlWriter, new TextBreakElement(), $withoutP); $writer = new $writerClass($xmlWriter, new TextBreakElement(), $withoutP);
$writer->write(); $writer->write();