-
Notifications
You must be signed in to change notification settings - Fork 6
/
jquery-editable-table.min.js
1 lines (1 loc) · 4.25 KB
/
jquery-editable-table.min.js
1
$.fn.editableTable=function(t){t=$.extend({},{cloneProperties:["padding","padding-top","padding-bottom","padding-left","padding-right","text-align","font","font-size","font-family","font-weight","border","border-top","border-bottom","border-left","border-right","color","background-color","border-radius"],columns:[]},t);let e,n="error",i="table-editor-input",o=$(this);o.find("th").show(),t.columns.forEach((t,e)=>{void 0!==t.isHidden&&t.isHidden&&o.find("th").eq(e).hide()}),void 0!==t.actions&&t.actions.length>0&&!o.find("thead tr th[actions]").length&&o.find("thead tr").append($('<th actions=""></th>'));let r,d=o.parent().find(`input[${i}]`);(r=d.length?d.first():$("<input>")).attr(i,"").css("position","absolute").hide().appendTo(o.parent());let a;function l(e){if((a=o.find("td:focus:not([action])")).length){let i=$(a).parent().children("td").index($(a)),d=t.columns[i];r.val(a.text()).removeClass(n).show().offset(a.offset()).css(a.css(t.cloneProperties)).width(a.width()).height(a.height()).focus(),void 0!==d.maxLength?r.attr("maxlength",d.maxLength):r.removeAttr("maxlength"),e&&r.select()}}function s(){let t=r.val(),e=$.Event("change"),i;if(a.text()===t||r.hasClass(n))return!0;i=a.html(),a.text(t).trigger(e,t),!1===e.result&&a.html(i)}function c(t,e){return 39===e?t.next("td"):37===e?t.prev("td"):38===e?t.parent().prev().children().eq(t.index()):40===e?t.parent().next().children().eq(t.index()):[]}return r.blur(function(){s(),r.hide()}),r.keydown(function(t){if(13===t.which)s(),r.hide(),a.focus(),t.preventDefault(),t.stopPropagation();else if(27===t.which)r.val(a.text()),t.preventDefault(),t.stopPropagation(),r.hide(),a.focus();else if(9===t.which)a.focus();else if(this.selectionEnd-this.selectionStart===this.value.length){let e=c(a,t.which);e.length>0&&(e.focus(),t.preventDefault(),t.stopPropagation())}}),r.on("input paste",function(){let t=$.Event("validate");a.trigger(t,r.val()),void 0!==t.result&&(!1===t.result?r.addClass(n):r.removeClass(n))}),o.on("click keypress dblclick",l),o.keydown(function(t){let e=!0,n=c($(t.target),t.which);n.length>0?n.focus():13===t.which?l(!1):((17===t.which||91===t.which||93===t.which)&&l(!0),e=!1),e&&(t.stopPropagation(),t.preventDefault())}),o.find("td").prop("tabindex",1),$(window).on("resize",function(){r.is(":visible")&&r.offset(a.offset()).width(a.width()).height(a.height())}),$("table td").on("validate",function(n,i){let o=$(n.currentTarget).parent().children("td").index($(n.currentTarget)),r=t.columns[o],d=e.getData({convert:!1});return r.isValid&&r.isValid(i,d)}),$("table td").on("change",function(e,n){let i=$(this),o=$(e.currentTarget).parent().children("td").index($(e.currentTarget)),r=t.columns[o];return r.removeRowIfCleared&&""==n&&i.parent("tr").remove(),"function"==typeof r.afterChange&&r.afterChange(n,i),!0}),e={getData:function(e){e=$.extend({},{convert:!0},e);let n=[];return o.find("tbody tr").toArray().forEach(i=>{let o={};$(i).find("td:not([action])").toArray().forEach(n=>{let i=t.columns[$(n).parent().children("td").index($(n))],r=$(n).text(),d=$(n).attr("data-is-null");void 0!==d&&!1!==d&&(r=null),e.convert&&"function"==typeof i.convertOut&&(r=i.convertOut(r)),o[i.name]=r}),n.push(o)}),n},addRow:function(e){let n=$("<tr></tr>");if(null!=e){let i=Object.keys(e),r=[];i.forEach(n=>{let i=t.columns.filter(t=>t.name===n);i.length&&(i=i[0],r.push({order:i.index,value:e[n],prop:n,def:i}))}),r.sort((t,e)=>t.order-e.order).forEach((e,i)=>{let o;o=null!==e.value?$(`<td>${e.value}</td>`):$("<td data-is-null></td>"),void 0!==e.def.classes&&e.def.classes.length&&e.def.classes.forEach(t=>o.addClass(t)),void 0!==e.def.style&&e.def.style.length&&o.attr("style",o.attr("style")+"; "+e.def.style),void 0!==e.def.isHidden&&e.def.isHidden&&o.hide(),n.append(o);let r=t.columns.filter(t=>t.name===e.prop)[0];"function"==typeof r.afterAdd&&r.afterAdd(e.value,o)})}else n=$("<tr></tr>"),activeOptions.columns.forEach(t=>{n.append("<td></td>")});if(void 0!==t.actions&&t.actions.length>0){let d=$("<td action></td>");t.actions.forEach(t=>{let e=$(t.label);e.css("cursor","pointer"),e.click(e=>t.action(e,n)),d.append(e)}),n.append(d)}let a=o.find("tbody tr:last");a.length>0?a.after(n):o.find("tbody").append(n),$(o).editableTable(t)},clear:function(){o.find("tbody tr").remove()},setData:function(t){t&&(this.clear(),t.forEach(t=>{this.addRow(t)}))}}};