| Category: | CGI Programing |
| Author/Contact Info | Houblon |
| Description: | I had to make many tables with a special font face. Instead of
writing the font code in each cell I made up this object that
print a table. I'm not an experienced perl programmer so, I'm sure
there is better way to do it, but it's working well for me and if
any one what's to use, go ahead. Any suggestion to make it better
will still be appreciate.
|
package TableMaker;
use strict;
use vars '$AUTOLOAD';
use CGI qw/:standard :html3/;
sub new {
my $this = shift;
my $class = ref($this) || $this;
my $self = {};
$self->{face} = undef;
$self->{color} = undef;
$self->{size} = undef;
$self->{width} = undef;
$self->{border} = undef;
$self->{cellspcacing} = undef;
$self->{cols} = 1;
$self->{cells} = [];
bless($self, $class);
return $self;
}
sub AUTOLOAD {
no strict 'refs';
my ($self, $val) = @_;
my ($name) = $AUTOLOAD =~ m/.*::(\w*)/;
return defined $val ? $self->{$name} = $val : $self->{$name};
}
sub addCell {
my $self = shift;
my @opt = qw/text face color size colspan rowspan align valign width
+ height/;
my (%font, %td);
my ($cs, $rs);
if ( ref($_[0]) eq "HASH" ) {
foreach ( @opt ) {
if ( $_ =~ /text|face|color/ ) {
if ( $_[0]->{$_} ) {
$font{$_} = $_[0]->{$_}
} elsif ( $_[0]->{"-$_"} ) {
$font{$_} = $_[0]->{"-$_"}
}elsif ( $self->{$_} ) {
$font{$_} = $self->{$_}
}
} elsif ( $_ eq 'colspan' ) {
if ( $_[0]->{$_} ) {
$td{$_} = $_[0]->{$_};
$cs = $_[0]->{$_}
} elsif ( $_[0]->{"-$_"} ) {
$td{$_} = $_[0]->{"-$_"};
$cs = $_[0]->{"-$_"}
}
} elsif ( $_ eq 'rowspan' ) {
if ( $_[0]->{$_} ) {
$td{$_} = $_[0]->{$_};
$rs = $_[0]->{$_}
} elsif ( $_[0]->{"-$_"} ) {
$td{$_} = $_[0]->{"-$_"};
$rs = $_[0]->{"-$_"}
}
} elsif ( $_[0]->{$_} ) {
$td{$_} = $_[0]->{$_};
} elsif ( $_[0]->{"-$_"} ) {
$td{$_} = $_[0]->{"-$_"};
}
}
if (%font) {
push(@{ $self->{cells} }, [td(\%td,font(\%font,$_[1])), $cs, $rs ]
+);
} else {
push(@{ $self->{cells} }, [td(\%td,$_[1]), $cs, $rs ]);
}
} else {
push(@{ $self->{cells} }, [td($_[0]), $cs, $rs ]);
}
}
sub print_table {
my $Rows;
my $self = shift;
my $col = 1;
my @tab_opt = qw/width border cellspacing/;
my %tab_opt;
foreach (@tab_opt) {
if ( $self->{$_} ) {
$tab_opt{$_} = $self->{$_}
}
}
my @rowspan;
my $row;
my $colspan;
foreach ( @{ $self->{cells} } ) {
if ( $self->{cols} == 1 ) {
$Rows .= Tr($_->[0])
} else {
while ( $rowspan[$col] ) {
$rowspan[$col]--;
if ( $col < $self->{cols} ) {
$col++;
} else {
$Rows .= Tr($row);
$row = ' ';
$col = 1;
}
}
$row .= $_->[0];
$colspan = $_->[1] ? $col + $_->[1] - 1 : $col;
$colspan = $self->{cols} if $colspan > $self->{cols};
while ( ($col <= $colspan) ) {
$rowspan[$col] = $_->[2] - 1 if $_->[2];
$col++;
}
unless ( $col <= $self->{cols} ) {
$Rows .= Tr($row);
$row = ' ';
$col = 1;
}
}
}
while ( $col <= $self->{cols} ) {
unless ( $rowspan[$col++] ){
$row .= td(' ')
}
}
$Rows .= Tr($row);
return table(\%tab_opt,$Rows);
}
1;
|
|
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
RE: TableMaker
by davorg (Chancellor) on Jul 10, 2000 at 23:44 UTC | |
by Houblon (Novice) on Jul 11, 2000 at 18:37 UTC | |
by davorg (Chancellor) on Jul 11, 2000 at 19:38 UTC | |
by Houblon (Novice) on Jul 13, 2000 at 21:58 UTC |