表格编辑表头多行并列多列并列

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>编辑表格数据</title>

<style type="text/css">
body,div,p,ul,li,font,span,td,th{font-size:10pt;line-height:155%;}
table{border-top-width:1px;border-right-width:1px;border-bottom-width:0px;border-left-width:1px;border-top-style:solid;border-right-style:solid;border-bottom-style:none;border-left-style:solid;border-top-color:#CCCCCC;border-right-color:#CCCCCC;border-bottom-color:#CCCCCC;border-left-color:#CCCCCC;}
td{border-bottom-width:1px;border-bottom-style:solid;border-bottom-color:#CCCCCC;}
.EditCell_TextBox{width:90%;border:1px solid #0099CC;}
.EditCell_DropDownList{width:90%;}
</style>

</head>

<body>

<!div style="width:700px;margin:0 auto;">
	<form id="form1" name="form1" method="post" action="">
		<h3>可编辑的表格</h3>
		<div id="tablediv" style="OVERFLOW: auto;width:1330px;height:300x;">
		<table id="tabProduct" border="1" width="1000">
		<!table width="698" border="0" cellpadding="0" cellspacing="0" id="tabProduct">
		<tbody align="center">
		
		<tr class="tabTh">
				<td rowspan="3" colspan="2" width="100" nowrap="nowrap">险种大类</td>
				<td colspan="3" nowrap="nowrap">201401</td>
				<td colspan="3" nowrap="nowrap">201402</td>
				<td colspan="3" nowrap="nowrap">201403</td>
			</tr>
			<tr >
				<td colspan="3" nowrap="nowrap">保费收入-再保前</td>
				<td colspan="3" nowrap="nowrap">保费收入-再保前</td>
				<td colspan="3" nowrap="nowrap">保费收入-再保前</td>
			</tr>
			<tr >
				<td width="100" nowrap="nowrap" id="business" EditType="TextBox">业务</td>
				<td width="100" nowrap="nowrap" id="finance" EditType="TextBox">财务</td>
				<td width="100" nowrap="nowrap" id="difference" EditType="TextBox">差异</td>
				<td width="100" nowrap="nowrap" EditType="TextBox">业务</td>
				<td width="100" nowrap="nowrap" EditType="TextBox">财务</td>
				<td width="100" nowrap="nowrap" EditType="TextBox">差异</td>
				<td width="100" nowrap="nowrap" EditType="TextBox">业务</td>
				<td width="100" nowrap="nowrap" EditType="TextBox">财务</td>
				<td width="100" nowrap="nowrap" EditType="TextBox">差异</td>
			</tr>
			
			<tr >
				<td rowspan="3" width="100" nowrap="nowrap" >A大类大类</td>	
                <td nowrap="nowrap" EditType="TextBox">A1</td>					
				<td nowrap="nowrap" EditType="TextBox">1234</td>
				<td nowrap="nowrap" EditType="TextBox">1,550,703.52</td>
				<td nowrap="nowrap" EditType="TextBox">0</td>
				<td nowrap="nowrap" EditType="TextBox">33,253.36</td>
				<td nowrap="nowrap" EditType="TextBox">33,751.68</td>
				<td nowrap="nowrap" EditType="TextBox">-498.32</td>
				<td nowrap="nowrap" EditType="TextBox">359,825.44</td>
				<td nowrap="nowrap" EditType="TextBox">359,327.12</td>
				<td nowrap="nowrap" EditType="TextBox">498.32</td>
			</tr>	
			<tr>	
			    <td nowrap="nowrap" EditType="TextBox">A2</td>	
				<td nowrap="nowrap" EditType="TextBox">1,550,703.52</td>
				<td nowrap="nowrap" EditType="TextBox">1,550,703.52</td>
				<td nowrap="nowrap" EditType="TextBox">0</td>
				<td nowrap="nowrap" EditType="TextBox">33,253.36</td>
				<td nowrap="nowrap" EditType="TextBox">33,751.68</td>
				<td nowrap="nowrap" EditType="TextBox">-498.32</td>
				<td nowrap="nowrap" EditType="TextBox">359,825.44</td>
				<td nowrap="nowrap" EditType="TextBox">359,327.12</td>
				<td nowrap="nowrap" EditType="TextBox">498.32</td>
			</tr>	
			<tr>		
                <td nowrap="nowrap" EditType="TextBox">A3</td>				
				<td nowrap="nowrap" EditType="TextBox">1,550,703.52</td>
				<td nowrap="nowrap" EditType="TextBox">1,550,703.52</td>
				<td nowrap="nowrap" EditType="TextBox">0</td>
				<td nowrap="nowrap" EditType="TextBox">33,253.36</td>
				<td nowrap="nowrap" EditType="TextBox">33,751.68</td>
				<td nowrap="nowrap" EditType="TextBox">-498.32</td>
				<td nowrap="nowrap" EditType="TextBox">359,825.44</td>
				<td nowrap="nowrap" EditType="TextBox">359,327.12</td>
				<td nowrap="nowrap" EditType="TextBox">498.32</td>				
			</tr>
			
			
			<tr class="tabTh">
				<td rowspan="2" width="100" nowrap="nowrap">B大类大类</td>	
                <td nowrap="nowrap" EditType="TextBox">B1</td>				
				<td nowrap="nowrap" EditType="TextBox">1,550,703.52</td>
				<td nowrap="nowrap" EditType="TextBox">1,550,703.52</td>
				<td nowrap="nowrap" EditType="TextBox">0</td>
				<td nowrap="nowrap" EditType="TextBox">33,253.36</td>
				<td nowrap="nowrap" EditType="TextBox">33,751.68</td>
				<td nowrap="nowrap" EditType="TextBox">-498.32</td>
				<td nowrap="nowrap" EditType="TextBox">359,825.44</td>
				<td nowrap="nowrap" EditType="TextBox">359,327.12</td>
				<td nowrap="nowrap" EditType="TextBox">498.32</td>
			</tr>	
			<tr>	
			    <td nowrap="nowrap" EditType="TextBox">B2</td>	
				<td nowrap="nowrap" EditType="TextBox">1,550,703.52</td>
				<td nowrap="nowrap" EditType="TextBox">1,550,703.52</td>
				<td nowrap="nowrap" EditType="TextBox">0</td>
				<td nowrap="nowrap" EditType="TextBox">33,253.36</td>
				<td nowrap="nowrap" EditType="TextBox">33,751.68</td>
				<td nowrap="nowrap" EditType="TextBox">-498.32</td>
				<td nowrap="nowrap" EditType="TextBox">359,825.44</td>
				<td nowrap="nowrap" EditType="TextBox">359,327.12</td>
				<td nowrap="nowrap" EditType="TextBox">498.32</td>
			</tr>	


			<tr class="tabTh">
				<td rowspan="3" width="100" nowrap="nowrap">A大类大类</td>	
                <td nowrap="nowrap" EditType="TextBox">A1</td>				
				<td nowrap="nowrap" EditType="TextBox">1,550,703.52</td>
				<td nowrap="nowrap" EditType="TextBox">1,550,703.52</td>
				<td nowrap="nowrap" EditType="TextBox">0</td>
				<td nowrap="nowrap" EditType="TextBox">33,253.36</td>
				<td nowrap="nowrap" EditType="TextBox">33,751.68</td>
				<td nowrap="nowrap" EditType="TextBox">-498.32</td>
				<td nowrap="nowrap" EditType="TextBox">359,825.44</td>
				<td nowrap="nowrap" EditType="TextBox">359,327.12</td>
				<td nowrap="nowrap" EditType="TextBox">498.32</td>
			</tr>	
			<tr>	
			    <td nowrap="nowrap" EditType="TextBox">A2</td>	
				<td nowrap="nowrap" EditType="TextBox">1,550,703.52</td>
				<td nowrap="nowrap" EditType="TextBox">1,550,703.52</td>
				<td nowrap="nowrap" EditType="TextBox">0</td>
				<td nowrap="nowrap" EditType="TextBox">33,253.36</td>
				<td nowrap="nowrap" EditType="TextBox">33,751.68</td>
				<td nowrap="nowrap" EditType="TextBox">-498.32</td>
				<td nowrap="nowrap" EditType="TextBox">359,825.44</td>
				<td nowrap="nowrap" EditType="TextBox">359,327.12</td>
				<td nowrap="nowrap" EditType="TextBox">498.32</td>
			</tr>	
			<tr>		
                <td nowrap="nowrap" EditType="TextBox">A3</td>				
				<td nowrap="nowrap" EditType="TextBox">1,550,703.52</td>
				<td nowrap="nowrap" EditType="TextBox">1,550,703.52</td>
				<td nowrap="nowrap" EditType="TextBox">0</td>
				<td nowrap="nowrap" EditType="TextBox">33,253.36</td>
				<td nowrap="nowrap" EditType="TextBox">33,751.68</td>
				<td nowrap="nowrap" EditType="TextBox">-498.32</td>
				<td nowrap="nowrap" EditType="TextBox">359,825.44</td>
				<td nowrap="nowrap" EditType="TextBox">359,327.12</td>
				<td nowrap="nowrap" EditType="TextBox">498.32</td>				
			</tr>
			
			<tr class="tabTh">
				<td rowspan="5" width="100" nowrap="nowrap">D大类大类</td>	
                <td nowrap="nowrap" EditType="TextBox">D1</td>				
				<td nowrap="nowrap" EditType="TextBox">1,550,703.52</td>
				<td nowrap="nowrap" EditType="TextBox">1,550,703.52</td>
				<td nowrap="nowrap" EditType="TextBox">0</td>
				<td nowrap="nowrap" EditType="TextBox">33,253.36</td>
				<td nowrap="nowrap" EditType="TextBox">33,751.68</td>
				<td nowrap="nowrap" EditType="TextBox">-498.32</td>
				<td nowrap="nowrap" EditType="TextBox">359,825.44</td>
				<td nowrap="nowrap" EditType="TextBox">359,327.12</td>
				<td nowrap="nowrap" EditType="TextBox">498.32</td>
			</tr>	
			<tr>	
			    <td nowrap="nowrap" EditType="TextBox">D2</td>	
				<td nowrap="nowrap" EditType="TextBox">1,550,703.52</td>
				<td nowrap="nowrap" EditType="TextBox">1,550,703.52</td>
				<td nowrap="nowrap" EditType="TextBox">0</td>
				<td nowrap="nowrap" EditType="TextBox">33,253.36</td>
				<td nowrap="nowrap" EditType="TextBox">33,751.68</td>
				<td nowrap="nowrap" EditType="TextBox">-498.32</td>
				<td nowrap="nowrap" EditType="TextBox">359,825.44</td>
				<td nowrap="nowrap" EditType="TextBox">359,327.12</td>
				<td nowrap="nowrap" EditType="TextBox">498.32</td>
			</tr>	
			<tr>		
                <td nowrap="nowrap" EditType="TextBox">D3</td>				
				<td nowrap="nowrap" EditType="TextBox">1,550,703.52</td>
				<td nowrap="nowrap" EditType="TextBox">1,550,703.52</td>
				<td nowrap="nowrap" EditType="TextBox">0</td>
				<td nowrap="nowrap" EditType="TextBox">33,253.36</td>
				<td nowrap="nowrap" EditType="TextBox">33,751.68</td>
				<td nowrap="nowrap" EditType="TextBox">-498.32</td>
				<td nowrap="nowrap" EditType="TextBox">359,825.44</td>
				<td nowrap="nowrap" EditType="TextBox">359,327.12</td>
				<td nowrap="nowrap" EditType="TextBox">498.32</td>				
			</tr>
			<tr>		
                <td nowrap="nowrap" EditType="TextBox">D4</td>				
				<td nowrap="nowrap" EditType="TextBox">1,550,703.52</td>
				<td nowrap="nowrap" EditType="TextBox">1,550,703.52</td>
				<td nowrap="nowrap" EditType="TextBox">0</td>
				<td nowrap="nowrap" EditType="TextBox">33,253.36</td>
				<td nowrap="nowrap" EditType="TextBox">33,751.68</td>
				<td nowrap="nowrap" EditType="TextBox">-498.32</td>
				<td nowrap="nowrap" EditType="TextBox">359,825.44</td>
				<td nowrap="nowrap" EditType="TextBox">359,327.12</td>
				<td nowrap="nowrap" EditType="TextBox">498.32</td>				
			</tr>
			<tr>		
                <td nowrap="nowrap" EditType="TextBox">D5</td>				
				<td nowrap="nowrap" EditType="TextBox">1,550,703.52</td>
				<td nowrap="nowrap" EditType="TextBox">1,550,703.52</td>
				<td nowrap="nowrap" EditType="TextBox">0</td>
				<td nowrap="nowrap" EditType="TextBox">33,253.36</td>
				<td nowrap="nowrap" EditType="TextBox">33,751.68</td>
				<td nowrap="nowrap" EditType="TextBox">-498.32</td>
				<td nowrap="nowrap" EditType="TextBox">359,825.44</td>
				<td nowrap="nowrap" EditType="TextBox">359,327.12</td>
				<td nowrap="nowrap" EditType="TextBox">498.32</td>			
			</tr>



			<tr>
			<td nowrap="nowrap">107T-车险(交强险)</td>
			<td nowrap="nowrap" EditType="TextBox">108,968,962.13</td>
			<td nowrap="nowrap" EditType="TextBox">108,968,962.13</td>
			<td nowrap="nowrap" EditType="TextBox">108,975,420.57</td>
			<td nowrap="nowrap" EditType="TextBox">-6,458.44</td>
			<td nowrap="nowrap" EditType="TextBox">59,672,975.8</td>
			<td nowrap="nowrap" EditType="TextBox">59,672,975.8</td>
			<td nowrap="nowrap" EditType="TextBox">0</td>
			<td nowrap="nowrap" EditType="TextBox">93,049,926.62</td>
			<td nowrap="nowrap" EditType="TextBox">93,048,446.62</td>
			<td nowrap="nowrap" EditType="TextBox">498.32</td>
			</tr>
			<tr>
			<td nowrap="nowrap">108T-车险(商业险)</td>
			<td nowrap="nowrap" EditType="TextBox">260,085,392.36</td>
			<td nowrap="nowrap" EditType="TextBox">108,968,962.13</td>
			<td nowrap="nowrap" EditType="TextBox">260,086,515.73</td>
			<td nowrap="nowrap" EditType="TextBox">-1,123.37</td>
			<td nowrap="nowrap" EditType="TextBox">131,815,686.81</td>
			<td nowrap="nowrap" EditType="TextBox">131,819,291.79</td>
			<td nowrap="nowrap" EditType="TextBox">-3,604.98</td>
			<td nowrap="nowrap" EditType="TextBox">211,276,060.23</td>
			<td nowrap="nowrap" EditType="TextBox">211,279,616.87</td>
			<td nowrap="nowrap" EditType="TextBox">498.32</td>
			</tr>
			<tr>
			<td nowrap="nowrap">109T-房屋险</td>
			<td nowrap="nowrap" EditType="TextBox">1,107,386.23</td>
			<td nowrap="nowrap" EditType="TextBox">108,968,962.13</td>
			<td nowrap="nowrap" EditType="TextBox">1,119,230.85</td>
			<td nowrap="nowrap" EditType="TextBox">-11,844.62</td>
			<td nowrap="nowrap" EditType="TextBox">369,424.34</td>
			<td nowrap="nowrap" EditType="TextBox">355,734.12</td>
			<td nowrap="nowrap" EditType="TextBox">13,690.22</td>
			<td nowrap="nowrap" EditType="TextBox">960,523.51</td>
			<td nowrap="nowrap" EditType="TextBox">968,921.8</td>
			<<td nowrap="nowrap" EditType="TextBox">498.32</td>
			</tr>
			<tr>
			<td nowrap="nowrap">110T-工程险</td>
			<td nowrap="nowrap" EditType="TextBox">52,470,322.9</td>
			<td nowrap="nowrap" EditType="TextBox">108,968,962.13</td>
			<td nowrap="nowrap" EditType="TextBox">52,507,149.28</td>
			<td nowrap="nowrap" EditType="TextBox">-36,826.38</td>
			<td nowrap="nowrap" EditType="TextBox">14,755,632.67</td>
			<td nowrap="nowrap" EditType="TextBox">14,755,852.67</td>
			<td nowrap="nowrap" EditType="TextBox">-220</td>
			<td nowrap="nowrap" EditType="TextBox">15,896,332.08</td>
			<td nowrap="nowrap" EditType="TextBox">16,042,977.52</td>
			<td nowrap="nowrap" EditType="TextBox">498.32</td>
			</tr>
			<tr class="tabTh">
				<td nowrap="nowrap">合计</td>		
				<td nowrap="nowrap" EditType="TextBox">108,968,962.13</td>				
				<td nowrap="nowrap" EditType="TextBox">753,959,667.45</td>
				<td nowrap="nowrap" EditType="TextBox">741,905,397.43</td>
				<td nowrap="nowrap" EditType="TextBox">12,054,270.02</td>
				<td nowrap="nowrap" EditType="TextBox">376,502,238.86</td>
				<td nowrap="nowrap" EditType="TextBox">388,700,315.28</td>
				<td nowrap="nowrap" EditType="TextBox">-12,198,076.42</td>
				<td nowrap="nowrap" EditType="TextBox">553,024,432.25</td>
				<td nowrap="nowrap" EditType="TextBox">554,140,700.32</td>
				<td nowrap="nowrap" EditType="TextBox">6,268.07</td>
			</tr>
		
		  </tbody>
		</table>
		<br />
		<input type="button" name="Submit" value="新增" οnclick="AddRow(document.getElementById('tabProduct'),1)" />
		<input type="button" name="Submit2" value="删除" οnclick="DeleteRow(document.getElementById('tabProduct'),1)" />
		<input type="button" name="Submit22" value="重置" οnclick="window.location.reload()" />
		<input type="submit" name="Submit3" value="提交" οnclick="GetTableData(document.getElementById('tabProduct'));return false;" />
	</form>
</div>

<script language="javascript">
//设置多个表格可编辑
function EditTables(){
	for(var i=0;i<arguments.length;i++){
	   SetTableCanEdit(arguments[i]);
	}
}

//设置表格是可编辑的
function SetTableCanEdit(table){
	for(var i=1; i<table.rows.length;i++){
	   SetRowCanEdit(table.rows[i]);
	}
}

function SetRowCanEdit(row){
	for(var j=0;j<row.cells.length; j++){
	
		//如果当前单元格指定了编辑类型,则表示允许编辑
		var editType = row.cells[j].getAttribute("EditType");
		if(!editType){
			//如果当前单元格没有指定,则查看当前列是否指定
			editType = row.parentNode.rows[0].cells[j].getAttribute("EditType");
		}
		if(editType){
			row.cells[j].onclick = function (){
				EditCell(this);
			}
		}
	}

}

//设置指定单元格可编辑
function EditCell(element, editType){

	var editType = element.getAttribute("EditType");
	if(!editType){
	   //如果当前单元格没有指定,则查看当前列是否指定
	   editType = element.parentNode.parentNode.rows[0].cells[element.cellIndex].getAttribute("EditType");
	}

	switch(editType){
		case "TextBox":
		CreateTextBox(element, element.innerHTML);
		break;
		case "DropDownList":
		CreateDropDownList(element);
		break;
		default:
		break;
	}
}

//为单元格创建可编辑输入框
function CreateTextBox(element, value){
	//检查编辑状态,如果已经是编辑状态,跳过
	var editState = element.getAttribute("EditState");
	if(editState != "true"){
		//创建文本框
		var textBox = document.createElement("INPUT");
		textBox.type = "text";
		textBox.className="EditCell_TextBox";
		
		
		//设置文本框当前值
		if(!value){
			value = element.getAttribute("Value");
		}  
		textBox.value = value;
		
		//设置文本框的失去焦点事件
		textBox.onblur = function (){
			CancelEditCell(this.parentNode, this.value);
		}
		//向当前单元格添加文本框
		ClearChild(element);
		element.appendChild(textBox);
		textBox.focus();
		textBox.select();
		
		//改变状态变量
		element.setAttribute("EditState", "true");
		element.parentNode.parentNode.setAttribute("CurrentRow", element.parentNode.rowIndex);
	}

}

//为单元格创建选择框
function CreateDropDownList(element, value){
	//检查编辑状态,如果已经是编辑状态,跳过
	var editState = element.getAttribute("EditState");
	if(editState != "true"){
		//创建下接框
		var downList = document.createElement("Select");
		downList.className="EditCell_DropDownList";
	
		//添加列表项
		var items = element.getAttribute("DataItems");
		if(!items){
			items = element.parentNode.parentNode.rows[0].cells[element.cellIndex].getAttribute("DataItems");
		}
	
		if(items){
			items = eval("[" + items + "]");
			for(var i=0; i<items.length; i++){
				var oOption = document.createElement("OPTION");
				oOption.text = items[i].text;
				oOption.value = items[i].value;
				downList.options.add(oOption);
			}
		}
	
		//设置列表当前值
		if(!value){
			value = element.getAttribute("Value");
		}
		downList.value = value;
	
		//设置创建下接框的失去焦点事件
		downList.onblur = function (){
			CancelEditCell(this.parentNode, this.value, this.options[this.selectedIndex].text);
		}
	
		//向当前单元格添加创建下接框
		ClearChild(element);
		element.appendChild(downList);
		downList.focus();
		
		//记录状态的改变
		element.setAttribute("EditState", "true");
		element.parentNode.parentNode.setAttribute("LastEditRow", element.parentNode.rowIndex);
	}

}


//取消单元格编辑状态
function CancelEditCell(element, value, text){
	element.setAttribute("Value", value);
	if(text){
		element.innerHTML = text;
	}else{
		element.innerHTML = value;
	}
	element.setAttribute("EditState", "false");

	//检查是否有公式计算
	CheckExpression(element.parentNode);
}

//清空指定对象的所有字节点
function ClearChild(element){
	element.innerHTML = "";
}

//添加行
function AddRow(table, index){
	var lastRow = table.rows[table.rows.length-1];
	var newRow = lastRow.cloneNode(true);
	table.tBodies[0].appendChild(newRow);
	SetRowCanEdit(newRow);
	return newRow;
}


//删除行
function DeleteRow(table, index){
	for(var i=table.rows.length - 1; i>0;i--){
		var chkOrder = table.rows[i].cells[0].firstChild;
		if(chkOrder){
			if(chkOrder.type = "CHECKBOX"){
				if(chkOrder.checked){
					//执行删除
					table.deleteRow(i);
				}
			}
		}
	}
}

//提取表格的值,JSON格式
function GetTableData(table){
	var tableData = new Array();
	alert("行数:" + table.rows.length);
	for(var i=1; i<table.rows.length;i++){
		tableData.push(GetRowData(tabProduct.rows[i]));
	}
	return tableData;
}

//提取指定行的数据,JSON格式
function GetRowData(row){
	var rowData = {};
	for(var j=0;j<row.cells.length; j++){
		name = row.parentNode.rows[0].cells[j].getAttribute("Name");
		if(name){
			var value = row.cells[j].getAttribute("Value");
			if(!value){
				value = row.cells[j].innerHTML;
			}
			rowData[name] = value;
		}
	}
	//alert("ProductName:" + rowData.ProductName);
	//或者这样:alert("ProductName:" + rowData["ProductName"]);
	return rowData;
}

//检查当前数据行中需要运行的字段
function CheckExpression(row){
	for(var j=0;j<row.cells.length; j++){
		expn = row.parentNode.rows[0].cells[j].getAttribute("Expression");
		//如指定了公式则要求计算
		if(expn){
			var result = Expression(row,expn);
			var format = row.parentNode.rows[0].cells[j].getAttribute("Format");
			if(format){
				//如指定了格式,进行字值格式化
				row.cells[j].innerHTML = formatNumber(Expression(row,expn), format);
			}else{
				row.cells[j].innerHTML = Expression(row,expn);
			}
		}
	}
}

//计算需要运算的字段
function Expression(row, expn){
	var rowData = GetRowData(row);
	//循环代值计算
	for(var j=0;j<row.cells.length; j++){
		name = row.parentNode.rows[0].cells[j].getAttribute("Name");
		if(name){
			var reg = new RegExp(name, "i");
			expn = expn.replace(reg, rowData[name].replace(/\,/g, ""));
		}
	}
	return eval(expn);
}

///
/** 
* 格式化数字显示方式   
* 用法 
* formatNumber(12345.999,'#,##0.00'); 
* formatNumber(12345.999,'#,##0.##'); 
* formatNumber(123,'000000'); 
* @param num 
* @param pattern 
*/ 
/* 以下是范例
formatNumber('','')=0
formatNumber(123456789012.129,null)=123456789012
formatNumber(null,null)=0
formatNumber(123456789012.129,'#,##0.00')=123,456,789,012.12
formatNumber(123456789012.129,'#,##0.##')=123,456,789,012.12
formatNumber(123456789012.129,'#0.00')=123,456,789,012.12
formatNumber(123456789012.129,'#0.##')=123,456,789,012.12
formatNumber(12.129,'0.00')=12.12
formatNumber(12.129,'0.##')=12.12
formatNumber(12,'00000')=00012
formatNumber(12,'#.##')=12
formatNumber(12,'#.00')=12.00
formatNumber(0,'#.##')=0
*/
function formatNumber(num,pattern){   
	var strarr = num?num.toString().split('.'):['0'];   
	var fmtarr = pattern?pattern.split('.'):[''];   
	var retstr='';   
	   
	// 整数部分   
	var str = strarr[0];   
	var fmt = fmtarr[0];   
	var i = str.length-1;     
	var comma = false;   
	for(var f=fmt.length-1;f>=0;f--){   
		switch(fmt.substr(f,1)){   
			case '#':   
			if(i>=0 ) retstr = str.substr(i--,1) + retstr;   
			break;   
			case '0':   
			if(i>=0) retstr = str.substr(i--,1) + retstr;   
			else retstr = '0' + retstr;   
			break;   
			case ',':   
			comma = true;   
			retstr=','+retstr;   
			break;   
		}   
	}   
	
	if(i>=0){   
		if(comma){   
			var l = str.length;   
			for(;i>=0;i--){   
				retstr = str.substr(i,1) + retstr;   
				if(i>0 && ((l-i)%3)==0) retstr = ',' + retstr;    
			}   
		}   
		else retstr = str.substr(0,i+1) + retstr;   
	}   
	   
	retstr = retstr+'.';   
	// 处理小数部分   
	str=strarr.length>1?strarr[1]:'';   
	fmt=fmtarr.length>1?fmtarr[1]:'';   
	i=0;   
	for(var f=0;f<fmt.length;f++){   
		switch(fmt.substr(f,1)){   
			case '#':   
			if(i<str.length) retstr+=str.substr(i++,1);   
			break;   
			case '0':   
			if(i<str.length) retstr+= str.substr(i++,1);   
			else retstr+='0';   
			break;   
		}   
	}
	return retstr.replace(/^,+/,'').replace(/\.$/,'');  
}  
</script>

<script type="text/javascript">
var tabProduct = document.getElementById("tabProduct");

// 设置表格可编辑
// 可一次设置多个,例如:EditTables(tb1,tb2,tb2,......)
EditTables(tabProduct);
</script>

</body>
</html>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值