1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
|
<%doc>
Example:
include( '/elements/checkboxes.html',
# required
#? 'name_callback' => sub { },
'names_list' => [ 'value',
'other value',
[ 'complex value' => { 'label' => 'Display value',
'desc' => "Add'l description",
'note' => ' *',
}
],
],
'element_name_prefix' => "$link_table.",
#recommended
'checked_callback' => sub { my( $cgi, $name ) = @_; },
)
</%doc>
<TABLE CELLSPACING=0 CELLPADDING=0>
<TR>
<TD COLSPAN=2 ALIGN="center"><FONT SIZE="-1">(
<A HREF="javascript:setAll<%$prefix%>(true)">select all</A> |
<A HREF="javascript:setAll<%$prefix%>(false)">unselect all</A> |
<A HREF="javascript:toggleAll<%$prefix%>()">toggle all</A>
)</FONT></TD>
</TR>
% my $num=0;
% foreach my $item ( @{ $opt{'names_list'} } ) {
%
% my $name = ref($item) ? $item->[0] : $item;
% my $display = ( ref($item) && $item->[1]{label} )
% ? $item->[1]{label}
% : $name;
% $display =~ s/ / /g;
% $display .= $item->[1]{note} if ref($item) && $item->[1]{note};
% my $desc = ref($item) && $item->[1]{desc} ? $item->[1]{desc} : '';
%
% my $callback =
% ( $cgi->param('error') ? 'error_' : '' ). 'checked_callback';
% my $checked = &{ $opt{$callback} }( $cgi, $name ) ? 'CHECKED' : '';
<TR>
<TD VALIGN="top">
<INPUT TYPE="checkbox" NAME="<% $opt{'element_name_prefix'}. $name %>" <% $checked %> ID="<%$prefix.$num++%>" VALUE="ON">
</TD>
<TD><% $display %>
% if ( $desc ) {
<BR><FONT SIZE="-2"><% $desc %></FONT>
% }
</TD>
</TR>
% }
</TABLE>
<SCRIPT TYPE="text/javascript">
function setAll<%$prefix%>(setTo) {
% for ( 0 .. ($num-1) ) {
document.getElementById('<%$prefix.$_%>').checked = setTo;
% }
}
function toggleAll<%$prefix%>(setTo) {
% for ( 0 .. ($num-1) ) {
var element = document.getElementById('<%$prefix.$_%>');
if ( element.checked == true ) {
element.checked = false;
} else {
element.checked = true;
}
% }
}
</SCRIPT>
<%init>
my( %opt ) = @_;
my @pset = ( 'a'..'z', 'A'..'Z', '0'..'9' );
my $prefix = $opt{prefix}
|| join('', map $pset[ int(rand $#pset) ], (0..20) );
$opt{checked_callback} ||= sub {};
$opt{'error_checked_callback'} ||= sub {
my( $cgi, $name ) = @_;
$cgi->param($opt{'element_name_prefix'}. $name );
};
</%init>
|