简体   繁体   English

根据文本内容设置HTML表格单元格背景色

[英]Set HTML table cell background colour according to text content

I have written a Perl program to create a web page with an HTML table derived from text file textfile.txt . 我已经编写了一个Perl程序来创建一个带有从文本文件textfile.txt派生的HTML表的网页。

I would like to change it so that cells of the table are coloured according to the text content. 我想对其进行更改,以使表格的单元格根据文本内容着色。 For instance, if the text is Reject then background of the cell should be red. 例如,如果文本为“ Reject则单元格的背景应为红色。

Here are two methods that I tried. 这是我尝试过的两种方法。 Neither of them worked 他们俩都没有工作

Method 1 方法1

if ( $_ eq "REJECT" ) {
    print map { "<td style=width:705 bgcolor=#FF0000 >REJECT</td>" } @$d;
}

Method 2 方法二

foreach my $d ( @data ) {

    $d //= '';    # Convert undefined values to empty strings

    my $class;

    if ( $d eq 'REJECT' ) {
        $class = 'hilite';
    }

    $html .= '<td';
    $html .= " class='$class'" if $class;
    $html .= ">$d</td>";
}

Perl program Perl程序

#!/usr/bin/perl

print "Content-type: text/html\n\n";

use strict;
use warnings;

my $output = `cat textfile.txt`;
my @lines = split /\n/, $output;

my @data;

foreach my $line ( @lines ) {
    chomp $line;
    my @d = split /\s+/, $line;
    push @data, \@d;
}

my $color1 = "black";
my $color2 = "darkgreen";
my $color3 = "black";
my $color4 = "red";
my $color5 = "lime";

my $num    = 6;
my $title  = "This is the heading";
my $fstyle = "Helvetica";

print "<body bgcolor = $color3>";
print "<font color = $color5  face = $fstyle  size = $num>$title</font><br />";

foreach my $d ( @data ) {

    print "<html>";
    print "<body>";
    print "<table style=table-layout= fixed width= 705 height=110 text = $color4 border = 2 bordercolor = $color1 bgcolor = $color2>";
    print "<tr>";
    print map {"<th style=width:705 >Column1</th>"}
            print map {"<th style=width:705 >Column2</th>"}
            print "</tr>";
    print "<tr>";
    print map {"<td style=width:705 >$_</td>"} @$d;

    if ( $d eq 'REJECT' ) {
        print map {"<td style=width:705 bgcolor=#FF0000 >Reject</td>"} @$d;
    }

    print "</tr>";
    print "</table>";
    print "</body>";
    print "</html>";
}

Input text file: 输入文字档:

Column1 Column2
Accept   Reject
Accept   Reject
Accept   Reject

This line 这条线

