2017-11-13 15 views
0

私はおそらく質問を耳にするが、カウンタを再起動するにはどうすればよいですか?また、あなたが私のコードで見るように、カウンターは一致すると思われますが、そうではありません。たとえば、最初の行のセルはID:cell 0.0、cell 0.1を持つ必要がありますが、最初の行が先行する行はスキップされています。JavaScriptを再起動するカウンタ

var rowCount = 0; 
 
var cellCount = 0; 
 

 
function appendRow(id, style) { 
 
    var table = document.getElementById(id); // table reference 
 
    length = table.length, 
 
    row = table.insertRow(table.rows.length); // append table row 
 
    row.setAttribute('id', style); 
 
    var i; 
 
    row.id = 'row' + rowCount; 
 
    rowCount++ 
 
    console.log(rowCount, cellCount); 
 
    // insert table cells to the new row 
 
    for (i = 0; i < table.rows[0].cells.length; i++) { 
 
    createCell(row.insertCell(i), i, 'cell ' + rowCount + '.' + cellCount); // doesn't start over once row changes 
 
    cellCount++ 
 
    } 
 
} 
 

 
function createCell(cell, text, style) { 
 
    var div = document.createElement('div'), // create DIV element 
 
    txt = document.createTextNode('_'); // create text node 
 
    div.appendChild(txt); // append text node to the DIV 
 
    div.setAttribute('id', style); // set DIV class attribute 
 
    div.setAttribute('idName', style); // set DIV class attribute for IE (?!) 
 
    cell.appendChild(div); // append DIV to the table cell 
 
}
table { 
 
    text-align: center; 
 
} 
 
td { 
 
    width: 100px; 
 
}
<button id="addCust" class="addSort" onclick="appendRow('custList')">add customer</button> 
 

 
<div class="custScroll"> 
 
    <table id="custListTop" contenteditable="false"> 
 
    <tr> 
 
     <td style="border-top-left-radius: 5px;">Customers</td> 
 
     <td style="border-top-right-radius: 5px;">Main Location</td> 
 
    </tr> 
 
    </table> 
 
    <table id="custList" contenteditable="true"> 
 
    <tr> 
 
     <td>Someone</td> 
 
     <td>something</td> 
 
    </tr> 
 
    </table> 
 
</div>

+0

あなたcellcounterは二倍の速行く:

あなたが次に細胞をやり直すcellCountの代わりに私を使用するようにcreateCellを変更したい場合。 – trincot

答えて

1

は、私は、これはあなたが望むものであるわからないんだけど、あなたはループの後rowCount++を置けば、それはそこからインクリメント0で最初の要素をオフに開始します。二回あなたのテーブルの行のように多くのセルがあるので、

createCell(row.insertCell(i), i, 'cell ' + rowCount + '.' + i); 
+0

うわー...間違いなく過思考 – hannacreed

関連する問題