diff --git a/demo/index.php b/demo/index.php index b42ebeb..215c79b 100644 --- a/demo/index.php +++ b/demo/index.php @@ -104,7 +104,9 @@ $(this).datepicker(); }); - // Example 4 + // Example 4 EDITED by shadow7853 + // shadow7853: I've modified the code to enable headerCols, row_templates to be also partials (define templates for first cells, others will be 'text') and to not disable column adding. + // shadow7853: Also data rows could be partials, not all with the same length. // Custom fields & validation var mynewtable = $('#examplex').editTable({ field_templates: { @@ -148,7 +150,8 @@ first_row: false, data: [ [false,"01/30/2013","50,00 €","Lorem ipsum...\n\nDonec in dui nisl. Nam ac libero eget magna iaculis faucibus eu non arcu. Proin sed diam ut nisl scelerisque fermentum."], - [true,"02/28/2013","50,00 €",'This is a + +
Show Code Validate table @@ -511,7 +527,12 @@ } } return true; + }, + + data_changed: function (col_id, action, value, $element) { + $('#examplexlog').append('<div>col_id: ' + col_id + ', action: ' + action + ', value: ' + $('<div/>').text(value).html() + ', $element: ' + $element + '</div>'); }, + tableClass: 'inputtable custom' }); diff --git a/demo/shadow7853.html b/demo/shadow7853.html new file mode 100644 index 0000000..f6eb3ab --- /dev/null +++ b/demo/shadow7853.html @@ -0,0 +1,296 @@ + + + + + + + + jQuery editTable + + + + + + + + + + +
+ +

jQuery editTable v0.2.0

+

Edited by shadow7853

+ +

Based on original jQuery editTable, thank's to micc83: https://github.com/micc83/editTable

+

jQuery editTable is a very small jQuery Plugin (~1Kb gzipped) that fill the gap left by the missing of a default input field for data tables. jQuery editTable can be used both in ajax and/or HTTP POST contest and let you preset the title and number of columns or just let complete freedom to the user. You can even append custom behaviors to single column cells (ex. jQuery UI Datepicker). The only limit is your imagination! :)

+ +

I've modified the code to enable headerCols, row_templates to be also partials (define templates for first cells, others will be 'text') and to not disable column adding.

+

Also data rows could be partials, not all with the same length.

+ + Go to MY version on GitHub + Go to original version on GitHub + +

To use it you just have to include jQuery and a copy of the plugin in your head or footer:

+
+<script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>
+<script type="text/javascript" src="jquery.edittable.min.js"></script>
+<link rel="stylesheet" href="jquery.edittable.min.css">
+
+ +

Now you can trigger editTable on any textarea or block element (ex. div, article, section ...). In case you trigger it on a textarea, its content will be used as JSON source for the table. If the textarea is inside a form, on submit, its content will be updated with the new JSON data. Otherwise, if you trigger it on a block element the table will be appended to the element itself (ajax).

+ +
+var mytable = $('#edittable').editTable({
+    data: [['']],           // Fill the table with a js array (this is overridden by the textarea content if not empty)
+    tableClass: 'inputtable',   // Table class, for styling
+    jsonData: false,        // Fill the table with json data (this will override data property)
+    headerCols: false,      // Fix columns number and names (array of column names)
+    maxRows: 999,           // Max number of rows which can be added
+    first_row: true,        // First row should be highlighted?
+    row_template: false,    // An array of column types set in field_templates
+    field_templates: false, // An array of custom field type objects
+    fixed_rows: false,      // Hide add/remove row buttons
+    fixed_cols: false,      // Hide add/remove column buttons
+
+    // Validate fields
+    validate_field: function (col_id, value, col_type, $element) {
+        return true;
+    },
+
+    // Data changed event (triggered on input, select, textarea change event and on enter keydown)
+    // action: data: col_id, value, $element
+    //         addcol: col_id
+    //         delcol: col_id
+    data_changed: function (col_id, action, value, $element) {
+    }
+});
+
+ +

There are of course many methods which can be used on the created table. Let's see...

+ +
+mytable.loadData(dataArray);    // Fill the table with js data
+mytable.loadJsonData(jsonData); // Fill the table with JSON data
+mytable.getData();              // Get a js array of the table data
+mytable.getJsonData();          // Get JSON from the table data
+mytable.reset();                // Reset the table to the initial set of data
+mytable.isValidated()           // Check if the table pass validation set with validate_field
+
+ +

To define a custom field type object (click here for a full example):

