Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re^2: HTML::Template with HTML::Pager

by flyby (Novice)
on Oct 09, 2013 at 18:59 UTC ( #1057605=note: print w/ replies, xml ) Need Help??


in reply to Re: HTML::Template with HTML::Pager
in thread HTML::Template with HTML::Pager

That's what the documentation says. I'm looking for what it doesn't seem to say.

PAGER_DATA_LIST gives you the table data with no columns names. You can add the headers in by using the column_names feature. That works just fine when using HTML::Pagers built in template but I can't get it to work when using it with HTML::Template. That's the part I'm missing.

Seems like there should be a TMPL_LOOP for the headers. Something like:

<TR> <TMPL_LOOP NAME="PAGER_DATA_HEADERS"> <TH BGCOLOR=#ffffff><TMPL_VAR NAME="HEADER"></TH> </TMPL_LOOP> </TR> <TMPL_LOOP NAME="PAGER_DATA_LIST"> <TR> <TD BGCOLOR=#ffffff><TMPL_VAR NAME="PAGER_DATA_COL_0"></TD> <TD BGCOLOR=#ffffff><TMPL_VAR NAME="PAGER_DATA_COL_1"></TD> </TR> </TMPL_LOOP>

That or the headers are passed to HTML::Template under a different array ref and HTML::Template is used to print out the headers.

Thanks for taking the time to look

-Matt


Comment on Re^2: HTML::Template with HTML::Pager
Download Code
Re^3: HTML::Template with HTML::Pager
by keszler (Priest) on Oct 09, 2013 at 19:39 UTC
    Is your $data hash or array? The source for HTML::Pager does:
    # fill in the template if ($args_type eq 'ARRAY') { # handle array case my @pager_list; if (defined($self->{column_names})) { my %row; my $x = 0; foreach my $col_name (@{$self->{column_names}}) { $row{"PAGER_DATA_COL_$x"} = "<B>$col_name</B>"; $x++; } push(@pager_list, \%row); } foreach my $rowRef (@{$self->{data}}) { my %row; my $x = 0; foreach my $value (@{$rowRef}) { $value = '' unless (defined($value)); $row{"PAGER_DATA_COL_$x"} = $value; $x++; } if ($x) { push(@pager_list, \%row); } } $template->param('PAGER_DATA_LIST', \@pager_list); } else { # handle the hash case $template->param(PAGER_DATA_LIST => $self->{data}); }
    which at first glance appears to ignore $self->{column_names} in the hash case.

      It's an array of hash refs. That's how you get the named fields out.

      my $hashref = { 'rows' => "$i $x", 'offset' => "offset $offset", 'i' => "$i", 'x' => $x, 'INTERFACE' => \@r, }; push(@total, $hashref);

      That's looped over a bunch of test data and then fed to get_data_callback as $data. It's just test junk but works as expected.

      <TD BGCOLOR=#ffffff><TMPL_VAR NAME="rows"></TD>

      produces the correct data stored in the hashref listed above.

      The documentation doesn't mention the ability to pass as hash to get_data_callback so I'm not sure how/if that would work.

      I've also been poking over the Pager.pm code between work issues but nothing has jumped out at me yet. It seems like either it's not possible or it's just not in the code because it's a default or obvious how it's suppose to work. Just not obvious to me. :)

      thanks for looking,

      -Matt

      -- And this works --

      Gen some random headers and add them to an array of hashrefs:

      my @headers = ("1","2", "three", "4", "blank", "blank", "blank", "desc +-1", "desc-2", "desc-3", "desc-4", "desc-5"); for (@headers) { my $href = { 'HEADER' => $_ }; push(@h, $href); };

      Make the primary array of hashrefs:

      my $hashref = { 'rows' => "$i $x", 'offset' => "offset", 'name' => "some data $i", 'x' => $x, 'INTERFACE' => \@r, }; push(@total, $hashref);

      Create the HTML::Template setup:

      my $template = HTML::Template->new( filename => 'testing.tmpl', die_on_bad_params => 0, associate => $query, );

      Add the header array of hashrefs to the template. Might be able to do this at the same time as you create the $template.. but for some reason it didn't work the first time I tried it.

      $template->param(HEADER => \@h );

      That works with this Template:

      <TMPL_VAR NAME="PAGER_JAVASCRIPT"> <FORM> <TABLE BORDER=0 BGCOLOR=#000000 WIDTH=100%> <TR><TD><TABLE BORDER=0 WIDTH=100%> <!-- TMPL_LOOP NAME="HEADER" --> <TH BGCOLOR=#ffffff><!-- TMPL_VAR NAME="HEADER" --></TH> <!-- /TMPL_LOOP --> </TR> <TMPL_LOOP NAME="PAGER_DATA_LIST"> <TR> <TD BGCOLOR=#ffffff><TMPL_VAR NAME="rows"></TD> <TD BGCOLOR=#ffffff><TMPL_VAR NAME="offset"></TD> <TD BGCOLOR=#ffffff><TMPL_VAR NAME="name"></TD> <TD BGCOLOR=#ffffff><TMPL_VAR NAME="x"></TD> <!-- TMPL_LOOP NAME=INTERFACE --> <TD BGCOLOR=#ffffff><!-- TMPL_VAR NAME="description" --></TD> <!-- /TMPL_LOOP --> </TR> </TMPL_LOOP> <TR><TD><TD BGCOLOR=#DDDDDD COLSPAN=15 ALIGN=CENTER> <TMPL_VAR NAME="PAGER_PREV"> <TMPL_VAR NAME="PAGER_JUMP"> <TMPL_VAR NAME="PAGER_NEXT"> </TD></TR> </TABLE> </TABLE> <TMPL_VAR NAME="PAGER_HIDDEN"> </FORM>

      Gives me the correct <TH></TH> headers I want. Cool beans.

      -Matt

        Reading the code more closely the segment that you posted seems to be the key. HTML::Pager doesn't bother creating "real" table headers with <TH></TH>. It just makes the first row of data bold, <B>, and calls that that table headers. I guess I could do the same thing with my data but it seems kind of lame to generate tables without "real" headers.

        Maybe there is a way inject another TMPL_LOOP before the PAGER_DATA_LIST loop and use that as the headers.

        Thanks for the help. Gives me some direction to work in.

        -Matt

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1057605]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (11)
As of 2014-08-29 09:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (277 votes), past polls