5 include( '/elements/select-table.html',
8 'table' => 'table_name',
9 'name_col' => 'name_column',
11 #strongly recommended (you want your forms to be "sticky" on errors, right?)
12 'curr_value' => 'current_value',
13 #'value' => #deprecated form of 'curr_value',
16 'empty_label' => '', #better specify it though, the default might change
19 'records' => \@records, #instead of hashref
20 'pre_options' => [ 'value' => 'option' ], #before normal options
21 'element_name' => '', #HTML element name, defaults to the name of
22 # the primary key column
23 'field' => '', #synonym for element_name
24 'element_etc' => '', #additional attributes (i.e. "DISABLED") for the
26 'onchange' => '', #javascript code
27 'multiple' => 0, # bool
28 'disable_empty' => 0, # bool (implied by multiple)
29 'debug' => 0, #set true to enable
30 'label_callback' => sub { my $record = shift; return "label"; },
35 <SELECT <% $opt{'multiple'} ? 'MULTIPLE' : '' %>
36 NAME = "<% $opt{'element_name'} || $opt{'field'} || $key %>"
38 <% $opt{'element_etc'} %>
41 % while ( @pre_options ) {
42 <OPTION VALUE="<% shift(@pre_options) %>"><% shift(@pre_options) %>
46 % unless ( $opt{'multiple'} || $opt{'disable_empty'} ) {
47 <OPTION VALUE=""><% $opt{'empty_label'} || 'all' %>
50 % foreach my $record ( sort { $a->$name_col() cmp $b->$name_col() } @records ) {
51 % my $recvalue = $record->$key();
52 <OPTION VALUE="<% $recvalue %>"
53 <% ref($value) && $value->{$recvalue} || $value == $recvalue
56 ><% $opt{'label_callback'}
57 ? &{ $opt{'label_callback'} }( $record )
58 : $record->$name_col()
68 warn "elements/select-table.html: \n". Dumper(%opt)
69 if exists $opt{debug} && $opt{debug};
71 my $onchange = $opt{'onchange'}
72 ? 'onChange="'. $opt{'onchange'}. '(this)"'
75 my $key = dbdef->table($opt{'table'})->primary_key; #? $opt{'primary_key'} ||
77 my $name_col = $opt{'name_col'};
79 my $value = $opt{'curr_value'} || $opt{'value'};
80 $value = [ split(/\s*,\s*/, $value) ] if $opt{'multiple'} && $value =~ /,/;
83 if ( $opt{'records'} ) {
84 @records = @{ $opt{'records'} };
87 'table' => $opt{'table'},
88 'hashref' => ( $opt{'hashref'} || {} ),
89 'extra_sql' => ( $opt{'extra_sql'} || '' ),
95 or ! exists( $opt{hashref}->{disabled} ) #??
96 or grep { $value == $_->$key() } @records
98 delete $opt{hashref}->{disabled};
99 $opt{hashref}->{$key} = $value;
100 my $record = qsearchs( {
101 'table' => $opt{table},
102 'hashref' => $opt{hashref},
103 'extra_sql' => ( $opt{extra_sql} || '' ),
105 push @records, $record if $record;
108 if ( ref( $value ) eq 'ARRAY' ) {
109 $value = { map { $_ => 1 } @$value };
112 my @pre_options = $opt{pre_options} ? @{ $opt{pre_options} } : ();