懂视1
懂视101
懂视201
懂视301
懂视401
懂视501
懂视601
懂视701
懂视801
懂视901
懂视1001
懂视1101
懂视1201
懂视1301
懂视1401
懂视1501
懂视1601
懂视1701
懂视1801
懂视1901
文库1
文库101
文库201
文库301
文库401
文库501
文库601
文库701
文库801
文库901
文库1001
文库1101
文库1201
文库1301
文库1401
文库1501
文库1601
文库1701
文库1801
文库1901
比熊情感
全部频道
首页
科技
教育
生活
旅游
时尚
美容
美食
健康
体育
游戏
汽车
家电
您的当前位置:
首页
AJAX仿EXCEL表格功能_javascript技巧
AJAX仿EXCEL表格功能_javascript技巧
来源:比熊情感
仿Excel表格演示
//////////////////////////////////////////变量—初始化/////////////////////////////////////// var toBeColor = "#F8F9FC"; var backColor = "#FFFFFF"; var tableId = "tbData"; var table; var tbody; var divShowInput; window.onload=function(){ beginListen(); table = document.getElementById(tableId); tbody = table.getElementsByTagName("tbody")[0]; actionFill (); otherFill(); creatDiv(); divShowInput = document.getElementById("divShowInput"); } function creatDiv(){ var filldiv = document.createElement("div"); filldiv.setAttribute("id","divShowInput"); var barp = document.createElement("p"); barp.setAttribute("id","barTitle"); barp.onclick=hideDiv; var defComP = document.createElement("p"); defComP.setAttribute("id","defComP"); defComP.onclick=hideDiv; var cleara = document.createElement("a"); cleara.setAttribute("href","javascript:void 0"); cleara.onclick=clearInput; var clearatext = document.createTextNode("清空"); cleara.appendChild(clearatext); defComP.appendChild(cleara); var autoP = document.createElement("P"); autoP.setAttribute("id","autoFillP"); filldiv.appendChild(barp); filldiv.appendChild(defComP); filldiv.appendChild(autoP); document.body.appendChild(filldiv); } //////////////////////////////////////////变量—_初始化/////////////////////////////////////// //////////////////////////////////////////动作填充/////////////////////////////////////// function actionFill (){ var dbinputs = tbody.getElementsByTagName("input"); for (var i = 1;i<=dbinputs.length-1;i++){ dbinputs[i].onfocus=stopListen; dbinputs[i].onblur=beginListen; dbinputs[i].ondblclick=showDiv; dbinputs[i].onmouseover=onChangTrColor; dbinputs[i].onmouseout=outChangTrColor; dbinputs[i].onclick=readyedit; dbinputs[i].onkeydown=gonext; } } function otherFill (){ var Bt_selectAll = document.getElementById("Bt_selectAll"); Bt_selectAll.setAttribute("href","javascript:void 0"); Bt_selectAll.onclick=selectAll; var Bt_delSelect = document.getElementById("Bt_delSelect"); Bt_delSelect.setAttribute("href","javascript:void 0"); Bt_delSelect.onclick=delSelect; var Bt_copySelect = document.getElementById("Bt_copySelect"); Bt_copySelect.setAttribute("href","javascript:void 0"); Bt_copySelect.onclick=copySelect; var Bt_allclear = document.getElementById("Bt_allclear"); Bt_allclear.setAttribute("href","javascript:void 0"); Bt_allclear.onclick=allClear; var Bt_sendData = document.getElementById("Bt_sendData"); Bt_sendData.setAttribute("href","javascript:void 0"); Bt_sendData.onclick=sendData; } //////////////////////////////////////////动作填充/////////////////////////////////////// //////////////////////////////////////////ajax类/////////////////////////////////////// function Ajax(url,recvT,stringS,resultF) { this.url = url; this.stringS = stringS; this.xmlHttp = this.createXMLHttpRequest(); this.resultF = resultF; this.recvT = recvT; if (this.xmlHttp == null) { alert("erro"); return; } var objxml = this.xmlHttp; var othis = this; objxml.onreadystatechange = function (){othis.handleStateChange()}; } Ajax.prototype = { createXMLHttpRequest:function() { try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch(e) {} try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch(e) {} try { return new XMLHttpRequest(); } catch(e) {} return null; }, createQueryString:function () { var queryString = this.stringS; return queryString; }, get : function () { url = this.url; var queryString = url+"?timeStamp=" + new Date().getTime() + "&" + this.createQueryString(); this.xmlHttp.open("GET",queryString,true); this.xmlHttp.send(null); }, post : function() { url = this.url; var url = url + "?timeStamp=" + new Date().getTime(); var queryString = this.createQueryString(); this.xmlHttp.open("POST",url,true); this.xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded"); this.xmlHttp.send(queryString); }, handleStateChange : function () { var xmlhttp = this.xmlHttp; var recvT = this.recvT; var resultF = this.resultF; if (xmlhttp.readyState == 4) { if (xmlhttp.status == 200) { resultF.call(this,recvT?xmlhttp.responseXML:xmlhttp.responseText); } else { alert("您所请求的页面有异常。"); } } } } //////////////////////////////////////////ajax类/////////////////////////////////////// //////////////////////////////////////////处理鼠标事件/////////////////////////////////////// //表格变色 function onChangTrColor(event) { var e = event || window.event; var obj = e.target || e.srcElement; obj.parentNode.style.backgroundColor = toBeColor; obj.style.backgroundColor = toBeColor; var inputs = obj.parentNode.parentNode.getElementsByTagName("input"); for (var i = 0; i < inputs.length; i++ ){ inputs[i].style.backgroundColor = toBeColor; inputs[i].parentNode.style.backgroundColor = toBeColor; } } function outChangTrColor(event) { var e = event || window.event; var obj = e.target || e.srcElement; obj.parentNode.style.backgroundColor = backColor; obj.style.backgroundColor = backColor; var inputs = obj.parentNode.parentNode.getElementsByTagName("input"); for (var i = 0; i < inputs.length; i++ ){ inputs[i].style.backgroundColor = backColor; inputs[i].parentNode.style.backgroundColor = backColor; } } //////////////////////////////////////////处理鼠标事件/////////////////////////////////////// //////////////////////////////////////////处理页面操作/////////////////////////////////////// //复制所选 function copySelect(){ var checkboxs = document.getElementsByName("checkbox"); for (var i=0; i
0) { var beliveM = true; for (var j = 0; j < menus.length; j++ ){ if(menus[j].firstChild.nodeValue == inputValue) { beliveM = false; } } if(beliveM){ var Svalue = document.createElement("a"); Svalue.setAttribute("href","javascript:void 0"); Svalue.onclick = function () {fillInput(this)}; var Stext = document.createTextNode(inputValue); Svalue.appendChild(Stext); autoFillP.appendChild(Svalue); } }else{ var Svalue = document.createElement("a"); Svalue.setAttribute("href","javascript:void 0"); Svalue.onclick = function () {fillInput(this)}; var Stext = document.createTextNode(inputValue); Svalue.appendChild(Stext); autoFillP.appendChild(Svalue); } } } if(bankM) { var Svalue = document.createElement("a"); Svalue.setAttribute("href","javascript:void 0"); var Stext = document.createTextNode("数据空"); Svalue.appendChild(Stext); autoFillP.appendChild(Svalue); } } function fillInput(obj) { currentObj.value = obj.innerHTML; divShowInput.style.display = "none"; } function clearInput() { currentObj.value = ""; divShowInput.style.display = "none"; } function hideDiv() { divShowInput.style.display = "none"; stophide(); } function hideListen(){ if(document.addEventListener){ document.addEventListener("click",hideDiv,true); }else{ document.attachEvent("onclick",hideDiv); } } function stophide(){ if(document.removeEventListener){ document.removeEventListener("click",keyDown,true); }else{ document.detachEvent("onclick",keyDown); } } //删除智能菜单已有数据 function fixMenu() { var autoFillP = document.getElementById("autoFillP"); var values = autoFillP.getElementsByTagName("a"); for (var i = values.length-1; i >= 0; i-- ){ autoFillP.removeChild(values[i]); } } //////////////////////////////////////////处理按钮事件/////////////////////////////////////// //////////////////////////////////////////数据发送/////////////////////////////////////// //////////////////////////////////////////数据发送/////////////////////////////////////// function sendData() { var divFoltupDiv = document.getElementById("divFoltupDiv"); divFoltupDiv.style.display = "block"; var sendokinf = document.getElementById("sendokinf"); var sendokBiginf = document.getElementById("sendokBiginf"); var sendName = new Array(); var trs = tbody.getElementsByTagName("tr"); var inputNames = trs[0].getElementsByTagName("input"); for (var i = 0; i < inputNames.length-1; i++) { sendName[i]=inputNames[i+1].getAttribute("name"); } var trnum = trs.length; var oknum = 0; for (var i = 0; i < trnum; i++) { var values = trs[i].getElementsByTagName("input"); var postStringAry = new Array(); for (var j = 0; j < values.length-1; j++) { postStringAry[j]=sendName[j]+"="+values[j+1].value; } var postString = postStringAry.join("&"); function sendOk(revData){ if(revData=="ok"){ oknum++; sendokinf.innerHTML = "已成功发送 "+oknum+" 行 共"+trnum+" 行"; sendokBiginf.innerHTML = trnum-oknum; if (trnum-oknum==0){ divFoltupDiv.style.display = "none"; allClear(); allDel(); } } } var SendAjax = new Ajax("isave.asp",0,postString,sendOk); SendAjax.post(); } } //////////////////////////////////////////数据发送/////////////////////////////////////// //-->
发送数据
全选
A1
A2
A3
A4
A5
A6
A7
A8
A9
A10
A11
复制所选
删除所选
清空所有
提交
操作方法 一 键盘操作 1.Insert键增加一行,并复制最后一行内容,Delete键删除最后一行,该操作用以调节表格总大小; 2.Tab键横向移动光标,Enter键纵向移动光标,符合Excle用户习惯; 二 页面操作 1.全选:选择全部行,再次执行释放全部行; 2.复制所选:复制已经选择的行及其内容; 3.删除所选:删除已经选择的行; 4.清空所选:清除所有单元格(input)中的内容; 5.提交:发送数据内容到服务器 三 智能操作 双击单元格将弹出菜单,菜单自动收集该列上所有不重复的已输入内容,为重复输入数据提供了方便。
[Ctrl+A 全选 注:如需引入外部Js需刷新才能执行]
显示全文