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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
|
<%doc>
(within a form)
<table>
<tr>
<th>Field 1</th>
<th>Field 2</th>
</tr>
<tr id="mytemplate">
<td><input type="text" name="field1"></td>
<td><select name="field2">...</td>
...
</tr>
</table>
<& /elements/auto-table.html,
table => 'mytable',
template_row = 'mytemplate',
rows => [
{ field1 => 'foo', field2 => 'CA', ... },
{ field1 => 'bar', field2 => 'TX', ... }, ...
],
&>
or if you prefer:
...
fieldorder => [ 'field1', 'field2', ... ],
rows => [
[ 'foo', 'CA' ],
[ 'bar', 'TX' ],
],
In the process/ handler, something like:
my @rows;
my %vars = $cgi->Vars;
for my $k ( keys %vars ) {
$k =~ /^${pre}magic(\d+)$/ or next;
my $rownum = $1;
# find all submitted names ending in this rownum
my %thisrow =
map { $_ => $vars{$_} }
grep /^(.*[\d])$rownum$/, keys %vars;
$thisrow->{num} = delete $thisrow{"${pre}magic$rownum"};
push @rows, $thisrow;
}
</%doc>
<tbody id="<%$pre%>autotable"></tbody>
<script type="text/javascript">
var <%$pre%>template;
var <%$pre%>tbody;
var <%$pre%>next_rownum;
var <%$pre%>set_rownum;
var <%$pre%>addRow;
var <%$pre%>deleteRow;
var <%$pre%>fieldorder = <% encode_json($fieldorder) %>;
function <%$pre%>possiblyAddRow_factory(obj) {
var callback = obj.onchange;
return function() {
if ( obj.rownum == <%$pre%>tbody.lastChild.rownum ) {
// then this is the last row, and it's being changed, so spawn a new row
<%$pre%>addRow();
}
if ( callback ) {
callback.apply(obj);
}
}
}
function <%$pre%>set_rownum(obj, rownum) {
obj.rownum = rownum;
if ( obj.id ) {
obj.id = obj.id + rownum;
}
if ( obj.getAttribute('name') ) {
obj.setAttribute('name', obj.getAttribute('name') + rownum);
// also, in this case it's a form field that will be part of the record
// so set up an onchange handler
obj.onchange = <%$pre%>possiblyAddRow_factory(obj);
}
for (var i = 0; i < obj.children.length; i++) {
if ( obj.children[i] instanceof Node ) {
<%$pre%>set_rownum(obj.children[i], rownum);
}
}
}
function <%$pre%>addRow(data) {
// duplicate the node
// warning: cloneNode doesn't clone event handlers that were set through
// the DOM
// if 'data' is an object, prepopulate the row's fields with the object's
// elements
// returns the rownum of the new row
var row = <%$pre%>template.cloneNode(true);
<%$pre%>tbody.appendChild(row);
var this_rownum = <%$pre%>next_rownum;
<%$pre%>set_rownum(row, this_rownum);
if(data instanceof Array) {
for (i = 0; i < data.length && i < <%$pre%>fieldorder.length; i++) {
var el = document.getElementsByName(<%$pre |js_string%> +
<%$pre%>fieldorder[i] +
this_rownum)[0];
if (el) {
if ( el.tagName.toLowerCase() == 'span' ) {
el.innerHTML = data[i];
} else if ( el.type == 'checkbox' ) {
el.checked = (el.value == data[i]);
} else {
el.value = data[i];
}
}
}
} else if (data instanceof Object) {
for (var field in data) {
var el = document.getElementsByName(<%$pre |js_string%> +
field +
this_rownum)[0];
if (el) {
if ( el.tagName.toLowerCase() == 'span' ) {
el.innerHTML = data[field];
} else if ( el.type == 'checkbox' ) {
el.checked = (el.value == data[field]);
} else {
el.value = data[field];
}
}
}
} // else nothing
<%$pre%>next_rownum++;
return this_rownum;
}
function <%$pre%>deleteRow(rownum) {
if ( rownum == <%$pre%>tbody.lastChild.rownum ) {
// if this is the last row, spawn another one after it
<%$pre%>addRow();
}
var r = document.getElementById('<%$pre%>row' + rownum);
<%$pre%>tbody.removeChild(r);
}
function <%$pre%>set_prefix(obj) {
if ( obj.id ) {
obj.id = <%$pre |js_string%> + obj.id;
}
if ( obj.getAttribute('name') ) {
obj.setAttribute('name', <%$pre |js_string%> + obj.getAttribute('name'));
}
for (var i = 0; i < obj.children.length; i++) {
if ( obj.children[i] instanceof Node ) {
<%$pre%>set_prefix(obj.children[i]);
}
}
}
function <%$pre%>init() {
<%$pre%>template = document.getElementById(<% $template_row |js_string%>);
<%$pre%>tbody = document.getElementById('<%$pre%>autotable');
<%$pre%>next_rownum = <%$pre%>template.sectionRowIndex;
// detach the template row
var table = <%$pre%>template.parentNode;
table.removeChild(<%$pre%>template);
// give it an id
<%$pre%>template.id = 'row';
// prefix the ids and names of the TR object and all its descendants
<%$pre%>set_prefix(<%$pre%>template);
// add a magic identifier so we know it's been submitted
var magic = document.createElement('INPUT');
magic.setAttribute('type', 'hidden');
magic.setAttribute('name', '<%$pre%>magic');
magic.value = '1';
// and a delete button
%# should this be enclosed in an actual <button> for aesthetics?
var delete_button = document.createElement('IMG');
delete_button.id = '<%$pre%>delete_button';
delete_button.src = '<%$fsurl%>images/cross.png';
delete_button.alt = 'X';
// use an inline string for this so that it will be cloned properly
delete_button.setAttribute('onclick', "<%$pre%>deleteRow(this.rownum);");
// and an error display
var error_span = document.createElement('SPAN');
error_span.className = 'error';
error_span.style.color = '#FF0000';
error_span.setAttribute('name', '<%$pre%>error');
error_span.style.padding = '5px';
var delete_cell = document.createElement('TD');
delete_cell.style.textAlign = 'left';
delete_cell.appendChild(delete_button);
delete_cell.appendChild(magic); // it has to go somewhere
delete_cell.appendChild(error_span);
<%$pre%>template.appendChild(delete_cell);
// preload rows
var rows = <% encode_json(\@rows) %>;
for (var i = 0; i < rows.length; i++) {
<%$pre%>addRow(rows[i]);
}
<%$pre%>addRow();
}
<%$pre%>init();
</script>
<%init>
my %opt = @_;
my $pre = '';
$pre = $opt{'table'} . '_' if $opt{'table'};
my $template_row = $opt{'template_row'}
or die "auto-table requires template_row\n"; # a DOM id
# rows that we will preload, as hashrefs of name => value
my @rows = @{ $opt{'data'} || [] };
foreach (@rows) {
# allow an array of FS::Record objects to be passed
if ( blessed($_) and $_->isa('FS::Record') ) {
$_ = $_->hashref;
}
}
my $fieldorder = $opt{'fieldorder'} || [];
</%init>
|