简体   繁体   中英

DataTable (jQuery) remove last child

I have the following question: I have a DataTable(jQuery) that I order by the first column descending. What I'm trying to do is to remove the last row if the count (Ex. 10) has been reached so the table will never exceed 10 rows. I have tried a few ways all with no success. Any help would be appreciated.

UPDATE: For some reason it only removes the first row.

    // This is where I assign the DataTable to a variable.
    var unprintedTable = $('#unprinted-table').DataTable({
           "order": [
               [1, "desc"]
           ]
    });

    var unprintedLogLength = 5;
    var unprintedLogCount = 0; // Incremented when a new row is added.

    if (unprintedLogCount > unprintedLogLength) {
        unprintedTable.row($(this).parent('tr:last-child')[0]).remove();
    }

Try This One

  <button onclick="RemoveLastRow()">Remove Last Row</button>
    <table id="MyTable" class="table table-bordered table-striped" width="100%" cellspacing="0">
        <thead>
            <tr>
                <th>Column 1</th>
                <th>Column 2</th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td><button class="Mybtn">Click me</button></td>
                <td>Hello</td>
            </tr>
        </tbody>
    </table>

    <script>
        var Dtable;
        $(document).ready(function () {
            Dtable = $("#MyTable").DataTable();
        });


        function RemoveLastRow() {
            Dtable.row(Dtable.data().length).remove().draw(false);         
         );
    </script>

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