【问题标题】:How to format the first column in a Symfony Table the same as the first row?如何将 Symfony 表中的第一列格式化为与第一行相同?
【发布时间】:2017-10-25 09:26:48
【问题描述】:

Symfony Table 通常将第一行的颜色与其他行不同。但是,我希望第一行和第一列具有相同的样式。我知道存在TableStyle,但我不知道如何为第一列+第一行提供相同的样式。在此示例表中,我希望第一列也为绿色(文本行 A、行 B 和行 C)。

可以使用以下代码生成此示例表:

$table = new Table($output);
$table
    ->setHeaders(array('Col A', 'Col B', 'Col C'))
    ->setRows(array(
        array('Row A', 'Divine Comedy', 'Dante Alighieri'),
        array('Row B', 'A Tale of Two Cities', 'Charles Dickens'),
        array('Row C', 'The Lord of the Rings', 'J. R. R. Tolkien'),
    ))
;
$table->render();

干杯

【问题讨论】:

    标签: php symfony symfony-console


    【解决方案1】:

    您需要创建TableStyle 实例并像这样设置cellRowContentFormat

    <?php
    
    use Symfony\Component\Console\Helper\Table;
    
    require_once 'vendor/autoload.php';
    
    $output = new Symfony\Component\Console\Output\ConsoleOutput();
    $table = new Table($output);
    $style = new \Symfony\Component\Console\Helper\TableStyle();
    $style->setCellRowContentFormat('<info> %s </info>');
    $table->setColumnStyle(0, $style);
    $table
        ->setHeaders(array('Col A', 'Col B', 'Col C'))
        ->setRows(array(
            array('Row A', 'Divine Comedy', 'Dante Alighieri'),
            array('Row B', 'A Tale of Two Cities', 'Charles Dickens'),
            array('Row C', 'The Lord of the Rings', 'J. R. R. Tolkien'),
        ))
    ;
    $table->render();
    

    您可以查看TableStyle::$cellRowContentFormatTableStyle::$cellHeaderFormat 成员的默认值:默认情况下$cellHeaderFormat'&lt;info&gt;%s&lt;/info&gt;',此值使单元格标题变为绿色(信息颜色)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-09-23
      • 2018-08-03
      • 2017-04-06
      • 2011-02-22
      • 1970-01-01
      • 2017-12-17
      相关资源
      最近更新 更多