PageRenderTime 20ms CodeModel.GetById 3ms app.highlight 14ms RepoModel.GetById 1ms app.codeStats 0ms

/apps/request/public/js/jquery.table.addrow.js

http://zoop.googlecode.com/
JavaScript | 221 lines | 212 code | 0 blank | 9 comment | 52 complexity | dfc54fc003c9086f3e868abdee4bba97 MD5 | raw file
  1/*
  2 *
  3 * Copyright (c) 2009 C. F., Wong (<a href="http://cloudgen.w0ng.hk">Cloudgen Examplet Store</a>)
  4 * Licensed under the MIT License:
  5 * http://www.opensource.org/licenses/mit-license.php
  6 * 
  7 * See details in: <a href="http://cloudgen.w0ng.hk/javascript/javascript.php">Javascript Examplet</a>
  8 *
  9 */
 10(function($){
 11	var ExpandableTableList=[],className="ExpandableTable";
 12	function ExpandableTable(target,maxRow){
 13		if(target) this.init(target,maxRow);
 14	}
 15	ExpandableTable.prototype.init=function(target,maxRow){
 16		ExpandableTableList.push(this);
 17		this.target=$(target).data(className,this);
 18		this.maxRow=maxRow;
 19		this.seed=Math.round(Math.random()*10000);
 20		this.onAddRow=[];
 21		return this
 22	};
 23	ExpandableTable.prototype.live=function(){
 24		if (!this.goLive){
 25			var t=this;
 26			this.update();
 27			$(".addRow"+this.seed)
 28			.live("click",function(){
 29				var newRow=t.addRow();
 30			});
 31			$(".delRow"+this.seed)
 32			.live("click",function(){
 33				var o=$(this).closest("tr")
 34				.clone();
 35				$(this)
 36				.closest("tr")
 37				.remove();
 38				$(".addRow"+t.seed)
 39				.attr("disabled",false);
 40				t.update();
 41				if(t.deleteCallBack && $.isFunction(t.deleteCallBack)) 
 42					t.deleteCallBack(o);
 43			});
 44			$(".autoAdd"+this.seed)
 45			.live("keyup",function(){
 46				if((this.nodeName.toLowerCase()=="textarea" && $(this).html()!="") ||
 47				(this.nodeName.toLowerCase()=="textarea" && $(this).val()!="") ||
 48				(this.nodeName.toLowerCase()=="input" && $(this).val()!="")) t.addRow();
 49			});
 50			this.goLive=true;
 51		}
 52		return this
 53	};
 54	ExpandableTable.prototype.updateRowNumber=function(){
 55		if(this.rowNumColumn){
 56		this.target.find("."+this.rowNumColumn).each(function(i,v){
 57				if(i+1!=parseInt($(this).text())) $(this).text(i+1)
 58			});
 59		}
 60		return this
 61	};
 62	ExpandableTable.prototype.updateInputBoxName=function(){
 63		$(".delRow"+this.seed).closest("tr").each(function(i,v){
 64			var n=i+1;
 65			$("input,textarea",this).each(function(i,v){
 66				if($(this).attr("name")!=""){
 67					var newName=$(this).attr("name").replace(/\d+$/,"")+n;
 68					$(this).attr("name",newName);
 69				}
 70			});
 71		});
 72	}
 73	ExpandableTable.prototype.updateOddRowCSS=function(){
 74		if(this.oddRowCSS){
 75			this.target.find("."+this.oddRowCSS).removeClass(this.oddRowCSS);
 76			this.target.find("tr:odd").addClass(this.oddRowCSS);
 77		}
 78		return this
 79	};
 80	ExpandableTable.prototype.updateEvenRowCSS=function(){
 81		if(this.evenRowCSS){
 82			this.target.find("."+this.evenRowCSS).removeClass(this.evenRowCSS);
 83			this.target.find("tr:even").addClass(this.evenRowCSS);
 84		}
 85		return this
 86	};
 87	ExpandableTable.prototype.update=function(){
 88		var t=this;
 89		this.delRowButtons=$(".delRow"+this.seed,this.target);
 90		if(this.delRowButtons.size()==1)
 91			this.delRowButtons.hide();
 92		else {
 93			if(this.autoAddRow)
 94				this.delRowButtons.not(":last").show();
 95			else
 96				this.delRowButtons.show();
 97		}
 98		if(this.autoAddRow) {
 99			this.target.find(".autoAdd"+this.seed).removeClass("autoAdd"+t.seed);
100			this.target
101			.find(".delRow"+this.seed+":last")
102			.closest("tr")
103			.find("input,textarea")
104			.addClass("autoAdd"+this.seed);
105		}
106		if(this.inputBoxAutoNumber) this.updateInputBoxName();
107		if(this.displayRowCountTo) 
108			$("."+this.displayRowCountTo).val(
109				$(".delRow"+this.seed).closest("tr").size()
110			);
111		this.updateRowNumber()
112		.updateOddRowCSS()
113		.updateEvenRowCSS();
114		return this
115	};
116	ExpandableTable.prototype.addRow=function(){
117		var newRow;
118		if(!this.maxRow || (this.maxRow && $(".delRow"+this.seed).size()<this.maxRow)){
119			this.delRowButtons.show();
120			var lastRow=$(".delRow"+this.seed+":last",this.target).closest("tr");
121			this.newRow=newRow=lastRow.clone();
122			newRow.find("input:text").val("");
123			newRow.find("input:hidden").not(".delRow"+this.seed).not(".addRow"+this.seed).val("");
124			newRow.find("textarea").text("");
125			if(this.autoAddRow) newRow.find(".delRow"+this.seed).hide();
126			newRow.insertAfter(lastRow);
127			if(this.ignoreClass && this.ignoreClass!=""){
128				newRow.find("."+this.ignoreClass).each(function(){
129					if(this.nodeName.toLowerCase()=="input" &&
130						($(this).attr("type").toLowerCase()=="text"
131						|| $(this).attr("type").toLowerCase()=="hidden"
132					)) $(this).val("");
133					else if(this.nodeName.toLowerCase()=="td") $(this).html(" ");
134					else if($(this).html()!="") $(this).text("");
135				});
136			}
137			if(this.maxRow && $(".delRow"+this.seed).size()>=this.maxRow) 
138				$(".addRow"+this.seed).attr("disabled",true);
139			this.target.find(".delRow"+this.seed+":first").closest("tr").find("*").each(function(i,v){
140				if($(this).data("init")) $.each($(this).data("init"),function(j,v){
141					this(newRow.find("*").eq(i)[0]);
142				});
143			});
144		}
145		this.update();
146		if(this.addCallBack && $.isFunction(this.addCallBack))
147			this.addCallBack(newRow);
148		return newRow
149	};
150	$.fn.btnAddRow=$.fn.tableAutoAddRow=function(options,func){
151		var callBack;
152		if (typeof options=="object")
153			callBack=(func && $.isFunction(func)) ? func :null; 
154		else
155			callBack=(options && $.isFunction(options)) ? options :null; 
156		options=$.extend({maxRow:null,ignoreClass:null,rowNumColumn:null,autoAddRow:false,oddRowCSS:null,evenRowCSS:null,inputBoxAutoNumber:false,displayRowCountTo:null},options);
157		this.each(function(){
158			var tbl=(this.nodeName.toLowerCase()=="table")?$(this):$(this).closest("table"),etbl;
159			if(tbl.size()>0){
160				if(typeof tbl.data(className)==="undefined"){
161					etbl=new ExpandableTable(tbl,options.maxRow);
162					if(this.nodeName.toLowerCase()!="table")
163						$(this)
164							.addClass("addRow"+etbl.seed)
165							.data(className,etbl);
166				}else{
167					if(this.nodeName.toLowerCase()!="table")
168						$(this)
169							.addClass("addRow"+tbl.data(className).seed)
170							.data(className,tbl.data(className));
171				}
172				if($(this).data(className)) {
173					etbl=$(this).data(className);
174				} 
175				etbl.update().addCallBack=callBack;
176				etbl.maxRow=options.maxRow;
177				etbl.maxRow=options.maxRow;
178				etbl.ignoreClass=options.ignoreClass;
179				etbl.rowNumColumn=options.rowNumColumn;
180				etbl.oddRowCSS=options.oddRowCSS;
181				etbl.evenRowCSS=options.evenRowCSS;
182				etbl.autoAddRow=options.autoAddRow;
183				etbl.inputBoxAutoNumber=options.inputBoxAutoNumber;
184				etbl.displayRowCountTo=options.displayRowCountTo;
185			};
186		});
187		for(var i=0;i<ExpandableTableList.length;i++){
188			if(!ExpandableTableList[i].goLive){
189				ExpandableTableList[i].live();
190			}
191		}
192	};
193	$.fn.btnDelRow=function(func){
194		var callBack=(func && $.isFunction(func)) ? func :null; 
195		this.each(function(){
196			var tbl=$(this).hide().closest("table");
197			if(tbl.size()>0){
198				if(typeof tbl.data(className)==="undefined"){
199					var etbl=new ExpandableTable(tbl);
200					$(this)
201						.addClass("delRow"+etbl.seed)
202						.data(className,etbl);
203				}else{
204					$(this)
205						.addClass("delRow"+tbl.data(className).seed)
206						.data(className,tbl.data(className));
207				}
208				if($(this).data(className)) {
209					$(this)
210					.data(className)
211					.update().deleteCallBack=callBack;
212				}
213			}
214		});
215		for(var i=0;i<ExpandableTableList.length;i++){
216			if(!ExpandableTableList[i].goLive){
217				ExpandableTableList[i].live();
218			}
219		}
220	};
221})(jQuery);