forked from micc83/editTable
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.edittable.js
385 lines (297 loc) · 13.1 KB
/
jquery.edittable.js
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
/*! editTable v0.2.0 by Alessandro Benoit */
/*! https://codeb.it/edittable/ - https://github.com/micc83/editTable */
/*! VERSIONE MODIFICATA da shaodw7853: https://github.com/shadow7853/editTable */
(function ($, window, i) {
'use strict';
$.fn.editTable = function (options) {
// Settings
var s = $.extend({
data: [['']],
tableClass: 'inputtable',
jsonData: false,
headerCols: false,
maxRows: 999,
first_row: true,
row_template: false,
field_templates: false,
fixed_cols: false,
validate_field: function (col_id, value, col_type, $element) {
return true;
},
data_changed: function (col_id, action, value, $element) {
}
}, options),
$el = $(this),
defaultTableContent = '<thead><tr></tr></thead><tbody></tbody>',
$table = $('<table/>', {
class: s.tableClass + ((s.first_row) ? ' wh' : '') + ((s.fixed_rows) ? ' fr' : ''),
html: defaultTableContent
}),
defaultthbuttons = '<a class="addcol icon-button" href="#">+</a> <a class="delcol icon-button" href="#">-</a>',
defaultth = '<th>' + defaultthbuttons + '</th>',
colnumber = 0,
rownumber = 0,
reset,
is_validated = true;
if (s.data_changed) {
$table.on('change.data', 'input, textarea, select', function () {
var colid = parseInt($(this).closest('tr').children().index($(this).parent('td')), 10);
s.data_changed(colid, 'data', $(this).val(), $(this));
});
$table.on('keydown.data', 'input, textarea, select', function (evt) {
var colid = parseInt($(this).closest('tr').children().index($(this).parent('td')), 10);
if (evt.keyCode === 13) {
s.data_changed(colid, 'data', $(this).val(), $(this));
}
});
}
// Increment for IDs
i = i + 1;
// Build cell
function buildCell(content, type) {
content = (content === 0) ? "0" : (content || '');
// Custom type
if (type && 'text' !== type) {
var field = s.field_templates[type];
return '<td>' + field.setValue(field.html, content)[0].outerHTML + '</td>';
}
// Default
return '<td><input type="text" value="' + content.toString().replace(/"/g, """) + '" /></td>';
}
// Build row
function buildRow(data) {
var rowcontent = '', b;
data = data || '';
if (!s.row_template) {
// Without row template
for (b = 0; b < colnumber; b += 1) {
rowcontent += buildCell(data[b]);
}
} else {
// With row template
for (b = 0; b < colnumber; b += 1) {
if (b < s.row_template.length) {
// For each field in the row
rowcontent += buildCell(data[b], s.row_template[b]);
} else {
rowcontent += buildCell(data[b]);
}
}
}
if (!s.fixed_rows) {
rowcontent = rowcontent + '<td><a class="addrow icon-button" href="#">+</a> <a class="delrow icon-button" href="#">-</a></td>';
}
return $('<tr/>', {
html: rowcontent
});
}
// Check button status (enable/disabled)
function checkButtons() {
if (colnumber < 2) {
$table.find('.delcol').addClass('disabled');
}
if (rownumber < 2) {
$table.find('.delrow').addClass('disabled');
}
if (s.maxRows && rownumber === s.maxRows) {
$table.find('.addrow').addClass('disabled');
}
}
// Fill table with data
function fillTableData(data) {
var a, crow = Math.min(s.maxRows, data.length);
// Clear table
$table.html(defaultTableContent);
colnumber = 0;
if (data) {
for (var i = 0; i < data.length; i++) {
var l = data[i].length;
if (colnumber < l) {
colnumber = l;
}
}
}
// If headers or row_template are set
if (s.headerCols || s.row_template) {
// Fixed first columns
var fixed = Math.max((s.headerCols || []).length, (s.row_template || []).length);
colnumber = Math.max(colnumber, fixed)
// Table headers
for (a = 0; a < colnumber; a += 1) {
var col_title = s.headerCols[a] || '';
if (s.fixed_cols) {
$table.find('thead tr').append('<th>' + col_title + '</th>');
} else {
$table.find('thead tr').append('<th>' + '<span style="display: block; margin-bottom: 3px">' + col_title + '</span>' + defaultthbuttons + '</th>');
}
}
// Table content
for (a = 0; a < crow; a += 1) {
// For each row in data
buildRow(data[a]).appendTo($table.find('tbody'));
}
} else {
// Variable columns
if (!s.fixed_cols) {
for (a = 0; a < colnumber; a += 1) {
$table.find('thead tr').append(defaultth);
}
}
for (a = 0; a < crow; a += 1) {
buildRow(data[a]).appendTo($table.find('tbody'));
}
}
// Append missing th
if (!s.fixed_rows) {
$table.find('thead tr').append('<th></th>');
}
// Count rows and columns
//colnumber = $table.find('thead th').length - 1;
rownumber = $table.find('tbody tr').length;
checkButtons();
}
// Export data
function exportData() {
var row = 0, data = [], value;
is_validated = true;
$table.find('tbody tr').each(function () {
row += 1;
data[row] = [];
$(this).find('td:not(:last-child)').each(function (i, v) {
if (s.row_template && 'text' !== s.row_template[i]) {
var field = s.field_templates[s.row_template[i]],
el = $(this).find($(field.html).prop('tagName'));
value = field.getValue(el);
if (!s.validate_field(i, value, s.row_template[i], el)) {
is_validated = false;
}
data[row].push(value);
} else {
value = $(this).find('input[type="text"]').val();
if (!s.validate_field(i, value, 'text', v)) {
is_validated = false;
}
data[row].push(value);
}
});
});
// Remove undefined
data.splice(0, 1);
return data;
}
// Fill the table with data from textarea or given properties
if ($el.is('textarea')) {
try {
reset = JSON.parse($el.val());
} catch (e) {
reset = s.data;
}
$el.after($table);
// If inside a form set the textarea content on submit
if ($table.parents('form').length > 0) {
$table.parents('form').submit(function () {
$el.val(JSON.stringify(exportData()));
});
}
} else {
reset = (JSON.parse(s.jsonData) || s.data);
$el.append($table);
}
fillTableData(reset);
// Add column
$table.on('click', '.addcol', function () {
var colid = parseInt($(this).closest('tr').children().index($(this).parent('th')), 10);
colnumber += 1;
$table.find('thead tr').find('th:eq(' + colid + ')').after(defaultth);
$table.find('tbody tr').each(function () {
$(this).find('td:eq(' + colid + ')').after(buildCell());
});
$table.find('.delcol').removeClass('disabled');
if (s.row_template && colid < s.row_template.length) {
s.row_template.splice(colid + 1, 0, 'text');
}
if (s.data_changed) {
s.data_changed(colid, 'addcol');
}
return false;
});
// Remove column
$table.on('click', '.delcol', function () {
if ($(this).hasClass('disabled')) {
return false;
}
var colid = parseInt($(this).closest('tr').children().index($(this).parent('th')), 10);
colnumber -= 1;
checkButtons();
$(this).parent('th').remove();
$table.find('tbody tr').each(function () {
$(this).find('td:eq(' + colid + ')').remove();
});
if (s.row_template && colid < s.row_template.length) {
s.row_template.splice(colid, 1);
}
if (s.data_changed) {
s.data_changed(colid, 'delcol');
}
return false;
});
// Add row
$table.on('click', '.addrow', function () {
if ($(this).hasClass('disabled')) {
return false;
}
rownumber += 1;
$(this).closest('tr').after(buildRow(0, colnumber));
$table.find('.delrow').removeClass('disabled');
checkButtons();
if (s.data_changed) {
s.data_changed(rownumber, 'addrow');
}
return false;
});
// Delete row
$table.on('click', '.delrow', function () {
if ($(this).hasClass('disabled')) {
return false;
}
rownumber -= 1;
checkButtons();
$(this).closest('tr').remove();
$table.find('.addrow').removeClass('disabled');
if (s.data_changed) {
s.data_changed(rownumber, 'delrow');
}
return false;
});
// Select all content on click
$table.on('click', 'input', function () {
$(this).select();
});
// Return functions
return {
// Get an array of data
getData: function () {
return exportData();
},
// Get the JSON rappresentation of data
getJsonData: function () {
return JSON.stringify(exportData());
},
// Load an array of data
loadData: function (data) {
fillTableData(data);
},
// Load a JSON rappresentation of data
loadJsonData: function (data) {
fillTableData(JSON.parse(data));
},
// Reset data to the first instance
reset: function () {
fillTableData(reset);
},
isValidated: function () {
return is_validated;
}
};
};
})(jQuery, this, 0);