dyno-table-1.0.0.js
2.96 KB
$(document).ready(function(){!function($){$.fn.extend({dynoTable:function(options){var defaults={removeClass:".row-remover",cloneClass:".row-cloner",addRowTemplateId:"#add-template",addRowButtonId:"#add-row",lastRowRemovable:!0,orderable:!0,dragHandleClass:".drag-handle",insertFadeSpeed:"slow",removeFadeSpeed:"fast",hideTableOnEmpty:!0,onRowRemove:function(){},onRowClone:function(clonedRow){},onRowAdd:function(){},onTableEmpty:function(){},onRowReorder:function(){}};options=$.extend(defaults,options);var cloneRow=function(btn){var clonedRow=$(btn).closest("tr").clone(),tbod=$(btn).closest("tbody");insertRow(clonedRow,tbod),options.onRowClone(clonedRow)},insertRow=function(clonedRow,tbod){var numRows=$(tbod).children("tr").length;options.hideTableOnEmpty&&0==numRows&&$(tbod).parents("table").first().show(),$(clonedRow).find("*").andSelf().filter("[id]").each(function(){this.id+="__c"}),$(tbod).append(clonedRow),bindActions(clonedRow),$(tbod).children("tr:last").hide().fadeIn(options.insertFadeSpeed)},removeRow=function(btn){var tbod=$(btn).parents("tbody:first"),numRows=$(tbod).children("tr").length;if(numRows>1||options.lastRowRemovable===!0){var trToRemove=$(btn).parents("tr:first");$(trToRemove).fadeOut(options.removeFadeSpeed,function(){$(trToRemove).remove(),options.onRowRemove(),1==numRows&&(options.hideTableOnEmpty&&$(tbod).parents("table").first().hide(),options.onTableEmpty())})}},bindClick=function(elem,fn){$(elem).click(fn)},bindCloneLink=function(lnk){bindClick(lnk,function(){var btn=$(this);return cloneRow(btn),!1})},bindRemoveLink=function(lnk){bindClick(lnk,function(){var btn=$(this);return removeRow(btn),!1})},bindActions=function(obj){obj.find(options.removeClass).each(function(){bindRemoveLink($(this))}),obj.find(options.cloneClass).each(function(){bindCloneLink($(this))})};return this.each(function(){if("table"==this.nodeName.toLowerCase()){var table=$(this),tbody=$(table).children("tbody").first();options.orderable&&jQuery().sortable&&$(tbody).sortable({handle:options.dragHandleClass,helper:function(e,ui){return ui.children().each(function(){$(this).width($(this).width())}),ui},items:"tr",update:function(event,ui){options.onRowReorder()}}),$(table).find(options.addRowTemplateId).each(function(){$(this).removeAttr("id");var tmpl=$(this);tmpl.remove(),bindClick($(options.addRowButtonId),function(){var newTr=tmpl.clone();return insertRow(newTr,tbody),options.onRowAdd(),!1})}),bindActions(table);var numRows=$(tbody).children("tr").length;options.hideTableOnEmpty&&0==numRows&&$(table).hide()}})}})}(jQuery),$("#add_prop").dynoTable({removeClass:".row-remover",cloneClass:".row-cloner",addRowTemplateId:"#add-template",addRowButtonId:"#add-row",lastRowRemovable:!0,orderable:!0,dragHandleClass:".drag-handle",insertFadeSpeed:"slow",removeFadeSpeed:"fast",hideTableOnEmpty:!0,onRowRemove:function(){},onRowClone:function(clonedRow){clonedRow.find('input[name="PropValue[value_id][]"]').val("")},onRowAdd:function(){},onTableEmpty:function(){},onRowReorder:function(){}})});