Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete a row from a datatable

I am trying to delete with jQuery a row selecting id="data(Number)" from a datatable how this. That's possible, or id would be better on <tr> tag, instead of <td> tag?

<table cellpadding="0" cellspacing="0" border="0" class="table table-striped table-bordered">
    <thead>
        <tr>
            <th>Check</th>
            <th>Field_1</th>
            <th>Field_2</th>
            <th>Field_3</th>
        </tr>
    </thead>
    <tbody id="dataTable">
        <tr>
            <td><input type='checkbox' id='data1'><br></td>
            <td>Field_1_Input1</td>
            <td>Field_2_Input1</td>
            <td>Field_3_Input1</td>
        </tr>
        <tr>
            <td><input type='checkbox' id='data2'><br></td>
            <td>Field_1_Input2</td>
            <td>Field_2_Input2</td>
            <td>Field_3_Input2</td>
        </tr>
    </tbody>
</table>
like image 743
oscarvady Avatar asked May 22 '26 10:05

oscarvady


2 Answers

Try:

 function removerow(number){
    $('#data'+number).closest('tr').remove();
}

and then you can call for example removerow(2) to delete row that has the input element with id=data2

DEMO

UPDATE (from comments)

To get also the td elements text within the row with $("#data"+i) try:

$('#data' + number).parent().siblings().each(function () {
        console.log($(this).text());
});

DEMO2

like image 176
laaposto Avatar answered May 24 '26 23:05

laaposto


try the following -

  1. First get the dataTable instance

var oTable = $('#table_id').dataTable();

  1. call below function to delete the row corresponding to selected

oTable.fnDeleteRow(oTable.fnGetPosition(selected_tr)); // JQuery dataTable function to delete the row from the table

like image 31
Ankush Jain Avatar answered May 25 '26 00:05

Ankush Jain



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!