1 <SELECT NAME="<% $opt{'element_name'} || $key %>" <% $opt{'element_etc'} %>>
3 % while ( @pre_options ) {
5 <OPTION VALUE="<% shift(@pre_options) %>"><% shift(@pre_options) %>
9 <OPTION VALUE=""><% $opt{'empty_label'} || 'all' %>
11 % foreach my $record ( sort { $a->$name_col() cmp $b->$name_col() } @records ) {
13 % ref($opt{'value'}) ? (exists($opt{'value'}->{$record->$key()}) and $matches=1)
14 % : ($opt{'value'} == $record->$key() and $matches=1);
16 <OPTION VALUE="<% $record->$key() %>"<% $matches ? ' SELECTED' : '' %>><% $record->$name_col() %>
24 # 'table' => 'table_name',
25 # 'name_col' => 'name_column',
27 ##strongly recommended (you want your forms to be "sticky" on errors, right?)
28 # 'value' => 'current_value',
31 # 'empty_label' => '', #better specify it though, the default might change
34 # 'records' => \@records, #instead of hashref
35 # 'pre_options' => [ 'value' => 'option' ], #before normal options
36 # 'element_name' => '', #HTML element name, defaults to the name of
37 # # the primary key column
38 # 'element_etc' => '', #additional attributes (i.e. "DISABLED") for the
40 # 'debug' => 0, #set true to enable
44 warn "elements/select-table.html: \n". Dumper(%opt)
45 if exists $opt{debug} && $opt{debug};
47 my $key = dbdef->table($opt{table})->primary_key; #? $opt{primary_key} ||
49 my $name_col = $opt{name_col};
54 if ( $opt{records} ) {
55 @records = @{ $opt{records} };
58 'table' => $opt{table},
59 'hashref' => $opt{hashref},
60 'extra_sql' => ( $opt{extra_sql} || '' ),
64 unless ( ! $opt{value}
66 or ! exists( $opt{hashref}->{disabled} ) #??
67 or grep { $opt{value} == $_->$key() } @records
69 delete $opt{hashref}->{disabled};
70 $opt{hashref}->{$key} = $opt{value};
71 my $record = qsearchs( {
72 'table' => $opt{table},
73 'hashref' => $opt{hashref},
74 'extra_sql' => ( $opt{extra_sql} || '' ),
76 push @records, $record if $record;
79 if ( ref( $opt{value} ) eq 'ARRAY' ) {
80 $opt{value} = { map { $_ => 1 } @{$opt{value}} };
83 my @pre_options = $opt{pre_options} ? @{ $opt{pre_options} } : ();