JavaScript實現線上編輯表格

來源:互聯網
上載者:User
http://dev.csdn.net/develop/article/13/13947.shtm

 

======JavaScript線上表格示範======

中國美國英國

*****=====下列按鈕根據需要選定使用======*****

 

 

 

=====以下為代碼清單=====

======OnlineEditTable.html======

<html>
<head>
<!--
 * Title:  Table to edit online
 * Description: Use to table to input data to a form
 * Copyright:  Copyright (c) 2003
 * Company:  weide
 * @author  weidegong(weidegong@yahoo.com.cn)
 * @version  3.2
 * Function  動態可編輯表格構成的表單,其中輸入欄位及其輸入形式可根據實際需要自行設定(參考本例Select),IE6測試通過
   將script和style定義放在頁面head區;將如<table></table>地區拷貝至需要使用該表格的位置即可;最下方為參考按鈕
   為了能在同一Form中使用多個表格,故將<table>放在form中,而不是將<TBODY>放在form中,這樣導致提交的輸入欄位的第一個值無效,如在JSP中:String strxs[]=request.getParameterValues("x");//則strxs[0]即為無效資料

   thead.tr.th:定義表頭,可以設定相應的列寬;th的數目需要同下面的td數目相同,以一一對應;其中"X"是固定列
   thead.tr.td.<input ... name=x>設定相應資料列對應的輸入欄位的名字;其中第一列用於顯示一個選擇框
   tableData,預設<TR>的樣式單名稱;tableDataHit,高亮度顯示時<TR>採用的樣式單
   inputTableData,顯示資料時表格中input輸入欄位使用的樣式單;inputTableDataHit,TR高亮度顯示時,input使用的樣式單
 * @History  1.0 從網上下載了例子,並略作修改
   2.0 使用CSS表單美化介面;增加一個類比可編輯Select下拉框
   3.0 將addRow,deleteRow修改為帶有物件類型參數的函數,使得在同一介面可使用可編輯表格的多個執行個體
   3.1 將addRow,deleteRow函數的參數修改為表格對象,更加方便;整理出onlineEditTable.css、onlineEditTable.js
   3.2 (2003-03-27)增加鍵盤導航功能,使用方向鍵或斷行符號鍵均可
-->

<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<LINK href="onlineEditTable.css" type=text/css rel=stylesheet>
<script language="javascript" src="onlineEditTable.js"></script>
</head>
<body>

<select id=qswh size=6  style="position:absolute;display:none" onblur="this.style.display='none'" onchange="setInputFromSelect(inputFocus,this)">
<option value="1">中國</option>
<option value="2">美國</option>
<option value="3">英國</option>
</select>

<form name=frmTableOnlineEdit action="showPara.htm">
<TABLE bgColor=silver border=2 borderColorDark=gray borderColorLight=silver
cellPadding=2 cellSpacing=1 cols=1 id=tableOnlineEdit rules=rows width="100%" onKeyDown="navigateKeys()" onKeyUp="setKeyDown(false)">
<thead>

<tr bgColor=#0a6846>
 <th class=thData width=1%><input type=checkbox id=checkLineAll onclick=setOnlineEditCheckBox(getUpperObj(this,"TABLE"),this.checked)></th><!--Fixed-->
 <th class=thData width=30%>標題一</th>
 <th class=thData width=30%>標題二</th>
 <th class=thData width=39%>標題三</th>
 <!--<th....................標題N...可定義多個-->
</tr>

<TR style="display:none" bgColor=#e0e0e0 class=tableData onclick=lightonRow(this)>
 <TD class=scheduleButtonVisible ><input type=checkbox id=checkLine></td><!--Fixed-->
 <TD><input class=inputTableData name=x onclick=showInputSelect(this,qswh) onblur=hideInput(qswh)></TD>
 <TD><input class=inputTableData name=y></TD>
 <TD><input class=inputTableData name=z></TD>
 <!--<TD><input...........name=...></TD>....可定義多個,與上面的<th>數目保持一致...-->
</TR>

</thead>

<tbody>
</tbody>

</TABLE>

</form>
<p align=center><font size="1">註:第一列單擊彈出下拉框選擇;在表格最後一行,按下“下箭頭”可以增加一個新的空行;使用斷行符號鍵可以向下遍曆整個表格</p>
<p align=center><font size="2">*****=====下列按鈕根據需要選定使用======*****</font></p>

