简体   繁体   English

使用deleteRow按ID删除行

[英]Using deleteRow to delete a row by id

Anyways, I'm currently doing a repeater-type of add/delete rows. 无论如何,我目前正在做添加/删除行的中继器类型。 Looking through the documentation, it only appears to manual delete based on row number. 浏览文档时,它似乎只能根据行号手动删除。

Take not that this is called under a LaravelBlade foreach loop so I added respective IDs when being clicked 不要以为这是在LaravelBlade foreach循环下调用的,所以我在单击时添加了各自的ID

var approverCount = 0;

function add(id)
{
    var table = document.getElementById("table" + id);

    var row = table.insertRow();
    var cell1 = row.insertCell(0);
    var cell2 = row.insertCell(1);       

    cell1.innerHTML = "Hi";
    cell2.innerHTML = "Hello";
    testCount++;
    row.id = 'teste_' + id + '_' + testCount;
}

function remove(tableId, rowId)
{
    var table = document.getElementById(tableId);
    var row = document.getElementById(rowId);
    table.deleteRow(row); 
    // stopped here due to constraints from problem
}

If your goal is to delete a table row by its id , then it can be done purely from that one id , and it's fairly straightforward with remove (slightly modern) or parentNode and removeChild (universally supported): 如果您的目标是通过其id删除表行,则可以纯粹从该id删除表行,并且使用remove (略微现代)或parentNoderemoveChild (普遍支持)非常简单:

// On a modern browser
document.getElementById(theRowID).remove();

or 要么

// On slightly less modern browsers
var row = document.getElementById(theRowID);
row.parentNode.removeChild(row);

If for some reason you really want to use the table's deleteRow , then you'd use the row's rowIndex : 如果出于某种原因您确实要使用表的deleteRow ,则可以使用该行的rowIndex

table.deleteRow(row.rowIndex);

您必须使用行号而不是id属性:

table.deleteRow(0);

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

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