####################My Simple Data Grid ################
package DataGrid;
sub new
{
my $obj={};
$obj->{"Cols"}=2;
$obj->{"Rows"}=2;
$obj->{"SHOW_HEADER"}=1;
$obj->{"AlternatingRowForeColor"}="black";
$obj->{"AlternatingRowBackColor"}="white";
$obj->{"RowForeColor"} ="black";
$obj->{"RowBackColor"}="white";
$obj->{"IsAlternating"}=0;
$obj->{"HEADER_COLOR"}="black";
$obj->{"HEADER_BACKCOLOR"}="white";
bless $obj,'DataGrid';
return $obj;
}
sub AlternatingRowForeColor
{
my $obj=$_[0];
$obj->{"AlternatingRowForeColor"}=$_1;
}
sub RowForeColor
{
my $obj=$_[0];
$obj->{"RowForeColor"}=$_1;
}
sub RowBackColor
{
my $obj=$_[0];
$obj->{"RowBackColor"}=$_1;
}
sub AlternatingRowBackColor
{
my $obj=$_[0];
$obj->{"AlternatingRowBackColor"}=$_1;
}
sub IsAlternating
{
my $obj=$_[0];
$obj->{"IsAlternating"}=$_1;
}
sub HeaderColor
{
my $obj=$_[0];
$obj->{"HEADER_COLOR"}=$_1;
}
sub HeaderBackGround
{
my $obj=$_[0];
$obj->{"HEADER_BACKCOLOR"}=$_1;
}
sub HideHeader
{
my $obj=$_[0];
$obj->{"SHOW_HEADER"}=0;
}
sub ShowHeader
{
my $obj=$_[0];
$obj->{"SHOW_HEADER"}=1;
}
sub DataSource
{
my $obj=$_[0];
my $sth=$_1;
$obj->{"STH"}=$sth;
}
sub Render
{
my $obj=$_[0];
my $html="";
my $rows=$obj->{"Rows"};
my $cols=$obj->{"Cols"};
my $alter=0;
if($obj->{"STH"})
{
my $sth=$obj->{"STH"};
my $arrRef=$sth->fetchrow_hashref;
$html.="
";
if($obj->{"SHOW_HEADER"}==1)
{
$html.="{HEADER_COLOR};background:$obj->{HEADER_BACKCOLOR} ; \">";
foreach my $colName (keys %{$arrRef})
{
$html.="| ";
$html.=$colName;
$html.=" | ";
}
$html.="
";
}
while($arrRef)
{
if($obj->{"IsAlternating"} && $alter==0)
{
$color=$obj->{"RowForeColor"};
$bgcolor=$obj->{"RowBackColor"};
$alter=1;
}
elsif($obj->{"IsAlternating"} && $alter==1)
{
$color=$obj->{"AlternatingRowForeColor"};
$bgcolor=$obj->{"AlternatingRowBackColor"};
$alter=0;
}
elsif(!$obj->{"IsAlternating"})
{
$color=$obj->{"RowForeColor"};
$bgcolor=$obj->{"RowBackColor"};
}
print $color;
print $bgcolor;
$html.="";
foreach my $col (keys %{$arrRef})
{
$html.="| ";
$html.=$arrRef->{$col};
$html.=" | ";
}
$arrRef=$sth->fetchrow_hashref;
$html.="
";
}
$html.="
";
return $html;
}
else
{
$html.="";
for(my $i=0;$i<$rows;$i++)
{
$html.="";
for(my $j=0;$j<$cols;$j++)
{
$html.="| HELLO | ";
}
$html.="
";
}
$html.="
";
return $html;
}
}
1;
__END__
#################### Client Code #######################
#In the client code i create a query statement handle ,
#execute it and pass it off to the DataGrid's DataSource
#Method . In the client code im using an HTML Template
#.The Render Method of th Data Grid basically spits
#all the rendered data as HTML .
#
######################################################
my $db="test";
my $host="localhost";
my $userid="test";
my $passwd="test";
my $connectionInfo="dbi:mysql:$db;$host";
# open the html template
my $template = HTML::Template->new(filename => 'hello_template.tmpl');
my $cgiObj=CGI->new();
my $grid=DataGrid->new();
my $dbh = DBI->connect($connectionInfo,$userid,$passwd);
my $query = "select * from test";
my $sth = $dbh->prepare($query);
$sth->execute();
###########Calling GRID CODE##########################
$grid->DataSource($sth);
$grid->HeaderColor("red");
$grid->HeaderBackGround("whitesmoke");
$grid->AlternatingRowForeColor("blue");
$grid->AlternatingRowBackColor("whitesmoke");
$grid->RowForeColor("white");
$grid->IsAlternating(1);
$grid->RowBackColor("blue");
##############GRID #################################
#######Add Grid to Template##########################
$template->param("Grid_PlaceHolder",$grid->Render());
#####################################################
########################################################