Hello I've just entered the PERL WORLD,a starter.I wrote a quick DataBound Grid using perl that you can use in any of your web applications.Just give the Data Grid the DataBase statement handle ,already executed , and it'll display all the data neatly.You can also set the styles for the Grid like the header forecolor and back color ,row back color and forecolor .To give a really catchy look i've also added an alternating style capability where each alternating row can have different forecolors and backcolor .Even turn on/off the headers .I'll be adding the paging mechanism to the grid later on for easing browsing through thousands of records. here is the code listing .just import the DataGrid module and get started.
####################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.="<table border=1>"; if($obj->{"SHOW_HEADER"}==1) { $html.="<tr style=\" color:$obj->{HEADER_COLOR};background:$obj->{HEAD +ER_BACKCOLOR} ; \">"; foreach my $colName (keys %{$arrRef}) { $html.="<td>"; $html.=$colName; $html.="</td>"; } $html.="</tr>"; } 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.="<tr style=\" color:$color; background:$bgcolor; \">"; foreach my $col (keys %{$arrRef}) { $html.="<td>"; $html.=$arrRef->{$col}; $html.="</td>"; } $arrRef=$sth->fetchrow_hashref; $html.="</tr>"; } $html.="</table>"; return $html; } else { $html.="<table border=1>"; for(my $i=0;$i<$rows;$i++) { $html.="<tr>"; for(my $j=0;$j<$cols;$j++) { $html.="<td>HELLO</td>"; } $html.="</tr>"; } $html.="</table>"; 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()); ##################################################### ########################################################
Let me have your feed back thanks

Edited by Chady -- removed html formatting, added code tags

Replies are listed 'Best First'.
Re: A Simple DataBound Grid
by zby (Vicar) on May 10, 2005 at 09:34 UTC
    Have you previewed that page?
Re: A Simple DataBound Grid
by MidLifeXis (Monsignor) on May 10, 2005 at 19:07 UTC

    Update: Added readmore tags

    Update-2: Removed non-HTML code snippet - janitors responded.

    --MidLifeXis