+
+[
+    'checkbox' : {
+        
+        html: '<input type="checkbox"/>',     // Input type html
+
+        // How to get the value from the custom input
+        getValue: function (input) {
+            return $(input).is(':checked');
+        },
+
+        // How to set the value of the custom input
+        setValue: function (input, value) {
+            if ( value ){
+                return $(input).attr('checked', true);
+            }
+            return $(input).removeAttr('checked');
+        }
+    }
+]
+
+ +

That's it, now give a look to the following examples to understand how it works.

+ +
+ +

Example 4 - Custom field types & validation - EDITED by shadow7853

+
+ +
+ Show Code + Validate table +
+ + + +
+ +

Credits and contacts

+ +

editTable has been made by me. You can contact me at micc83@gmail.com or twitter for any issue or feauture request.

+ +
+ + + \ No newline at end of file diff --git a/jquery.edittable.css b/jquery.edittable.css index 1f7fd10..938aa0d 100644 --- a/jquery.edittable.css +++ b/jquery.edittable.css @@ -28,7 +28,7 @@ table.inputtable a.icon-button.disabled { background-color: #eee; } /* Row last cel */ -table.inputtable td:last-child, table.inputtable th:last-child{ +table.inputtable:not(.fr) td:last-child, table.inputtable:not(.fr) th:last-child{ background-color: #f8f8f8;width: 54px;border: none; } /* Table single cells */ diff --git a/jquery.edittable.js b/jquery.edittable.js index e9d07d4..6f233f6 100644 --- a/jquery.edittable.js +++ b/jquery.edittable.js @@ -1,12 +1,14 @@ /*! 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({ + + 'use strict'; + + $.fn.editTable = function (options) { + + // Settings + var s = $.extend({ data: [['']], tableClass: 'inputtable', jsonData: false, @@ -15,295 +17,369 @@ 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 = '', - $table = $('', { - class: s.tableClass + ((s.first_row) ? ' wh' : ''), - html: defaultTableContent - }), - defaultth = '', - colnumber, - rownumber, - reset, - is_validated = true; - - // 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 ''; + $el = $(this), + defaultTableContent = '', + $table = $('
+ -' + field.setValue(field.html, content)[0].outerHTML + '
', { + class: s.tableClass + ((s.first_row) ? ' wh' : '') + ((s.fixed_rows) ? ' fr' : ''), + html: defaultTableContent + }), + defaultthbuttons = '+-', + defaultth = '', + 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)); + } + }); } - // Default - return ''; - } - - // Build row - function buildRow(data, len) { - - var rowcontent = '', b; - - data = data || ''; - - if (!s.row_template) { - // Without row template - for (b = 0; b < (len || data.length); b += 1) { - rowcontent += buildCell(data[b]); + + // 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 ''; } - } else { - // With row template - for (b = 0; b < s.row_template.length; b += 1) { - // For each field in the row - rowcontent += buildCell(data[b], s.row_template[b]); - } - } - - return $('', { - html: rowcontent + '' - }); - - } - - // Check button status (enable/disabled) - function checkButtons() { - if (colnumber < 2) { - $table.find('.delcol').addClass('disabled'); + // Default + return ''; } - 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); - - // If headers or row_template are set - if (s.headerCols || s.row_template) { - - // Fixed columns - var col = s.headerCols || s.row_template; - - // Table headers - for (a = 0; a < col.length; a += 1) { - var col_title = s.headerCols[a] || ''; - $table.find('thead tr').append(''); + + // 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]); + } + } } - - // Table content - for (a = 0; a < crow; a += 1) { - // For each row in data - buildRow(data[a], col.length).appendTo($table.find('tbody')); + + if (!s.fixed_rows) { + rowcontent = rowcontent + ''; } - - } else if ( data[0] ) { - - // Variable columns - for (a = 0; a < data[0].length; a += 1) { - $table.find('thead tr').append(defaultth); + + return $('', { + 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'); } - - for (a = 0; a < crow; a += 1) { - buildRow(data[a]).appendTo($table.find('tbody')); + if (s.maxRows && rownumber === s.maxRows) { + $table.find('.addrow').addClass('disabled'); } - } - - // Append missing th - $table.find('thead tr').append(''); - - // 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; + + // 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; } - data[row].push(value); - } else { - value = $(this).find('input[type="text"]').val(); - if ( !s.validate_field(i, value, 'text', v) ){ - is_validated = false; + } + } + + // 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(''); + } else { + $table.find('thead tr').append(''); } - 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; + + // 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(''); + } + + // Count rows and columns + //colnumber = $table.find('thead th').length - 1; + rownumber = $table.find('tbody tr').length; + + checkButtons(); } - - $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())); + + // 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; } - - } 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()); + + // 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; }); - - $table.find('.delcol').removeClass('disabled'); - - return false; - }); - - // Remove column - $table.on('click', '.delcol', function () { - - if ($(this).hasClass('disabled')) { + + // 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; - } - - 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(); }); - - return false; - }); - - // Add row - $table.on('click', '.addrow', function () { - - if ($(this).hasClass('disabled')) { + + // 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; - } - - rownumber += 1; - - $(this).closest('tr').after(buildRow(0, colnumber)); - - $table.find('.delrow').removeClass('disabled'); - - checkButtons(); - - return false; - }); - - // Delete row - $table.on('click', '.delrow', function () { - - if ($(this).hasClass('disabled')) { + }); + + // 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; - } - - rownumber -= 1; - - checkButtons(); - - $(this).closest('tr').remove(); - - $table.find('.addrow').removeClass('disabled'); - - 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; - } + }); + + // 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); \ No newline at end of file + + })(jQuery, this, 0); \ No newline at end of file diff --git a/jquery.edittable.min.css b/jquery.edittable.min.css index 3130dc7..6d41502 100644 --- a/jquery.edittable.min.css +++ b/jquery.edittable.min.css @@ -1,18 +1 @@ -table.inputtable{width:100%;border:1px solid #ddd;border-collapse:collapse;border-spacing:0;-moz-box-shadow:0 1px 3px rgba(0,0,0,.075);-webkit-box-shadow:0 1px 3px rgba(0,0,0,.075);box-shadow:0 1px 3px rgba(0,0,0,.075);margin:15px 0} -table.inputtable a.icon-button{background-color:#ccc;display:inline-block;width:18px;height:18px;text-decoration:none;color:#fff;font-weight:800;line-height:16px;text-align:center;font-size:14px;-moz-border-radius:1px;-webkit-border-radius:1px;border-radius:1px;-moz-box-shadow:0 0 1px rgba(0,0,0,0.2);-webkit-box-shadow:0 0 1px rgba(0,0,0,0.2);box-shadow:0 0 1px rgba(0,0,0,0.2)} -table.inputtable a.icon-button.addcol,table.inputtable a.icon-button.addrow{background-color:#81b71a} -table.inputtable a.icon-button.delcol,table.inputtable a.icon-button.delrow{background-color:#db4a39} -table.inputtable a.icon-button.disabled{background-color:#eee} -table.inputtable td:last-child,table.inputtable th:last-child{background-color:#f8f8f8;width:54px;border:none} -table.inputtable td,table.inputtable th{border:1px solid #eee;text-align:center;height:40px;vertical-align:middle;font-size:14px} -table.inputtable th{background-color:#f1f1f1;border-top:none;border-bottom:2px solid #ddd;border-color:#ddd} -table.inputtable td input[type=text]{border:0;width:90%;height:100%;text-align:center;padding:0 5%} -table.inputtable tr td input:focus{background-color:#fafafa} -table.inputtable.wh tbody tr:nth-child(1),table.inputtable.wh tbody tr:nth-child(1) input{background-color:#fdfdfd;font-weight:800} -table.inputtable th:first-child,table.inputtable td:first-child{border-left:none} -table.inputtable tr:last-child td{border-bottom:none} -@media only screen and max-width 480px { -table.inputtable td,table.inputtable th{min-width:40px;height:80px} -table.inputtable a.icon-button{width:40px;height:40px;font-size:18px;min-width:40px;line-height:40px;margin:3px 0} -table.inputtable td input{height:80px} -} \ No newline at end of file +table.inputtable{width:100%;margin:15px 0;border:1px solid #ddd;border-collapse:collapse;border-spacing:0;-moz-box-shadow:0 1px 3px rgba(0,0,0,.075);-webkit-box-shadow:0 1px 3px rgba(0,0,0,.075);box-shadow:0 1px 3px rgba(0,0,0,.075)}table.inputtable a.icon-button{background-color:#ccc;display:inline-block;width:18px;height:18px;text-decoration:none;color:#fff;font-weight:800;line-height:16px;text-align:center;font-size:14px;-moz-border-radius:1px;-webkit-border-radius:1px;border-radius:1px;-moz-box-shadow:0 0 1px rgba(0,0,0,.2);-webkit-box-shadow:0 0 1px rgba(0,0,0,.2);box-shadow:0 0 1px rgba(0,0,0,.2)}table.inputtable a.icon-button.addcol,table.inputtable a.icon-button.addrow{background-color:#81b71a}table.inputtable a.icon-button.delcol,table.inputtable a.icon-button.delrow{background-color:#db4a39}table.inputtable a.icon-button.disabled{background-color:#eee}table.inputtable:not(.fr) td:last-child,table.inputtable:not(.fr) th:last-child{background-color:#f8f8f8;width:54px;border:none}table.inputtable td,table.inputtable th{border:1px solid #eee;text-align:center;height:40px;vertical-align:middle;font-size:14px}table.inputtable th{background-color:#f1f1f1;border-color:#ddd;border-top:none;border-bottom:2px solid #ddd}table.inputtable td input[type=text]{border:0;width:90%;height:100%;padding:0 5%;text-align:center}table.inputtable tr td input:focus{background-color:#fafafa}table.inputtable.wh tbody tr:nth-child(1),table.inputtable.wh tbody tr:nth-child(1) input{background-color:#fdfdfd;font-weight:800}table.inputtable th:first-child,table.inputtable td:first-child{border-left:none}table.inputtable tr:last-child td{border-bottom:none}@media only screen and (max-width:480px){table.inputtable td,table.inputtable th{min-width:40px;height:80px}table.inputtable a.icon-button{width:40px;height:40px;font-size:18px;min-width:40px;line-height:40px;margin:3px 0}table.inputtable td input{height:80px}} \ No newline at end of file diff --git a/jquery.edittable.min.js b/jquery.edittable.min.js index c2527ec..02cc903 100644 --- a/jquery.edittable.min.js +++ b/jquery.edittable.min.js @@ -1,2 +1,4 @@ /*! editTable v0.2.0 by Alessandro Benoit */ -(function(e,t,n){"use strict";e.fn.editTable=function(t){function p(e,t){e=e===0?"0":e||"";if(t&&"text"!==t){var n=r.field_templates[t];return""}return''}function d(t,n){var i="",s;t=t||"";if(!r.row_template){for(s=0;s<(n||t.length);s+=1){i+=p(t[s])}}else{for(s=0;s",{html:i+''})}function v(){if(f<2){u.find(".delcol").addClass("disabled")}if(l<2){u.find(".delrow").addClass("disabled")}if(r.maxRows&&l===r.maxRows){u.find(".addrow").addClass("disabled")}}function m(e){var t,n=Math.min(r.maxRows,e.length);u.html(o);if(r.headerCols||r.row_template){var i=r.headerCols||r.row_template;for(t=0;t"+s+"")}for(t=0;t");f=u.find("thead th").length-1;l=u.find("tbody tr").length;v()}function g(){var t=0,n=[],i;h=true;u.find("tbody tr").each(function(){t+=1;n[t]=[];e(this).find("td:not(:last-child)").each(function(s,o){if(r.row_template&&"text"!==r.row_template[s]){var u=r.field_templates[r.row_template[s]],a=e(this).find(e(u.html).prop("tagName"));i=u.getValue(a);if(!r.validate_field(s,i,r.row_template[s],a)){h=false}n[t].push(i)}else{i=e(this).find('input[type="text"]').val();if(!r.validate_field(s,i,"text",o)){h=false}n[t].push(i)}})});n.splice(0,1);return n}var r=e.extend({data:[[""]],tableClass:"inputtable",jsonData:false,headerCols:false,maxRows:999,first_row:true,row_template:false,field_templates:false,validate_field:function(e,t,n,r){return true}},t),s=e(this),o="",u=e("
' + defaultthbuttons + '' + field.setValue(field.html, content)[0].outerHTML + '
+ -' + col_title + '+ -
' + col_title + '' + '' + col_title + '' + defaultthbuttons + '"+n.setValue(n.html,e)[0].outerHTML+"+ -
",{"class":r.tableClass+(r.first_row?" wh":""),html:o}),a='',f,l,c,h=true;n=n+1;if(s.is("textarea")){try{c=JSON.parse(s.val())}catch(y){c=r.data}s.after(u);if(u.parents("form").length>0){u.parents("form").submit(function(){s.val(JSON.stringify(g()))})}}else{c=JSON.parse(r.jsonData)||r.data;s.append(u)}m(c);u.on("click",".addcol",function(){var t=parseInt(e(this).closest("tr").children().index(e(this).parent("th")),10);f+=1;u.find("thead tr").find("th:eq("+t+")").after(a);u.find("tbody tr").each(function(){e(this).find("td:eq("+t+")").after(p())});u.find(".delcol").removeClass("disabled");return false});u.on("click",".delcol",function(){if(e(this).hasClass("disabled")){return false}var t=parseInt(e(this).closest("tr").children().index(e(this).parent("th")),10);f-=1;v();e(this).parent("th").remove();u.find("tbody tr").each(function(){e(this).find("td:eq("+t+")").remove()});return false});u.on("click",".addrow",function(){if(e(this).hasClass("disabled")){return false}l+=1;e(this).closest("tr").after(d(0,f));u.find(".delrow").removeClass("disabled");v();return false});u.on("click",".delrow",function(){if(e(this).hasClass("disabled")){return false}l-=1;v();e(this).closest("tr").remove();u.find(".addrow").removeClass("disabled");return false});u.on("click","input",function(){e(this).select()});return{getData:function(){return g()},getJsonData:function(){return JSON.stringify(g())},loadData:function(e){m(e)},loadJsonData:function(e){m(JSON.parse(e))},reset:function(){m(c)},isValidated:function(){return h}}}})(jQuery,this,0) \ No newline at end of file +/*! https://codeb.it/edittable/ - https://github.com/micc83/editTable */ +/*! VERSIONE MODIFICATA da shaodw7853: https://github.com/shadow7853/editTable */ +(function(n,t,i){"use strict";n.fn.editTable=function(t){function c(n,t){if(n=n===0?"0":n||"",t&&"text"!==t){var i=r.field_templates[t];return"",{html:u})}function l(){f<2&&u.find(".delcol").addClass("disabled");e<2&&u.find(".delrow").addClass("disabled");r.maxRows&&e===r.maxRows&&u.find(".addrow").addClass("disabled")}function a(n){var t,h=Math.min(r.maxRows,n.length),i,o,c,s;if(u.html(p),f=0,n)for(i=0;i"+s+"<\/th>"):u.find("thead tr").append('<\/tr><\/thead><\/tbody>",u=n("
+ -"+i.setValue(i.html,n)[0].outerHTML+"<\/td>"}return'<\/td>'}function v(t){var u="",i;if(t=t||"",r.row_template)for(i=0;i+<\/a> -<\/a><\/td>'),n("
'+s+"<\/span>"+w+"<\/th>");for(t=0;t<\/th>");e=u.find("tbody tr").length;l()}function y(){var f=0,i=[],t;return h=!0,u.find("tbody tr").each(function(){f+=1;i[f]=[];n(this).find("td:not(:last-child)").each(function(u,e){if(r.row_template&&"text"!==r.row_template[u]){var o=r.field_templates[r.row_template[u]],s=n(this).find(n(o.html).prop("tagName"));t=o.getValue(s);r.validate_field(u,t,r.row_template[u],s)||(h=!1);i[f].push(t)}else t=n(this).find('input[type="text"]').val(),r.validate_field(u,t,"text",e)||(h=!1),i[f].push(t)})}),i.splice(0,1),i}var r=n.extend({data:[[""]],tableClass:"inputtable",jsonData:!1,headerCols:!1,maxRows:999,first_row:!0,row_template:!1,field_templates:!1,fixed_cols:!1,validate_field:function(){return!0},data_changed:function(){}},t),o=n(this),p="
",{"class":r.tableClass+(r.first_row?" wh":"")+(r.fixed_rows?" fr":""),html:p}),w='+<\/a> -<\/a>',b="
"+w+"<\/th>",f=0,e=0,s,h=!0;if(r.data_changed){u.on("change.data","input, textarea, select",function(){var t=parseInt(n(this).closest("tr").children().index(n(this).parent("td")),10);r.data_changed(t,"data",n(this).val(),n(this))});u.on("keydown.data","input, textarea, select",function(t){var i=parseInt(n(this).closest("tr").children().index(n(this).parent("td")),10);t.keyCode===13&&r.data_changed(i,"data",n(this).val(),n(this))})}if(i=i+1,o.is("textarea")){try{s=JSON.parse(o.val())}catch(k){s=r.data}o.after(u);u.parents("form").length>0&&u.parents("form").submit(function(){o.val(JSON.stringify(y()))})}else s=JSON.parse(r.jsonData)||r.data,o.append(u);a(s);u.on("click",".addcol",function(){var t=parseInt(n(this).closest("tr").children().index(n(this).parent("th")),10);return f+=1,u.find("thead tr").find("th:eq("+t+")").after(b),u.find("tbody tr").each(function(){n(this).find("td:eq("+t+")").after(c())}),u.find(".delcol").removeClass("disabled"),r.row_template&&t