2013-08-01 32 views
0

我正在使用this code向表中添加列和行。由Javascript生成的表格中的每個單元格的ID

現在我正在嘗試爲每個生成的單元添加一個id,如在Excel表中。

對於行分配0,1,2等等,對於列分配a,b,c等,因此第一個單元格的ID將爲0aa0或類似內容。

UPDATE:here is my actual code

var table = document.getElementById("excelTable"); 
var input = table.getElementsByTagName("input"); 
var globalLen = input.length; 
var arr = [], arrStr = []; 
var numId = null, modStr = null, re = null, var1 = null, var2 = null, total = null, sign = null; 

function createCell(cell, r, c) { 
    cell.innerHTML = "<input id='td" + r + c+ "' type='text' onfocus='doEdit(this.id);' onblur='doUpdate(this.value, this.id);' />"; 
} 

function addRowId(tbl) { 
    return tbl.rows.length; 
} 

function addColId(tbl) { 
    for (i = 0; i < tbl.rows.length; i++) { 
     return tbl.rows[i].cells.length; 
    } 
} 

function appendRow(n) { 
    for (; n > 0; n--) { 
     var tbl = document.getElementById('excelTable'), 
      row = tbl.insertRow(tbl.rows.length), 
      i; 
     for (i = 0; i < tbl.rows[0].cells.length; i++) { 
      createCell(row.insertCell(i), addRowId(tbl), addColId(tbl)); 
     } 
    } 
} 

function appendColumn(n) { 
    for (; n > 0; n--) { 
     var tbl = document.getElementById('excelTable'), 
      i; 
     for (i = 0; i < tbl.rows.length; i++) { 
      createCell(tbl.rows[i].insertCell(tbl.rows[i].cells.length), addRowId(tbl), addColId(tbl)); 
     } 
    } 
} 

我有問題,每一個細胞分配唯一的ID。該ID必須包含行號(1,2,3等)中的一位數字和一位數字或列中的字母(1,2,3等或a,b,c等)

因此,最終它看起來像11,12等或1a,1b等。

該ID將被分配給該單元格中的input

我的目標是獲取單元格(行和列)的索引並將這些索引分配給ID。

+0

您可以嘗試'yourcell.setAttribute(「id」,「uniqueIdentifier」);'您擁有的每個單元格和'uniqueidentifier'都可以從行和列中派生,如您所述。 – Si8

+1

或'yourcell.id =「uniqueIdentifier」;'。請點擊「編輯」並將您的實際代碼添加到問題中,而不要依賴指向外部文章的鏈接。 – nnnnnn

+1

好的推特狀態。等等...太久了? –

回答

1

您可以使用yourCell.setAttribute('id', 'a0');只需從循環變量中生成ID即可。

0

您可以嘗試yourcell.setAttribute("id", "uniqueIdentifier");

對於uniqueidentifier你可以得到rows[index]column[index],這將給你該行&列的值。

+1

你可以舉例和這個行[index]列[index]? –

+0

我正在去上班的路上,如果我有機會,我會稍後再演示一個例子,如果沒有人做過。 – Si8

+0

@RazieL又名ALieN類似'alert(document.getElementById(「myTable」)。rows [0] .cells [0] .innerHTML);' –

相關問題