简体   繁体   English

DataTable(jQuery)删除最后一个孩子

[英]DataTable (jQuery) remove last child

I have the following question: I have a DataTable(jQuery) that I order by the first column descending. 我有以下问题:我有一个按第一列降序排列的DataTable(jQuery)。 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. 我要尝试执行的操作是,如果已达到计数(例如10),则删除最后一行,因此表永远不会超过10行。 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>

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

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