loris has asked for the wisdom of the Perl Monks concerning the following question:

Dear Row-spanning Monks,

I know I should probably be using something like HTLM::Template, but I am not. I am trying to create a table with spanned rows:

my @headings = ('Foo','Bar'); my @rows = th(\@headings); my @row1 = (td({-rowspan=>'2'},'foo1')); push(@row1,td('bar1')); push(@rows,@row1); my @row2 = (td(['','bar2'])); push(@rows,@row2); my @row3 = (td({-rowspan=>'2'},'foo2')); push(@row3,td('bar3')); push(@rows,@row3); my @row4 = (td(['','bar4'])); push(@rows,@row4); print start_html('foobar'), table({-border=>'1'},Tr(\@rows)), end_html;

However, bar1 and bar3 end up in their own rows, instead of the rows with foo1 and foo2, respectively.

Could anyone let me know what is going on? Thanks.

loris

Replies are listed 'Best First'.
Re: Using rowspan with CGI.pm
by merlyn (Sage) on Jun 24, 2005 at 14:16 UTC
    You have an extra TD in each of your rows below your rowspans. You want the HTML to look like this:
    <table border=1> <tr> <td rowspan=2>foo1</td> <td>bar1</td> </tr> <tr> <td>bar2</td> </tr> <tr> <td rowspan=2>foo2</td> <td>bar3</td> </tr> <tr> <td>bar4</td> </tr> </table>
    Notice how bar2 and bar4 have only one TD in the row.

    -- Randal L. Schwartz, Perl hacker
    Be sure to read my standard disclaimer if this is a reply.

      Hello merlyn,

      You are right, of course. However, even if I remove the extraneous td, I still get bar1 and bar3 ending up in their own row. Why is that?

      Thanks,

      loris

Re: Using rowspan with CGI.pm
by tlm (Prior) on Jun 24, 2005 at 14:30 UTC

    Without looking at your Perl code, one general piece of advice when trying to debug this sort of problem is to first get it right by editing a test HTML file directly. I.e. first figure out what the correct HTML should be, and only after this, worry about how to get your Perl program to generate this HTML. To debug the HTML through the CGI script will drive you nuts.

    the lowliest monk

Re: Using rowspan with CGI.pm
by donarb (Beadle) on Jun 26, 2005 at 18:28 UTC
    The problem is that you are pushing each created <td> onto the @rows array, then using that array to create table rows at the end. What you should do is create the row using just the <td>s for that row and push that onto the @rows array. At the end, you can just use the @rows array as an argument to table(). Your code should look like this:
    my @rows; my @headings = (th(['Foo','Bar'])); push(@rows, Tr(@headings)); my @row1 = (td({-rowspan=>'2'},'foo1')); push(@row1,td('bar1')); push(@rows,Tr(@row1)); my @row2 = (td(['bar2'])); push(@rows,Tr(@row2)); my @row3 = (td({-rowspan=>'2'},'foo2')); push(@row3,td('bar3')); push(@rows,Tr(@row3)); my @row4 = (td(['bar4'])); push(@rows,Tr(@row4)); print start_html('foobar'), table({-border=>'1'},@rows), end_html;
    The above code produces:

    Foo Bar
    foo1 bar1
    bar2
    foo2 bar3
    bar4

      That was exactly what I needed.

      Thanks very much, donarb.