简体   繁体   English

如何添加一个按钮来清除所有表格行?

[英]How to add a button to clear All table rows?

How to add a button to clear All table rows?如何添加一个按钮来清除所有表格行? I want to add this button to my form, and this is the fiddle of my code.我想将此按钮添加到我的表单中,这是我的代码的小提琴 I try a lot of ways but I didn't work.我尝试了很多方法,但我没有工作。

function render(){
     for (let i = 0; i < allMovie.length; i++) {
        var tr = document.createElement('tr');
        tr.setAttribute("id", "mainTR");
        table.appendChild(tr);
    
        var td1 = document.createElement('td');
        td1.textContent = allMovie[i].MovieName11;
        td1.setAttribute("class", "td1");
        tr.appendChild(td1);
        
        var td2 = document.createElement('td');
        td2.textContent = allMovie[i].selectPlatform11;
        td2.setAttribute("class", "td2");
        tr.appendChild(td2);
    
        var td3 = document.createElement('td');
        td3.textContent = allMovie[i].randomRate;
        td3.setAttribute("class", "td3");
        tr.appendChild(td3);
    
        var td4 = document.createElement('td');
        td4.textContent = allMovie[i].monthlyPay11;
        td4.setAttribute("class", "td4");
        tr.appendChild(td4);
    
        var td5 = document.createElement('td');
        td5.setAttribute("id", "td5");
        td5.innerHTML = `<button onclick=remove()> X </button>`
        tr.appendChild(td5);
    }
    }
    
    function remove() { /line10
      var removeOBJ = document.getElementById("mainTR");
      return removeOBJ.remove();
    } 

First, your table structure is not correct, tr (table rows) should be in tbody and current tbody content should be in thead.首先,您的表格结构不正确,tr(表格行)应该在tbody中,当前 tbody 内容应该在 thead 中。

在此处输入图像描述

Replace your code with following structure:用以下结构替换您的代码:

     <table id="table">
      <thead>
        <tr>
            <th>Movie Name</th>
            <th>Favourite streaming platform</th>
            <th>Movie Rate</th>
            <th>Your pay monthly on Streaming platforms</th>
            <th>Remove</th>
        </tr> 
      </thead>
      <tbody>
        <!-- Your data here -->
      </tbody>
    </table>

In render function instead of appending data in table and you got structure from image above, you should append your data table rows in tbody.在渲染 function 而不是在表中附加数据并且您从上图获得结构时,您应该 append 您的数据表行在 tbody 中。 That means you should replace your line for table variable like this:这意味着您应该像这样替换表变量的行:

var table = document.querySelector('table > tbody');

Now you have control to insert your data table rows in right place.现在您可以控制在正确的位置插入数据表行。 Then we can go and implement deleting data.然后我们可以 go 并实现删除数据。

With just new button and event click you can remove all data from tbody like this:只需新按钮和事件单击,您就可以从 tbody 中删除所有数据,如下所示:

<input type="button" value="Delete data" id="deleteData" onclick="document.querySelector('table > tbody').innerHtml = '';">

You don't really need to assign ids to all rows before you can delete them, you mainly need a reference to the containing table/tbody element.在删除它们之前,您实际上并不需要为所有行分配 id,您主要需要对包含 table/tbody 元素的引用。 To remove all rows, (ie all tr children of a table's body), use the tableElement.removeChild() function.要删除所有行(即表主体的所有tr子项),请使用tableElement.removeChild() function。 Like so:像这样:

function clearTable() {
    const parent = document.getElementById("tbodyElementId");

    while(parent.firstChild)
    {
        parent.removeChild(parent.firstChild);
    }
}

This assumes you have a structure like this:这假设您有这样的结构:

<table>
  <thead>...</thead>
  <tbody id="tbodyElementId">
    <tr>
      <td></td>
      <td></td>
      <td></td>
    </tr>
    <tr>
      <td></td>
      <td></td>
      <td></td>
    </tr>
  </tbody>
</table>

Note that this method does not remove any row(s) in the table header ( <thead> )请注意,此方法不会删除表 header ( <thead> ) 中的任何行

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM