简体   繁体   中英

jQuery DataTable delete Tr with ID

My Table

<table id="grid">
    <thead>
        <tr>
            <th>ID</th>
            <th>Name</th>
            <th>Position</th>
            <th>Action</th>
        </tr>
    </thead>
    <tbody>
        <tr id="row1">
            <td>Tiger Nixon</td>
            <td>System Architect</td>
            <td><button onclick="doDeleteClick('row1')">Delete</button></td>
        </tr>
        <tr id="row2">
            <td>Garrett Winters</td>
            <td>Accountant</td>
            <td><button onclick="doDeleteClick('row2')">Delete</button></td>
        </tr>
        <tr id="row3">
            <td>Ashton Cox</td>
            <td>Junior Technical Author</td>
            <td><button onclick="doDeleteClick('row3')">Delete</button></td>
        </tr>
    </tbody>
</table>

in a function, I tried

$('#grid').DataTable().row($('#row-2')).remove().draw();

use a function to execute by sending the parameter, how to do this in a row?

just create buttons like this

<button data-id="some-unique-id" class="removeBtn">Delete</button>

and then use simple jquery

$(".removeBtn").on('click', function(){
    $(this).closest('tr').remove();
    var id = $(this).attr('data-id');
    //Do something with the id
});

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