<p align=center>
<input type=button onclick="addRow(tableOnlineEdit)" value="添加空資料行">
<input type=button onclick="addInstanceRow(tableOnlineEdit,['x','y','z'],['1','100','這是一個測試的例子'])" value="添加執行個體資料行">
<input type=button onclick="deleteRow(tableOnlineEdit)" value="刪除">
<input type=button onclick="clearRow(tableOnlineEdit)" value="刪除全部">
<input type=button onclick="frmTableOnlineEdit.submit()" value="submit Form">
<input type=button onclick="frmTableOnlineEdit.reset()" value="Reset">
</p>

 

 

 

============================另一個執行個體=================================
<form action="showPara.htm">
<TABLE bgColor=silver border=2 borderColorDark=gray borderColorLight=silver
cellPadding=2 cellSpacing=1 cols=1 id=tableResource rules=rows width="100%" onKeyDown="navigateKeys()" onKeyUp="setKeyDown(false)">
<thead>

<tr bgColor=#0a6846>
 <th class=thData width=1%><input type=checkbox id=checkLineAll onclick=setOnlineEditCheckBox(getUpperObj(this,"TABLE"),this.checked)></th><!--Fixed-->
 <th class=thData width=30%>標題一</th>
 <th class=thData width=30%>標題二</th>
 <th class=thData width=39%>標題三</th>
 <!--<th....................標題N...可定義多個-->
</tr>

<TR style="display:none" bgColor=#e0e0e0 class=tableData onclick=lightonRow(this)>
 <TD class=scheduleButtonVisible ><input type=checkbox id=checkLine></td><!--Fixed-->
 <TD><input class=inputTableData name=x onclick=showInputSelect(this,qswh) onblur=hideInput(qswh)></TD>
 <TD><input class=inputTableData name=y></TD>
 <TD><input class=inputTableData name=z></TD>
 <!--<TD><input...........name=...></TD>....可定義多個,與上面的<th>數目保持一致...-->
</TR>

</thead>

<tbody>
</tbody>

</TABLE>

</form>
<p align=center><font size="2">*****=====下列按鈕根據需要選定使用======*****</font></p>

<p align=center>
<input type=button onclick="addRow(tableResource)" value="添加空資料行">
<input type=button onclick="addInstanceRow(tableResource,['x','y','z'],['1','100','這是一個測試的例子'])" value="添加執行個體資料行">
<input type=button onclick="deleteRow(tableResource)" value="刪除">
<input type=button onclick="clearRow(tableResource)" value="刪除全部">
<input type=button onclick="setOnlineEditCheckBox(tableResource,true)" value="選中全部">
</p>

</body>
</html>

 

 

======onlineEditTable.js======

//全域變數
var inputFocus;//該變數記錄當前焦點的input
var bKeyDown=false;//記錄鍵盤被按下的狀態,當有鍵盤按下時其值為true

function setRowClass(obj,className){
 obj.className=className;
 var oldClass,toClass;
 if(className=="tableData")  {oldClass="inputTableDataHit";toClass="inputTableData";}
 if(className=="tableDataHit") {oldClass="inputTableData";toClass="inputTableDataHit";}
 var objsInput=obj.all;
 for(var i=0;i<objsInput.length;i++)
  if(objsInput[i].tagName=="INPUT")if(objsInput[i].className==oldClass)objsInput[i].className=toClass;
}

function lightonRow(obj){
 if(obj.tagName!="TR")return;

 //將所有未被選中的行取消高亮度現實
        var tableOnlineEdit=obj.parentElement;
        while(tableOnlineEdit.tagName!="TABLE")tableOnlineEdit=tableOnlineEdit.parentElement;
 var objsCheckBox=tableOnlineEdit.all("checkLine");
 for(var iCheckBox=1;iCheckBox<objsCheckBox.length;iCheckBox++)
  if(objsCheckBox[iCheckBox].checked==false) setRowClass(tableOnlineEdit.rows[iCheckBox+1],"tableData");

 //當前點擊行高亮度顯示
 setRowClass(obj,"tableDataHit");
}

//得到obj的上階項目TagName
function getUpperObj(obj,TagName){
 var strTagName=TagName.toUpperCase();
 var objUpper=obj.parentElement;
 while(objUpper){
  if(objUpper.tagName==strTagName) break;
  objUpper=objUpper.parentElement;
 }
 return objUpper;
}

