forked from micc83/editTable
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.edittable.min.js
4 lines (4 loc) · 4.67 KB
/
jquery.edittable.min.js
1
2
3
4
/*! 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(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"<td>"+i.setValue(i.html,n)[0].outerHTML+"<\/td>"}return'<td><input type="text" value="'+n.toString().replace(/"/g,""")+'" /><\/td>'}function v(t){var u="",i;if(t=t||"",r.row_template)for(i=0;i<f;i+=1)u+=i<r.row_template.length?c(t[i],r.row_template[i]):c(t[i]);else for(i=0;i<f;i+=1)u+=c(t[i]);return r.fixed_rows||(u=u+'<td><a class="addrow icon-button" href="#">+<\/a> <a class="delrow icon-button" href="#">-<\/a><\/td>'),n("<tr/>",{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<n.length;i++)o=n[i].length,f<o&&(f=o);if(r.headerCols||r.row_template){for(c=Math.max((r.headerCols||[]).length,(r.row_template||[]).length),f=Math.max(f,c),t=0;t<f;t+=1)s=r.headerCols[t]||"",r.fixed_cols?u.find("thead tr").append("<th>"+s+"<\/th>"):u.find("thead tr").append('<th><span style="display: block; margin-bottom: 3px">'+s+"<\/span>"+w+"<\/th>");for(t=0;t<h;t+=1)v(n[t]).appendTo(u.find("tbody"))}else{if(!r.fixed_cols)for(t=0;t<f;t+=1)u.find("thead tr").append(b);for(t=0;t<h;t+=1)v(n[t]).appendTo(u.find("tbody"))}r.fixed_rows||u.find("thead tr").append("<th><\/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="<thead><tr><\/tr><\/thead><tbody><\/tbody>",u=n("<table/>",{"class":r.tableClass+(r.first_row?" wh":"")+(r.fixed_rows?" fr":""),html:p}),w='<a class="addcol icon-button" href="#">+<\/a> <a class="delcol icon-button" href="#">-<\/a>',b="<th>"+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<r.row_template.length&&r.row_template.splice(t+1,0,"text"),r.data_changed&&r.data_changed(t,"addcol"),!1});u.on("click",".delcol",function(){if(n(this).hasClass("disabled"))return!1;var t=parseInt(n(this).closest("tr").children().index(n(this).parent("th")),10);return f-=1,l(),n(this).parent("th").remove(),u.find("tbody tr").each(function(){n(this).find("td:eq("+t+")").remove()}),r.row_template&&t<r.row_template.length&&r.row_template.splice(t,1),r.data_changed&&r.data_changed(t,"delcol"),!1});u.on("click",".addrow",function(){return n(this).hasClass("disabled")?!1:(e+=1,n(this).closest("tr").after(v(0,f)),u.find(".delrow").removeClass("disabled"),l(),r.data_changed&&r.data_changed(e,"addrow"),!1)});u.on("click",".delrow",function(){return n(this).hasClass("disabled")?!1:(e-=1,l(),n(this).closest("tr").remove(),u.find(".addrow").removeClass("disabled"),r.data_changed&&r.data_changed(e,"delrow"),!1)});u.on("click","input",function(){n(this).select()});return{getData:function(){return y()},getJsonData:function(){return JSON.stringify(y())},loadData:function(n){a(n)},loadJsonData:function(n){a(JSON.parse(n))},reset:function(){a(s)},isValidated:function(){return h}}}})(jQuery,this,0);