首頁 >web前端 >js教程 >智能表格_javascript技巧

智能表格_javascript技巧

WBOY
WBOY原創
2016-05-16 19:21:471215瀏覽

作者 llinzzi
版本 0.9
说明:
当焦点不在表格内的input时,回车键复制最后一行,delete删除键最后一行
选择checkbox,可以进行复制,删除
双击表格会出现菜单,自动收集该列已存在数据,选中自动填充  这里是亮点
数据发送采用ajax(自定义的一个ajax类,blog已发布过)一行一行的发送
兼容IE6和Firefox1.5 符合W3C
本表格一切功能都是为了减少输入录入工作,适合大项目开放使用

复制代码 代码如下:

nbsp;HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">




无标题文档
<script> <BR><!-- <BR>//////////////////////////////////////////页面初始化/////////////////////////////////////// <BR>beginListen(); <BR>//////////////////////////////////////////页面初始化/////////////////////////////////////// <BR>//////////////////////////////////////////ajax类/////////////////////////////////////// <BR>function Ajax(url,recvT,stringS,resultF) { <BR> this.url = url; <BR> this.stringS = stringS; <BR> this.xmlHttp = this.createXMLHttpRequest(); <BR> if (this.xmlHttp == null) { <BR> alert("erro"); <BR> return; <BR> } <BR> var objxml = this.xmlHttp; <BR> objxml.onreadystatechange = function (){Ajax.handleStateChange(objxml,recvT,resultF)}; <BR>} <br><br>Ajax.prototype.createXMLHttpRequest = function() { <BR> try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch(e) {} <BR> try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch(e) {} <BR> try { return new XMLHttpRequest(); } catch(e) {} <BR> return null; <BR>} <br><br>Ajax.prototype.createQueryString = function () { <BR> var queryString = this.stringS; <BR> return queryString; <BR>} <br><br>Ajax.prototype.get = function () { <BR> url = this.url; <BR> var queryString = url+"?timeStamp=" + new Date().getTime() + "&" + this.createQueryString(); <BR> this.xmlHttp.open("GET",queryString,true); <BR> this.xmlHttp.send(null); <BR>} <br><br>Ajax.prototype.post = function() { <BR> url = this.url; <BR> var url = url + "?timeStamp=" + new Date().getTime(); <BR> var queryString = this.createQueryString(); <BR> this.xmlHttp.open("POST",url,true); <BR> this.xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded"); <BR> this.xmlHttp.send(queryString); <BR>} <br><br>Ajax.handleStateChange = function (xmlHttp,recvT,resultF) { <BR> if (xmlHttp.readyState == 4) { <BR> if (xmlHttp.status == 200) { <BR> resultF(recvT?xmlHttp.responseXML:xmlHttp.responseText); <BR> } else { <BR> alert("您所请求的页面有异常。"); <BR> } <BR> } <BR>} <BR>//////////////////////////////////////////ajax类/////////////////////////////////////// <br><br>//////////////////////////////////////////处理鼠标事件/////////////////////////////////////// <BR>//表格变色 <BR>var toBeColor = "#F8F9FC"; <BR>var backColor = "#FFFFFF"; <BR>function onChangTrColor(obj) { <BR> obj.parentNode.style.backgroundColor = toBeColor; <BR> obj.style.backgroundColor = toBeColor; <BR> var inputs = obj.parentNode.parentNode.getElementsByTagName("input"); <BR> for (var i = 0; i < inputs.length; i++ ){ <BR> inputs[i].style.backgroundColor = toBeColor; <BR> inputs[i].parentNode.style.backgroundColor = toBeColor; <BR> } <BR>} <br><br>function outChangTrColor(obj) { <BR> obj.parentNode.style.backgroundColor = backColor; <BR> obj.style.backgroundColor = backColor; <BR> var inputs = obj.parentNode.parentNode.getElementsByTagName("input"); <BR> for (var i = 0; i < inputs.length; i++ ){ <BR> inputs[i].style.backgroundColor = backColor; <BR> inputs[i].parentNode.style.backgroundColor = backColor; <BR> } <BR>} <br><br>//////////////////////////////////////////处理鼠标事件/////////////////////////////////////// <br><br>//////////////////////////////////////////处理页面操作/////////////////////////////////////// <BR>//复制所选 <BR>function copySelect(){ <BR> var checkboxs = document.getElementsByName("checkbox"); <BR> for (var i=0; i<checkboxs.length; i++) { <BR> if(checkboxs[i].checked == true){ <BR> checkboxs[i].checked = false; <BR> copyTr(checkboxs[i]); <BR> checkboxs[i].checked = true; <BR> } <BR> } <BR>} <br><br>function copyTr(obj) { <BR> var tbody = document.getElementById("tbData").getElementsByTagName("tbody")[0]; <BR> var Str = obj.parentNode.parentNode; <BR> var tr = Str.cloneNode(true); <BR> tbody.appendChild(tr); <BR>} <br><br>//删除所选 <BR>function delSelect(){ <BR> var checkboxs = document.getElementsByName("checkbox"); <BR> var table = document.getElementById("tbData"); <BR> var tr = table.getElementsByTagName("tr"); <BR> for (var i=0; i<checkboxs.length; i++) { <BR> if(tr.length==2){ <BR> checkboxs[i].checked = false; <BR> return; <BR> } <BR> if(checkboxs[i].checked==true){ <BR> removeTr(checkboxs[i]); <BR> i=-1; <BR> } <BR> } <BR>} <br><br>function removeTr(obj) { <BR> var sTr = obj.parentNode.parentNode; <BR> sTr.parentNode.removeChild(sTr); <BR>} <br><br>//全选按钮 <BR>function selectAll() { <BR> var checkboxs = document.getElementsByName("checkbox"); <BR> var mark = true; <BR> for (var i=0; i<checkboxs.length; i++) { <BR> if (checkboxs[i].checked==false){mark = false} <BR> } <BR> if (mark){ <BR> for (var i=0; i<checkboxs.length; i++) { <BR> checkboxs[i].checked = false; <BR> } <BR> }else{ <BR> for (var i=0; i<checkboxs.length; i++) { <BR> checkboxs[i].checked = true; <BR> } <BR> } <BR>} <br><br>//////////////////////////////////////////处理页面操作/////////////////////////////////////// <br><br>//////////////////////////////////////////处理键盘操作/////////////////////////////////////// <BR>//键盘事件 <BR>function beginListen(){ <BR> if(document.addEventListener){ <BR> document.addEventListener("keydown",keyDown,true); <BR> }else{ <BR> document.attachEvent("onkeydown",keyDown); <BR> } <BR>} <BR>function stopListen(){ <BR> document.detachEvent("onkeydown",keyDown); <BR>} <br><br>//处理键盘事件 <BR>function keyDown(event){ <BR> if(event.keyCode==13){addTr()} <BR> if(event.keyCode==46){delTr()} <BR>} <br><br>//增加表格 <BR>function addTr() { <BR> var tbody = document.getElementById("tbData").getElementsByTagName("tbody")[0]; <BR> var sTr = tbody.getElementsByTagName("tr")[0]; <BR> var tr = sTr.cloneNode(true); <BR> tbody.appendChild(tr); <BR>} <br><br>//增加表格 <BR>function addTr() { <BR> var tbody = document.getElementById("tbData").getElementsByTagName("tbody")[0]; <BR> var trs = tbody.getElementsByTagName("tr"); <BR> var sTr = trs[trs.length-1]; <BR> var tr = sTr.cloneNode(true); <BR> tbody.appendChild(tr); <BR>} <br><br>//删除表格 <BR>function delTr() { <BR> var table = document.getElementById("tbData"); <BR> var tr = table.getElementsByTagName("tr"); <BR> if(tr.length==2){return;} <BR> var lastTr = tr[tr.length-1]; <BR> lastTr.parentNode.removeChild(lastTr); <BR>} <br><br><BR>//////////////////////////////////////////处理键盘操作/////////////////////////////////////// <br><br>//////////////////////////////////////////处理按钮事件/////////////////////////////////////// <BR>//自动填充 <BR>var currentObj; <BR>function showDiv(event,obj) { <BR> var eX = event.clientX; <BR> var eY = event.clientY; <BR> var sY = document.body.parentNode.scrollTop; <BR> var dY = eY + sY; <BR> var divShowInput = document.getElementById("divShowInput"); <BR> divShowInput.style.top = dY + "px"; <BR> divShowInput.style.left = eX+"px"; <BR> divShowInput.style.display = "block"; <BR> currentObj = obj; <BR> ////智能菜单//// <BR> fixMenu(); <BR> //判断焦点位置 <BR> var tds = obj.parentNode.parentNode.getElementsByTagName("td"); <BR> var tdOrder; <BR> for (var i = 0; i < tds.length; i++ ){ <BR> if(tds[i] === obj.parentNode){ <BR> tdOrder = i; <BR> } <BR> } <BR> //填充标题标题 <BR> var tdTitleTr = document.getElementById("tbData").getElementsByTagName("tr")[0]; <BR> var tdTitle = tdTitleTr.getElementsByTagName("td")[tdOrder]; <BR> document.getElementById("barTitle").innerHTML = tdTitle.innerHTML; <BR> //收集表格信息//判断重复 <BR> var trs = obj.parentNode.parentNode.parentNode.getElementsByTagName("tr"); <BR> var autoFillP = document.getElementById("autoFillP"); <BR> var bankM = true; <BR> for (var i = 0; i < trs.length; i++ ){ <BR> var inputValue = trs[i].getElementsByTagName("td")[tdOrder].getElementsByTagName("input")[0].value; <BR> if (inputValue != "") { <BR> bankM = false; <BR> var menus = autoFillP.getElementsByTagName("a"); <BR> if(menus.length > 0) { <BR> var beliveM = true; <BR> for (var j = 0; j < menus.length; j++ ){ <BR> if(menus[j].firstChild.nodeValue == inputValue) { <BR> beliveM = false; <BR> } <BR> } <BR> if(beliveM){ <BR> var Svalue = document.createElement("a"); <BR> Svalue.setAttribute("href","javascript:void 0"); <BR> Svalue.onclick = function () {fillInput(this)}; <BR> var Stext = document.createTextNode(inputValue); <BR> Svalue.appendChild(Stext); <BR> autoFillP.appendChild(Svalue); <BR> } <BR> }else{ <BR> var Svalue = document.createElement("a"); <BR> Svalue.setAttribute("href","javascript:void 0"); <BR> Svalue.onclick = function () {fillInput(this)}; <BR> var Stext = document.createTextNode(inputValue); <BR> Svalue.appendChild(Stext); <BR> autoFillP.appendChild(Svalue); <BR> } <BR> } <BR> } <BR> if(bankM) { <BR> var Svalue = document.createElement("a"); <BR> Svalue.setAttribute("href","javascript:void 0"); <BR> var Stext = document.createTextNode("数据空"); <BR> Svalue.appendChild(Stext); <BR> autoFillP.appendChild(Svalue); <BR> } <BR>} <br><br>function fillInput(obj) { <BR> currentObj.value = obj.innerHTML; <BR> var divShowInput = document.getElementById("divShowInput"); <BR> divShowInput.style.display = "none"; <BR>} <br><br>function clearInput() { <BR> currentObj.value = ""; <BR> var divShowInput = document.getElementById("divShowInput"); <BR> divShowInput.style.display = "none"; <BR>} <br><br>function hideDiv(obj) { <BR> obj.parentNode.style.display = "none"; <BR>} <br><br>//删除智能菜单已有数据 <BR>function fixMenu() { <BR> var autoFillP = document.getElementById("autoFillP"); <BR> var values = autoFillP.getElementsByTagName("a"); <BR> for (var i = values.length-1; i >= 0; i-- ){ <BR> autoFillP.removeChild(values[i]); <BR> } <BR>} <BR>//////////////////////////////////////////处理按钮事件/////////////////////////////////////// <br><br>//////////////////////////////////////////数据发送/////////////////////////////////////// <br><br>//////////////////////////////////////////数据发送/////////////////////////////////////// <BR>function sendData() { <BR> var sendName = new Array("ok","A1","A2","A3","A4","A5","A6","A7","A8","A9","A10","A11"); <BR> var trs = document.getElementById("tbData").getElementsByTagName("tbody")[0].getElementsByTagName("tr"); <BR> for (var i = 0; i < trs.length; i++) { <BR> var sendValue = new Array(); <BR> var values = trs[i].getElementsByTagName("input"); <BR> var postString = sendName[1] + "=" + values[1].value;; <BR> for (var j = 2; j < values.length; j++) { <BR> postString = postString + "&" + sendName[j] + "=" + values[j].value; <BR> } <BR> var SendAjax = new Ajax("isave.asp",0,postString,sendok); <BR> SendAjax.post(); <BR> function sendok(revData){ <BR> alert(revData); <BR> } <BR> } <BR>} <br><br><br><br>//////////////////////////////////////////数据发送/////////////////////////////////////// <br><br><BR>//--> <BR></script>





    


    


    清空
    


    


    Llinzzi
    Huanhuan
    Tom.com
    超级长的长长长长长长
    




  
    
  
  
    
  
  
    
  

      
        
        
        
      
    
复制所选 删除所选  

    
      
        
        
        
        
        
        
        
        
        
        
        
        
      
      
      
      
        
        
        
        
        
        
        
        
        
        
        
        
      
      
    
全选 A1 A2 A3 A4 A5 A6 A7 A8 A9 A10 A11

      
        
        
        
        
      
    
复制所选 删除所选 提交  





[Ctrl+A 全选 注:如需引入外部Js需刷新才能执行]
陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn