2012-08-30 40 views
2

我dinamicaly建立一个HTML表格与Java脚本:这是我的HTML表格:生成HTML表使用JavaScript,行顺序

<table class="ui-list" cellpadding="2" id = "tbl"> 
       <thead> 
        <tr> 
         <th>Artikel ID</th> 
         <th>Artikelnr</th> 
         <th>Bezeichnung</th> 
         <th>Stk.</th> 
         <th><?php echo FW_HTML_Button::build('entfernen', array('onclick' => '' ,'id'=>'get_button')); ?></th> 

        </tr> 
       </thead> 
       <tbody> 
        <tr> 
        <td></td> 
        <td></td> 
        <td>Gesamt:</td> 
        <td></td> 
        <td>Action:</td> 
        </tr> 
       <!-- Fill with JS --> 
       </tbody> 
      </table> 

这是我写些行:

  function addRow(tableID) { 

       var table = document.getElementById(tableID); 

       var rowCount = table.rows.length; 
       var row = table.insertRow(rowCount); 

       var cell1 = row.insertCell(0); 
       var element1 = obj[16][count]["id"]["article_id"]; 
       cell1.innerHTML = element1; 

       var cell2 = row.insertCell(1); 
       var element2 = obj[16][count]["id"]["article_no_internal"]; 
       cell2.innerHTML = element2; 

       var cell3 = row.insertCell(2); 
       var element3 = obj[16][count]["id"]["article_name_internal"]; 
       cell3.innerHTML = element3; 

       var cell4 = row.insertCell(3); 
       cell4.innerHTML = obj[16][count]["stk"];; 

       var cell5 = row.insertCell(4); 
       var element4 = document.createElement("input"); 
       element4.type = "checkbox"; 
       cell5.appendChild(element4); 

      } 

它工作得很好,我唯一的瑕疵是,因为你可以看到我已经在我的HTML部分中有一行,我想把新行,befor我在html中创建的行。现在它总是将新行放在写入'gesamt'和'action'的行之后。是否有可能使这个不同?

回答

2

使用

var row = table.insertRow(rowCount - 1); 

,而不是

var row = table.insertRow(rowCount); 

(假设,当然,这是guarranteed,你永远在你的表中有这个页脚行)。

请参阅:http://jsfiddle.net/hwSfG/

+0

是它保证,它的作品,谢谢。 –