var divObj = document.getElementById("tbTest");
var tbObj = document.createElement("table");
var tableObj= new tableObj();
var tbRow;
var tbTd;
var numRows = 0;
var num = 0;
var index = 0;
var currentCol;//table对象
function tableObj(){
var t_this = this;
t_this.rows;
t_this.clos;
t_this.thColor;
t_this.rowsColor;
t_this.checkBoxId; t_this.setCheckBoxId = function(id){
t_this.checkBoxId = id;
}; t_this.getCheckBoxId = function(){
return t_this.checkBoxId;
} t_this.setRows = function(rows){
t_this.rows = rows;
}; t_this.getRows = function(){
return t_this.rows;
}; t_this.setClos = function(clos){
t_this.clos = clos;
}; t_this.getClos = function(){
return t_this.clos;
}; t_this.setRowsColor = function(rowsColor){
t_this.rowsColor = rowsColor;
}; t_this.getRowsColor = function(){
return t_this.rowsColor;
}; t_this.setThColor = function(thColor){
t_this.thColor = thColor;
}; t_this.getThColor = function(){
return t_this.thColor;
}; //显示table
t_this.showTable = function(rows,clos){
divObj = document.getElementById("tbTest");
tbObj=document.createElement("table");
tbObj.cellSpacing="0";
tbObj.id="myTable";
tbRow=tbObj.insertRow();
tbRow.id=tbRow.rowIndex;
tbCheck = document.createElement("<input type='checkBox' id="+(tbRow.rowIndex)+"/>");
tbTd=tbRow.insertCell();
tbCheck.onclick = check;
tbTd.appendChild(tbCheck);

for(var m = 0; m < clos; m++){
tbTd=tbRow.insertCell();
tbTd.innerHTML = "我是第"+(m+1)+"个标题";
} for(var i = 0; i < rows; i++){
tbRow=tbObj.insertRow();
tbRow.id=tbRow.rowIndex;
tbtd = tbRow.insertCell(); tbCheck = document.createElement("<input type='checkBox' id="+(tbRow.rowIndex)+"/>");
tbtd.appendChild(tbCheck); tbCheck.onclick =check; for(var j = 0;j<clos;j++){
tbTd=tbRow.insertCell();
tdInput = document.createElement("input");
tdInput.type = "text";
tdInput.value = "TABLE"+i+j;
tdInput.readOnly = "readonly";
tbTd.appendChild(tdInput);
}
}
divObj.appendChild(tbObj);
};
//添加一行
var newRows=null;
t_this.newRow= function(rows,clos){
//每次都添加到最后面
newRows=document.getElementById("myTable").insertRow(document.getElementById("myTable").rows.length);
newRows.id =newRows.rowIndex;
tbCheck = document.createElement("<input type='checkBox' id="+(newRows.rowIndex)+"/>");
tbCheck.onclick=check;
ta= newRows.insertCell();
ta.appendChild(tbCheck);
for(var o = 0;o<clos;o++){
ta= newRows.insertCell();
var tdInput = document.createElement("input");
tdInput.type = "text";
tdInput.value = "NEW";
tdInput.readOnly = "readonly";
ta.appendChild(tdInput);
}
};
//修改数据
t_this.updateInfo = function(index){
//得到点击的输入框
var trElem = document.getElementById(t_this.getCheckBoxId()).parentNode.parentNode;
for(var i = 0; i < trElem.cells.length; i++){
trElem.cells(i+1).children.item(0).readOnly = false;
trElem.cells(i+1).children.item(0).style.background = "rgb(204,221,240)";
}
};
//删除行
t_this.deleteRows = function(){
var rowAll = document.getElementById("myTable").rows;
var checkBoxAll = new Array();
var arr = new Array();
for(var j = 0; j < rowAll.length; j ++){
checkBoxAll[j] = rowAll[j].cells(0).children.item(0);
}
for(var k = 0; k < checkBoxAll.length; k ++){
if(checkBoxAll[k].checked == true && (parseInt(checkBoxAll[k].id)!=0)){
arr.push(checkBoxAll[k].id);
}
}
for(var i = 0; i < arr.length; i++){
var tdIndex = document.getElementById(arr[i]).parentNode.parentNode.rowIndex;
document.getElementById("myTable").deleteRow(tdIndex);

}
rowAll[0].cells(0).children.item(0).checked = false;
arr = {};
arr = new Array();
};
function check(){
if(parseInt(this.id) == 0){
var rowAll = document.getElementById("myTable").rows;
var checkBoxAll = new Array();
for(var j = 1; j < rowAll.length; j ++){
rowAll[j].cells(0).children.item(0).checked = true;
}
}
t_this.setCheckBoxId(this.id);
index = document.getElementById(t_this.getCheckBoxId()).parentNode.parentNode.rowIndex;
}; t_this.saveInfo = function(){

var trElem = document.getElementById(t_this.getCheckBoxId()).parentNode.parentNode;
for(var i = 0; i < trElem.cells.length - 1; i++){
trElem.cells(i+1).children.item(0).readOnly = true;
trElem.cells(i+1).children.item(0).style.background = "transparent";
}
document.getElementById(t_this.getCheckBoxId()).checked = false;
};
}//显示
function createTable(rows,clos){
tableObj.showTable(rows,clos);  
}
//添加一行
function newsRow(rows,clos){
tableObj.newRow(rows,clos);  
}
//删除一行
function delRows(){
tableObj.deleteRows();
}
//修改一行
function modify(){
tableObj.updateInfo(index);  
}

function save(){
tableObj.saveInfo();
}

/*function sortTable(colNo,type){
var tbElem = document.getElementById("myTable"); for(var i = 0; i < tbElem.rows.length; i++){
arr[i] = tbElem.rows[i+1];
}
if(currentCol != colNo){
arr.sort(compareTr(colNo,type));
}else{
arr.reversr();
}
currentCol = colNo;
}*/function compareTr(colNo,type){
return compare(tr1,tr2){
var value1 = transfer(tr1.cells[colNo].children.item(0).value,type);
var value2 = transfer(tr2.cells[colNo].children.item(0).value,type); if(value1 < value2){
return -1;
}else if(value1 > value2){
return 1;
}else{
return 0;
}
}
}function transfer(value,type){
switch(type){
case "int":
return parseInt(value);
break;
case "date":
return Date.parse(value);
break;
default:
return value.toString(value);
break;
}
}