Skip to content

Commit 84202d6

Browse files
committed
feature symfony#5861 Updated Table Console helper for spanning cols and rows (hiddewie)
This PR was merged into the 2.7 branch. Discussion ---------- Updated Table Console helper for spanning cols and rows | Q | A | ------------- | --- | Doc fix? | no | New docs? | yes, symfony/symfony#13438 | Applies to | 2.7+ | Fixed tickets | symfony#5119 Commits ------- 7ea6441 Updated rowspan example to be more logical to humans ef3a7de Updated Table Console helper for spanning cols and rows
2 parents d96d75e + 7ea6441 commit 84202d6

File tree

1 file changed

+91
-0
lines changed

1 file changed

+91
-0
lines changed

components/console/helpers/table.rst

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,3 +143,94 @@ Here is a full list of things you can customize:
143143
$table->setStyle('colorful');
144144

145145
This method can also be used to override a built-in style.
146+
147+
Spanning multiple columns and rows
148+
----------------------------------
149+
150+
.. versionadded:: 2.7
151+
Spanning multiple columns and rows was introduced in Symfony 2.7.
152+
153+
To make a table cell which spans multiple columns you can use a :class:`Symfony\\Component\\Console\\Helper\\TableCell`::
154+
155+
use Symfony\Component\Console\Helper\Table;
156+
use Symfony\Component\Console\Helper\TableSeparator;
157+
use Symfony\Component\Console\Helper\TableCell;
158+
159+
$table = new Table($output);
160+
$table
161+
->setHeaders(array('ISBN', 'Title', 'Author'))
162+
->setRows(array(
163+
array('99921-58-10-7', 'Divine Comedy', 'Dante Alighieri'),
164+
new TableSeparator(),
165+
array(new TableCell('This value spans 3 columns.', array('colspan' => 3))),
166+
))
167+
;
168+
$table->render();
169+
170+
which results in:
171+
172+
.. code-block:: text
173+
174+
+---------------+---------------+-----------------+
175+
| ISBN | Title | Author |
176+
+---------------+---------------+-----------------+
177+
| 99921-58-10-7 | Divine Comedy | Dante Alighieri |
178+
+---------------+---------------+-----------------+
179+
| This value spans 3 columns. |
180+
+---------------+---------------+-----------------+
181+
182+
.. tip::
183+
184+
You can create a multiple-line page title using a header cell that spans
185+
the enire table width::
186+
187+
$table->setHeaders(array(
188+
array(new TableCell('Main table title', array('colspan' => 3))),
189+
array('ISBN', 'Title', 'Author'),
190+
))
191+
// ...
192+
193+
This generates:
194+
195+
.. code-block:: text
196+
197+
+-------+-------+--------+
198+
| Main table title |
199+
+-------+-------+--------+
200+
| ISBN | Title | Author |
201+
+-------+-------+--------+
202+
| ... |
203+
+-------+-------+--------+
204+
205+
In a similar way you can span multiple rows::
206+
207+
use Symfony\Component\Console\Helper\Table;
208+
use Symfony\Component\Console\Helper\TableCell;
209+
210+
$table = new Table($output);
211+
$table
212+
->setHeaders(array('ISBN', 'Title', 'Author'))
213+
->setRows(array(
214+
array(
215+
'978-0521567817',
216+
'De Monarchia',
217+
new TableCell("Dante Alighieri\nspans multiple rows", array('rowspan' => 2)),
218+
),
219+
array('978-0804169127', 'Divine Comedy'),
220+
))
221+
;
222+
$table->render();
223+
224+
which results in:
225+
226+
.. code-block:: text
227+
228+
+----------------+---------------+---------------------+
229+
| ISBN | Title | Author |
230+
+----------------+---------------+---------------------+
231+
| 978-0521567817 | De Monarchia | Dante Alighieri |
232+
| 978-0804169127 | Divine Comedy | spans multiple rows |
233+
+----------------+---------------+---------------------+
234+
235+
You can use the ``colspan`` and ``rowspan`` options at the same time which allows
236+
you to create any table layout you may wish.

0 commit comments

Comments
 (0)