function getPosition(obj,pos){
   var t=eval("obj."+pos);
   while(obj=obj.offsetParent){
      t+=eval("obj."+pos);
   }
   return t;
}
function showInputSelect(obj,objShow){
 inputFocus=obj;//記錄當前焦點input至全域變數
 objShow.style.top =getPosition(obj,"offsetTop")+obj.offsetHeight+2;
 objShow.style.left=getPosition(obj,"offsetLeft");
 objShow.style.width=obj.offsetWidth;
 objShow.value=obj.value;
 objShow.style.display="block";
}

function setInputFromSelect(objTo,objShow){
 objTo.value=objShow.options[objShow.selectedIndex].value;
 //objShow.style.display="none";
}

function hideInput(obj){
 obj.style.display="none";
}

function clearRow(objTable){
  var tbodyOnlineEdit=objTable.getElementsByTagName("TBODY")[0];
  for (var i=tbodyOnlineEdit.children.length-1;i>=0;i--)
    tbodyOnlineEdit.deleteRow(i);
}

function deleteRow(objTable){
 var tbodyOnlineEdit=objTable.getElementsByTagName("TBODY")[0];
 for (var i=tbodyOnlineEdit.children.length-1; i>=0 ; i-- )
  if (tbodyOnlineEdit.children[i].firstChild.firstChild.checked)
   tbodyOnlineEdit.deleteRow(i);
}

function addRow(objTable){
 var tbodyOnlineEdit=objTable.getElementsByTagName("TBODY")[0];
 var theadOnlineEdit=objTable.getElementsByTagName("THEAD")[0];
 var elm = theadOnlineEdit.lastChild.cloneNode(true);
 elm.style.display="";
 tbodyOnlineEdit.insertBefore(elm);
}

//將指定資料行的objRow的輸入欄位strName設定為strValue
function setInputValue(objRow,strName,strValue){
 var objs=objRow.all;
 for(var i=0;i<objs.length;i++){
  if(objs[i].name==strName)objs[i].value=strValue;
 }
}

//添加一個執行個體資料行
function addInstanceRow(objTable,Names,Values){
 var tbodyOnlineEdit=objTable.getElementsByTagName("TBODY")[0];
 var theadOnlineEdit=objTable.getElementsByTagName("THEAD")[0];
 var elm = theadOnlineEdit.lastChild.cloneNode(true)
 elm.style.display="";
        for(var i=0;i<Names.length;i++)
          setInputValue(elm,Names[i],Values[i]);
 tbodyOnlineEdit.insertBefore(elm);
}

//將全部複選框設為指定值
function setOnlineEditCheckBox(obj,value){
 var tbodyOnlineEdit=obj.getElementsByTagName("TBODY")[0];
 for (var i=tbodyOnlineEdit.children.length-1; i>=0 ; i-- )
  tbodyOnlineEdit.children[i].firstChild.firstChild.checked=value;
}

//為動態表格增加鍵盤導航功能,要使用該功能請在表格定義中增加事件處理onKeyDown="navigateKeys()" onKeyUp="setKeyDown(false)"
//有一點點問題,當按下"->"跳轉到下一輸入欄位時,游標顯示在第一個字元之後
//建議仍然使用Tab鍵跳轉
function navigateKeys(){
 if(bKeyDown) return;
 bKeyDown=true;
 var elm=event.srcElement;
 if(elm.tagName!="INPUT") return;//預設只對INPUT進行導航,可自行設定

 var objTD=elm.parentElement;
 var objTR=objTD.parentElement;
 var objTBODY=objTR.parentElement.parentElement;
 var objTable=objTBODY.parentElement;

 var nRow=objTR.rowIndex;
 var nCell=objTD.cellIndex;

 var nKeyCode=event.keyCode;
 switch(nKeyCode){
  case 37://<-
   if(getCursorPosition(elm)>0)return;
   nCell--;
   if(nCell==0){
    nRow--;//跳轉到上一行
    nCell=objTR.cells.length-1;//最後一列
   }
   break;
  case 38://^
   nRow--;
   break;
  case 39://->
   if(getCursorPosition(elm)<elm.value.length)return;
   nCell++;
   if(nCell==objTR.cells.length){    
    nRow++;//跳轉到下一行首位置
    nCell=1;//第一列
   }
   break;
  case 40:///|/
   nRow++;
   if(nRow==objTBODY.rows.length){    
    addRow(objTable);//增加一個空行
    nCell=1;//跳轉到第一列
   }
   break;
  case 13://Enter
   nCell++;
   if(nCell==objTR.cells.length){    
    nRow++;//跳轉到下一行首位置
    nCell=1;//第一列
   }
   if(nRow==objTBODY.rows.length){    
    addRow(objTable);//增加一個空行
    nCell=1;//跳轉到第一列
   }

   break;
  default://do nothing
   return;
 }
 if(nRow<2 || nRow>=objTBODY.rows.length || nCell<1 ||nCell>=objTR.cells.length) return;

 objTR=objTBODY.rows[nRow];
 objTD=objTR.cells[nCell];
 var objs=objTD.all;
 for(var i=0;i<objs.length;i++){
  //此處使用ojbs[0],實際使用時可能需要加以修改,或加入其他條件
  try{
   lightonRow(objTR);
   objs[i].focus();//setCursorPosition(objs[i],-1);
   return;
  }catch(e){
   continue;
   //if error occur,continue to next element
  }
 }//end for objs.length
}

