简体   繁体   中英

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

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):

// 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 :

table.deleteRow(row.rowIndex);

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

table.deleteRow(0);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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