【问题标题】:Color coding using HTML inside Perl在 Perl 中使用 HTML 进行颜色编码
【发布时间】:2017-06-18 21:46:08
【问题描述】:
$html -> holds the html table code
$query ->holds the data fetched from table(Oracle)

我将数据从 $query 插入到 $html,并根据条件进行颜色编码

代码:

while (my @rec =  $query->fetchrow_array() ) {
  my $file_status = $rec[9];
  my $data_status = $rec[11];
  my $data_status2 = $rec[13];

  if ($file_status eq "Files Received"
    || $data_status eq "Data Not Found"
    || $data_status2 eq "Partial Data") {
    # This changes the font color for all columns in a single row.
    # Is there a way to color code only the specific column?
    $html=$html."\n<font size=3 color=#F12A3D>"; 
  }
  else {
    $html = $html."\n<font size=3><tr>";
  }

  foreach my $x (@rec) {
    if (! defined $x) {
      $x = "";
    }
    $html = $html."<td>$x</td>"
  }
  $html = $html."</tr>"
}

如果列值与条件匹配,请告诉我是否可以使用 perl 中的 html 对特定列进行颜色编码(例如,如果列 8="a" 用于记录,那么我必须打印只有第 2 列的行红色)。如果需要,我可以分享这个脚本的完整 perl 代码。

谢谢, 斯科特

【问题讨论】:

  • 请对您的代码进行布局以使其可读。在寻求免费帮助以使其正常工作时,这是您至少可以做的。
  • /-- ... --/ 不会在 Perl 或任何我知道的语言中创建评论。
  • 如何更改&lt;td&gt;,而不是整个&lt;tr&gt;
  • 我已重新格式化您的代码以使其更易于理解。不客气,但下次请自己做。

标签: html perl


【解决方案1】:

首先,现在是 2017 年。我们不再在 HTML 中使用 &lt;font&gt; 标记。最好在 HTML 标记上设置类,然后使用 CSS 更改外观。

如果您想为单个表格单元格而不是整行着色,那么只需将逻辑移动到生成表格单元格的代码中即可。

foreach my $cell_data (@rec) {
  $cell_data //= ''; # Convert undefined values to empty strings
  my $class;

  if ($cell_data eq 'a') { # for example
    $class = 'hilite';
  }
  $html .= '<td';
  $html .= " class='$class'" if $class;
  $html .= ">$cell_data</td>";
}

我还要指出,像这样将原始 HTML 放入您的 Perl 代码是一个非常糟糕的主意。我强烈建议您改用templating solution

【讨论】:

    【解决方案2】:
    for( my $i=0; $i<@rec; $i++) {
      my $x = $rec[$i] // '';
      if( $i == 1 && ($rec[7]//"") eq 'a' ) {
        $html .= "<td><font size=\"3\" color=\"#FF0000\">$x</font></td>";
      }else{  
        $html .= "<td>$x</td>";
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-07
      • 1970-01-01
      • 2020-01-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多