//动态添加一行
function addRow(){
var firstrow=document.getElementById('firstrow');
var firstCopy=firstrow.cloneNode(true);
firstrow.parentNode.insertAdjacentElement("beforeEnd",firstCopy);
}
//获取表格每行的信息,添加到数组对象中
function rowInfo(){
var atable=document.getElementById('atable');
var trs=atable.getElementsByClassName('row');
var msgList=[];
for(var i=0;i<trs.length;i++){
var msg={"a":"","b":"","c":"","d":""};
var tds=trs[i].getElementsByTagName('td');
msg.a=tds[0].innerText;
msg.b=tds[1].innerText;
msg.c=tds[2].innerText;
msg.d=tds[3].innerText;
msgList.push(msg);
}
//console.log(msgList);
}
//获取表格某列信息
function oneColInfo(index){
var atable=document.getElementById('atable');
var trs=atable.getElementsByClassName('row');
var colList=[];
for(var i=0;i<trs.length;i++){
var cols=trs[i].getElementsByTagName('td');
colList.push(cols[index].innerText);
}
//console.log(colList);
return colList;
}
//查询表格内容
function findInfo(){
var atable=document.getElementById('atable');
var trs=atable.getElementsByClassName('row');
var number=document.getElementById('number');
var city=document.getElementById('city');
var name=document.getElementById('name');
//console.log(number.value);
var colNumber=oneColInfo(1);//获取表格中所有联行号
var colCity=oneColInfo(3);
var colName=oneColInfo(4);
//console.log(typeof colNumber[0]);
var result=[];
if(number.value!=""){
for(var i=0;i<colNumber.length;i++){
// console.log(typeof number.value);
// console.log(typeof colNumber[3]);
if(number.value==colNumber[i]){
result.push(i);
}
}
console.log(result);
}
if(city.value!=""){
for(var i=0;i<colCity.length;i++){
// console.log(city.value);
// console.log(colCity[i]);
if(city.value==colCity[i]){
result.push(i);
}
}
console.log(result);
}
if(name.value!=""){
for(var i=0;i<colName.length;i++){
if(colName[i].indexOf(name.value)!=-1){
result.push(i);
}
}
console.log(result);
}
console.log(result);
var resultNum=numinArrayCount(result);
var result2=[];
for(var i=0;i<result.length;i++){
if(resultNum[result[i]]>1){
result2.push(result[i]);
console.log(result2);
}
}
if(result.length>0&&result2.length==0){
delRow(result);
}
if(result2.length>0){
delRow(result2);
}
}
//删除所有,保留某几行
function delRow(arr){
var atable=document.getElementById('atable');
var trs=atable.getElementsByClassName('row');
var orow=[];
for(var k=0;k<arr.length;k++){
orow.push(trs[arr[k]]);
}
console.log(orow);
for(var i=1,len=trs.length+1;i<len;i++){//删除全部
atable.deleteRow(1);
}
for(var i=0;i<orow.length;i++){
atable.appendChild(orow[i]);
}
}