How to delete current row with jquery datatable plugin

前端 未结 5 1862
既然无缘
既然无缘 2020-12-28 14:59

I have a column with buttons in a table I\'m using jQuery datatable plugin. The buttons say \"Remove\" and the idea is that when you click on that button it deletes the curr

5条回答
  •  孤城傲影
    2020-12-28 15:40

    This is how it works for me. In document ready function I assign converted version of HTML table to a variable and when a button in the is clicked I go through parents/childs with JQuery and send the row you get as a parameter to the library's fnDeleteRow() function.

    Here's is the comments from the library function. And an example that's mentioned in library.

    /**
    * Remove a row for the table
    *  @param {mixed} target The index of the row from aoData to be deleted, or
    *    the TR element you want to delete
    *  @param {function|null} [callBack] Callback function
    *  @param {bool} [redraw=true] Redraw the table or not
    *  @returns {array} The row that was deleted
    *  @dtopt API
    *  @deprecated Since v1.10
    *
    *  @example
    *    $(document).ready(function() {
    *      var oTable = $('#example').dataTable();
    *
    *      // Immediately remove the first row
    *      oTable.fnDeleteRow( 0 );
    *    } );
    */
    
    // And here's how it worked for me.
    var oTable;
    $("document").ready(function () {
        oTable = $("#myTable").dataTable();
    });
    
    //Remove/Delete button's click.
    $("a[name='deleteColumn']").click(function () {
        var $row = $(this).parent().parent();
        oTable.fnDeleteRow($row);
    });
    

提交回复
热议问题