CGI-Form-Table

 view release on metacpan or  search on metacpan

lib/CGI/Form/Table.pm  view on Meta::CPAN

Each has a unique name, and on form submission the inputs are effectively
serialized.

L<CGI::Form::Table::Reader> will use the CGI module to produce a data structure
based on the parameters submitted by a form of this type.

=head1 METHODS

=head2 C<< CGI::Form::Table->new(%arg) >>

This method constructs a new form.  The only required arguments  are
C<columns>, which names the columns that will be in the form table, and
C<prefix>, which gives the unique prefix for input fields.

If given, C<initial_rows> specifies how many rows should initially be in the
form.

Instead of C<initial_rows>, you can pass C<initial_values>, a reference to an
array of hashes providing values for the columns of each row.  For example:

 my $table = CGI::Form::Table->new(
  prefix  => "charsheet",
  columns => [ qw(ability score) ],
  initial_values => [
   { ability => 'Str', score => '18/00' },
   { ability => 'Cha', score => '11'    }
  ]
 );

C<column_header>, if passed, is a hash of text strings to use as column
headers.  The keys are column names.  Columns without C<column_header> entries
are headed by their names.

Another argument, C<column_content>, may be passed.  It must contain a hashref,
with entries providing subs to produce initial content.  The subs are passed the
form object, the row number, and the name of the column.  For example, to add a
reminder of the current row in the middle of each row, you might create a form
like this:

 my $form = CGI::Form::Table->new(
   prefix  => 'simpleform',
   columns => [qw(one two reminder three four)],
   column_content => {
     reminder => sub { $_[1] }
   }
 );

This can be useful for forms that require SELECT elements or other complicated
parts.  (The JavaScript will just copy the column value when new rows are added,
updating the name attribute.)

=cut

sub new {
 my ($class, %arg) = @_;
 return unless $arg{columns};
 return unless $arg{prefix};
 $arg{initial_rows} = 1 unless $arg{initial_rows};
 $arg{initial_rows} = @{$arg{initial_values}}
  if ($arg{initial_values} && @{$arg{initial_values}} > $arg{initial_rows});
 bless \%arg => $class;
}

=head2 C<< $form->as_html >>

This returns HTML representing the form object.  JavaScript is required to make
the form expandible/shrinkable; see the C<javascript> method.  (L</"SEE ALSO">)

=cut

sub as_html {
 my ($self) = @_;
 my $prefix = $self->{prefix};

 my $column_headers = join q{},
  map { "\t\t\t<th class='input_column'>" . $self->column_header($_) . "</th>\n" }
  @{$self->{columns}};

 my $html = <<"EOH";
<table class='cft $prefix'>
 <thead>
  <tr>
   <td class='row_number'></td>
   <td class='add button'></td>
   <td class='delete button'></td>
$column_headers
   <td class='row_number'></td>
  </tr>
 </thead>

 <tbody>
EOH

 for my $row_number (1 .. $self->{initial_rows}) {
  my $content = join q{},
   map { "<td class='input_column'>" . $self->cell_content($row_number, $_) . "</td>" }
   @{$self->{columns}};

  $html .= <<"EOH";
  <tr>
   <td class='row_number'>$row_number</td>
   <td class='add button'>
     <input type='button' onClick='cloneParentOf(this.parentNode, "$prefix")' value='+' />
    </td>
    <td class='delete button'>
    <input type='button' onClick='removeParentOf(this.parentNode, "$prefix")' value='-' />
   </td>
   $content
   <td class='row_number'>$row_number</td>
  </tr>
EOH
 }
 $html .= "\t</tbody>\n";
 $html .= "</table>\n";

 return $html;
}

=head2 C<< $form->column_header($column_name) >>

This method returns the text that should be used as the column header in the

 view all matches for this distribution
 view release on metacpan -  search on metacpan

( run in 1.419 second using v1.00-cache-2.02-grep-82fe00e-cpan-1925d2aa809 )