From: Moritz Bunkus Date: Mon, 11 Jan 2010 10:17:13 +0000 (+0100) Subject: Neue Option 'with_empty' für options_for_select. X-Git-Tag: release-2.6.2beta1~331^2~19 X-Git-Url: http://wagnertech.de/git?a=commitdiff_plain;h=ee61dd2b559ab9d6bf741086c86fa9076efba022;p=kivitendo-erp.git Neue Option 'with_empty' für options_for_select. Sorgt dafür, dass das erste Element ein leeres Element ist. --- diff --git a/SL/Template/Plugin/L.pm b/SL/Template/Plugin/L.pm index 4f5632b9d..692c39663 100644 --- a/SL/Template/Plugin/L.pm +++ b/SL/Template/Plugin/L.pm @@ -107,7 +107,9 @@ sub options_for_select { my $value_key = $options{value} || 'id'; my $title_key = $options{title} || $value_key; - my @tags = (); + my @elements = (); + push @elements, [ undef, $options{empty_title} || '' ] if $options{with_empty}; + if ($collection && (ref $collection eq 'ARRAY')) { foreach my $element (@{ $collection }) { my @result = !ref $element ? ( $element, $element ) @@ -115,14 +117,19 @@ sub options_for_select { : ref $element eq 'HASH' ? ( $element->{$value_key}, $element->{$title_key} ) : ( $element->$value_key, $element->$title_key ); - my %attributes = ( value => $result[0] ); - $attributes{selected} = 'selected' if $options{default} && ($options{default} eq ($result[0] || '')); - - push @tags, $self->html_tag('option', _H($result[1]), %attributes); + push @elements, \@result; } } - return join('', @tags); + my $code = ''; + foreach my $result (@elements) { + my %attributes = ( value => $result->[0] ); + $attributes{selected} = 'selected' if $options{default} && ($options{default} eq ($result->[0] || '')); + + $code .= $self->html_tag('option', _H($result->[1]), %attributes); + } + + return $code; } 1; @@ -240,6 +247,11 @@ respectively. For cases 3 and 4 C<$options{value}> defaults to C and C<$options{title}> defaults to C<$options{value}>. +If the option C is set then an empty element (value +C) will be used as the first element. The title to display for +this element can be set with the option C and defaults to +an empty string. + =back =head1 MODULE AUTHORS