Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated Table Console helper for spanning cols and rows #5861

Merged
merged 2 commits into from
Nov 8, 2015
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 91 additions & 0 deletions components/console/helpers/table.rst
Original file line number Diff line number Diff line change
Expand Up @@ -143,3 +143,94 @@ Here is a full list of things you can customize:
$table->setStyle('colorful');

This method can also be used to override a built-in style.

Spanning multiple columns and rows
----------------------------------

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing versionadded directive saying in which version this is available

.. versionadded:: 2.7
Spanning multiple columns and rows was introduced in Symfony 2.7.

To make a table cell which spans multiple columns you can use a :class:`Symfony\\Component\\Console\\Helper\\TableCell`::

use Symfony\Component\Console\Helper\Table;
use Symfony\Component\Console\Helper\TableSeparator;
use Symfony\Component\Console\Helper\TableCell;

$table = new Table($output);
$table
->setHeaders(array('ISBN', 'Title', 'Author'))
->setRows(array(
array('99921-58-10-7', 'Divine Comedy', 'Dante Alighieri'),
new TableSeparator(),
array(new TableCell('This value spans 3 columns.', array('colspan' => 3))),
))
;
$table->render();

which results in:

.. code-block:: text

+---------------+---------------+-----------------+
| ISBN | Title | Author |
+---------------+---------------+-----------------+
| 99921-58-10-7 | Divine Comedy | Dante Alighieri |
+---------------+---------------+-----------------+
| This value spans 3 columns. |
+---------------+---------------+-----------------+

.. tip::

You can create a multiple-line page title using a header cell that spans
the enire table width::

$table->setHeaders(array(
array(new TableCell('Main table title', array('colspan' => 3))),
array('ISBN', 'Title', 'Author'),
))
// ...

This generates:

.. code-block:: text

+-------+-------+--------+
| Main table title |
+-------+-------+--------+
| ISBN | Title | Author |
+-------+-------+--------+
| ... |
+-------+-------+--------+

In a similar way you can span multiple rows::

use Symfony\Component\Console\Helper\Table;
use Symfony\Component\Console\Helper\TableCell;

$table = new Table($output);
$table
->setHeaders(array('ISBN', 'Title', 'Author'))
->setRows(array(
array(
new TableCell("This ISBN is spanning\nmultiple rows", array('rowspan' => 2)),
'A Tale of Two Cities',
'Charles Dickens',
),
array('Divine Comedy', 'Dante Alighieri'),
))
;
$table->render();

which results in:

.. code-block:: text

+-----------------------+----------------------+-----------------+
| ISBN | Title | Author |
+-----------------------+----------------------+-----------------+
| This ISBN is spanning | A Tale of Two Cities | Charles Dickens |
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for being picky, but having two different books with the same ISBN doesn't make sense to me. What if we update this example to have two different books (different ISBNs and Titles) written by the same author (that's the "rowspan")?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea. I've changed the rowspan to use the author column.

| multiple rows | Divine Comedy | Dante Alighieri |
+-----------------------+----------------------+-----------------+

You can use the ``colspan`` and ``rowspan`` options at the same time which allows
you to create any table layout you may wish.