How to delete current row with jquery datatable plugin

前端 未结 5 1859
既然无缘
既然无缘 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:36

    Let's say you attached a function to be called when the user clicks on the button. The function would be something like this

    function DeleteRow(event)
    {
      //get the row of the cell that is clicked
      var $row = $(this).parents("tr").eq(0)
      //if you need the id you can get it as
      var rowid = $row.attr("id");
      //now you can call delete function on this row
      $row.delete(); 
    }
    
    0 讨论(0)
  • 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);
    });
    
    0 讨论(0)
  • 2020-12-28 15:44

    How about this:

        // Delete Row
        $('.glyphicon-minus').on("click", function() {
            configTable.row($(this).closest("tr").get(0)).remove().draw();
        });
    
    0 讨论(0)
  • 2020-12-28 15:50

    Try this:

    var row = $(this).closest("tr").get(0);
    oTable.fnDeleteRow(oTable.fnGetPosition(row));
    

    If it doesn't work, check the following example

    0 讨论(0)
  • 2020-12-28 15:52

    from this page:

    $('#example tbody td').click( function () {
        /* Get the position of the current data from the node */
        var aPos = oTable.fnGetPosition( this );
    
        //...
    } );
    
    0 讨论(0)
提交回复
热议问题