print map { "<td style=width:705 bgcolor=#FF0000 >Reject</td>"

is adding the background color RED to the cell but it is not matching the condition Reject . 正在将背景色RED添加到单元格中,但与条件Reject不匹配。

Output 输出量

在此处输入图片说明

Here are some of the errors in your Perl code 这是您的Perl代码中的一些错误

  • As I said, you are misusing map 正如我所说,您滥用map

  • You are creating a new HTML document for every element of @data . 您正在为@data每个元素创建一个新的HTML文档。 What do you expect the browser to do with multiple <html> elements? 您期望浏览器如何处理多个<html>元素? It can't display them all 它不能全部显示

  • You are expecting the string REJECT to match Reject 您期望字符串REJECT匹配Reject

  • You are using a mixture of CSS style strings and element attributes. 您正在混合使用CSS style字符串和元素属性。 For instance 例如

     print "<table style=table-layout= fixed width= 705 height=110 text = $color4 border = 2 bordercolor = $color1 bgcolor = $color2>" 

    should be 应该

     print qq{<table style="table-layout:fixed; width=705; height=110; text=$color4" border=2 bordercolor="$color1" bgcolor="$color2">\\n} 

    because table-layout , width , height , and text are CSS properties, while border , bordercolor , and bgcolor are HTML element attributes 因为table-layoutwidthheighttext是CSS属性,而borderbordercolorbgcolor是HTML元素属性

    I think you should be writing CSS to solve this problem, but that is another matter 我认为您应该编写CSS来解决此问题,但这是另一回事

It would also help you a lot if you printed a newline "\\n" after each HTML element. 如果在每个HTML元素之后打印换行符"\\n" ,它也会对您有很大帮助。 That way the output will be much more readable and you will be able to see better what you have created 这样,输出将更具可读性,您将能够更好地看到自己创建的内容

Please don't persist with this "try things until it works" approach. 请不要坚持这种“尝试直到成功”的方法。 You always end up coming here for help to get you out of the mess you've created, and you're not asking intelligent questions. 您总是总是来这里寻求帮助,以帮助您摆脱已创建的混乱局面,而且您也不会问聪明的问题。 To be using map like that after so long means you're not learning at all, and you owe it to yourself as well as your employer to learn the language properly 长时间使用这样的map意味着您根本不学习,而应归功于自己和您的雇主,以正确地学习语言

Here is a solution that performs correctly, but it is no more than a correction of your own code. 这是一个可以正确执行的解决方案,但这仅是对您自己的代码的更正。 The problems that I have outlined have been fixed, and that is all 我概述的问题已经解决,仅此而已

#!/usr/bin/perl

use strict;
use warnings 'all';

my $color1 = 'black';
my $color2 = 'darkgreen';
my $color3 = 'black';
my $color4 = 'red';
my $color5 = 'lime';

my $size   = 6;
my $title  = 'This is the heading';
my $fstyle = 'Helvetica';

print "Content-type: text/html\n\n";

print "<body bgcolor = $color3>\n";
print "<font color = $color5 face=$fstyle size=$size>$title</font><br />\n";

{
    print "<html>\n";
    print "<body>\n";

    print qq{<table
            style="table-layout:fixed; width=705; height=110; text=$color4"
            border=2
            bordercolor="$color1"
            bgcolor="$color2">\n};

    print "<tr>\n";
    print qq{<th style="width:705" >Column1</th>};
    print qq{<th style="width:705" >Column2</th>};
    print "</tr>\n";

    open my $fh, '<', 'textfile.txt' or die $!;

    while ( <$fh> ) {

        my @line = split;

        print "<tr>\n";

        for ( @line ) {

            if ( /reject/i ) {
                print qq{<td style=width:705 bgcolor=red>$_</td>};
            }
            else {
                print "<td style=width:705>$_</td>"
            }
        }

        print "</tr>\n";
    }

    print "</table>\n";
    print "</body>\n";
    print "</html>\n";
}

output 输出

Content-type: text/html

<body bgcolor = black>
<font color = lime face=Helvetica size=6>This is the heading</font><br />
<html>
<body>
<table
        style="table-layout:fixed; width=705; height=110; text=red"
        border=2
        bordercolor="black"
        bgcolor="darkgreen">
<tr>
<th style="width:705" >Column1</th><th style="width:705" >Column2</th></tr>
<tr>
<td style=width:705>Column1</td><td style=width:705>Column2</td></tr>
<tr>
<td style=width:705>Accept</td><td style=width:705 bgcolor=red>Reject</td></tr>
<tr>
<td style=width:705>Accept</td><td style=width:705 bgcolor=red>Reject</td></tr>
<tr>
<td style=width:705>Accept</td><td style=width:705 bgcolor=red>Reject</td></tr>
</table>
</body>
</html>

Appearance 出现

这是结果

I still have misgivings about your approach. 我仍然对您的做法存有疑虑。 Hacking together a program from bits and pieces of others' work that you don't understand is a recipe for failure. 从您不了解的其他零碎工作中窃取一个程序是失败的秘诀。 If you have no inclination to explore and learn the details enough to survive on your own then you have chosen the wrong job 如果您无意探索和学习足以独自生存的细节,那么您选择了错误的工作

  • I think you should be using a template system such as Template::Toolkit instead of printing HTML from your Perl program 我认为您应该使用诸如Template::Toolkit类的模板系统,而​​不是从Perl程序中打印HTML

  • The colours should be changed using CSS and an appropriate class , rather than printing HTML attributes in line 应该使用CSS和适当的class来更改颜色,而不是一行打印HTML属性

You seem to think that a casual and approximate approach is fine, or at least that you are unwilling to offer any more, but while that may be true of other professions, software engineering requires much more care and precision 您似乎认为休闲和近似的方法不错,或者至少您不愿意提供更多,但是尽管其他职业可能如此,但是软件工程需要更多的关注和精确度

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM