代码之家  ›  专栏  ›  技术社区  ›  Ken Ingram

如何将<tr>插入<表>(使用PHP)

  •  0
  • Ken Ingram  · 技术社区  · 8 年前

    我正在使用PHP,我有一个表,我想动态填充。

    <table>
        <tr>
             <th>This One</th>
             <th>That One</th>
             <th>The Other One</th>
        </tr>
        <tr>
             <td>Final This</td>
             <td>Final That</td>
             <td>Final The Other</td>
       </tr>
    </table>
    

    我正在填充表数据onchange,我希望添加数据 在上面 标有“Final blah”的行。

    每一新行都有数据填充每个单元格。

    是否有一些功能可以在PHP上实现这一点,mayber可以利用一些JavaScript?

    编辑: 我想得到这样的最终产品:

    <table>
        <tr>
             <th>This One</th>
             <th>That One</th>
             <th>The Other One</th>
        </tr>
        <tr> Dynamically added row. Cells already populated from database</tr>
        <tr> Next row of data from the database</tr>
        <tr> The next row should **ALWAYS** be the last row</tr>
        <tr>
             <td>Final This</td>
             <td>Final That</td>
             <td>Final The Other</td>
       </tr>
    </table>
    

    注意,我想保留一个标题行!
    我想在标题行和带有“Final something”的行之间插入行。 最后一行始终存在。

    1 回复  |  直到 8 年前
        1
  •  1
  •   helpdoc    8 年前

    如果我收到您的询问:

     <table id="myTable">
      <tr>
         <th>This One</th>
         <th>That One</th>
         <th>The Other One</th>
      </tr>
       <tbody id="myid">
        <tr><td>Final This</td><td>Final That</td><td>This is your Last Row</td></tr>
       </tbody>
        </table>
        <br>
    
    <button onclick="myCreateFunction()">Add row</button>
    
    <script>
    function myCreateFunction() {
        var table = document.getElementById("myid");
        var row = table.insertRow(0);
        var cell1 = row.insertCell(0);
        var cell2 = row.insertCell(1);
        var cell3 = row.insertCell(2);
        cell1.innerHTML = "Final This";
        cell2.innerHTML = "Final That";
        cell3.innerHTML = "I am inserted between First and last row";
    }
    
    </script>
    

    Fiddle