動態增刪表格中的行【代碼片段】

//----------------爲表格添加一個新行----------------//
 function tableAdd() {
  var newRow;
  var rownum = table_elment.rows.length;
  newRow=table_elment.insertRow(rownum);
  newRow.id="r"+rownum;
  newRow.align = "center";
  newRow.bgColor="#ffffff";
  i++;
  
  
  var str1='<input type="hidden" id="specialtyid'+rownum+'" name="speaceid" value="${loginer.specialtyId}">'
   +'<input type="text" id="specialtyname'+rownum+'" name="specialtyname" value="${loginer.specialtyName}"'
   +'onClick="SelectWorkGroup1('+rownum+');"'
   +'size="15" readonly/>';
   
  
  
  var str2='<input type="hidden" id="depid'+rownum+'" name="depid" value="${loginer.str_DepId}" />'
    +'<input type="text" id="depname'+rownum+'" name="depname" value="${loginer.str_DepName}"'
    +'onClick="SelectWorkGroup4('+rownum+',/'${loginer.strOpFac}/')"'
    +'size="12" readonly />';
    
  
  
  newRow.insertCell(0).innerHTML=document.all.div_tdvalue1.innerHTML;
  newRow.insertCell(1).innerHTML=document.all.div_tdvalue2.innerHTML;
  newRow.insertCell(2).innerHTML=str1;
  newRow.insertCell(3).innerHTML=str2;
  newRow.insertCell(4).innerHTML=document.all.div_tdvalue3.innerHTML;;
  newRow.insertCell(5).innerHTML=document.all.div_tdvalue4.innerHTML;
  cell6 = newRow.insertCell(6);
  cell6.colSpan = 2;
  cell6.innerHTML = document.all.div_tdvalue5.innerHTML;
 
  cont++;
 }

 


 // -----------------  刪除當前行  ----------------- //
 function tabledel() {
         var trIndex = getRowIndex();
        
         table_elment.deleteRow(trIndex);
 }

 


 // --------------  獲取當前行的索引  ------------- //
 function getRowIndex()
 {
     try
     {
         var Elm  =  event.srcElement;
         while(Elm  &&  Elm.tagName  !=  "TR")
         {
             Elm  =  Elm.parentElement;
         }
         var intRowIndex = Elm.rowIndex;
         return intRowIndex;
     }
     catch(ex)
     {
         alert("getRowIndex() Err  5001:/r/n"  +  ex);
         m_strError += "getRowIndex() Error" + ex;
     }
 }

 

 

......

 

 

 

 // --------------  新增一行的代碼示例  ------------- //

<input type="button" class="fm" οnclick="tableAdd()"
        id="add" value="新增">

 

// --------------  刪除一行的代碼示例  ------------- //

<div id="div_tdvalue5" style="display:none">
  <div id="div_del" align="center" style="cursor:hand;display:"
   onClick="tabledel();">
   刪除
  </div>
  <div id="div_del1" align="center" style="cursor:hand;display:none">
   <font color="#cccccc">刪除</font>
  </div>
 </div>

( 該方法適合於刪除連接或按鈕位於各自行中) 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章