//設定鍵盤狀態,即bKeyDown的值
function setKeyDown(status){
 bKeyDown=status;
}

//得到游標的位置
function getCursorPosition(obj){
 var qswh="@#%#^&#*$"
 obj.focus();
 rng=document.selection.createRange();
 rng.text=qswh;
 var nPosition=obj.value.indexOf(qswh)
 rng.moveStart("character", -qswh.length)
 rng.text="";
 return nPosition;
}

//設定游標位置
function setCursorPosition(obj,position){
 range=obj.createTextRange();
 range.collapse(true);
 range.moveStart('character',position);
 range.select();
}

 

 

======onlineEditTable.css======

.thData{
BACKGROUND:0a6846;
}
.tableData {
BACKGROUND: white; BORDER-BOTTOM: white 1px dashed; BORDER-LEFT: white 1px dashed; BORDER-RIGHT: white 1px dashed; BORDER-TOP: white 1px dashed; COLOR: black; CURSOR: hand; FONT-FAMILY: verdana,arial,helvetica; FONT-SIZE: 12px
}
.inputTableData {
BACKGROUND: white; BORDER-BOTTOM: white 1px dashed; BORDER-LEFT: white 1px dashed; BORDER-RIGHT: white 1px dashed; BORDER-TOP: white 1px dashed; COLOR: black; CURSOR: hand; FONT-FAMILY: verdana,arial,helvetica; FONT-SIZE: 12px;width:100%;
}
.tableDataSel {
BACKGROUND: #6090d0; BORDER-BOTTOM: #6090d0 1px dashed; BORDER-LEFT: #6090d0 1px dashed; BORDER-RIGHT: #6090d0 1px dashed; BORDER-TOP: #6090d0 1px dashed; COLOR: white; CURSOR: hand; FONT-FAMILY: verdana,arial,helvetica; FONT-SIZE: 12px
}
.tableDataHit {
BACKGROUND: #d0e0ff; BORDER-BOTTOM: #d0e0ff 1px dashed; BORDER-LEFT: #d0e0ff 1px dashed; BORDER-RIGHT: #d0e0ff 1px dashed; BORDER-TOP: #d0e0ff 1px dashed; COLOR: black; CURSOR: hand; FONT-FAMILY: verdana,arial,helvetica; FONT-SIZE: 12px
}
.inputTableDataHit {
BACKGROUND: #d0e0ff; BORDER-BOTTOM: #d0e0ff 1px dashed; BORDER-LEFT: #d0e0ff 1px dashed; BORDER-RIGHT: #d0e0ff 1px dashed; BORDER-TOP: #d0e0ff 1px dashed; COLOR: black; CURSOR: hand; FONT-FAMILY: verdana,arial,helvetica; FONT-SIZE: 12px;width:100%
}
.tableDataOver {
BACKGROUND: #d0e0ff; BORDER-BOTTOM: #d0e0ff 1px dashed; BORDER-LEFT: #d0e0ff 1px dashed; BORDER-RIGHT: #d0e0ff 1px dashed; BORDER-TOP: #d0e0ff 1px dashed; COLOR: black; CURSOR: hand; FONT-FAMILY: verdana,arial,helvetica; FONT-SIZE: 12px
}
.scheduleButtonVisible {
BACKGROUND: silver; BORDER-BOTTOM: gray 1px dashed; BORDER-LEFT: white 1px dashed; BORDER-RIGHT: gray 1px dashed; BORDER-TOP: white 1px dashed; COLOR: black; CURSOR: hand; FONT-FAMILY: webdings; FONT-SIZE: 12px
}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.