Cómo eliminar la fila actual con el complemento datatable de jquery


Tengo una columna con botones en una tabla Que estoy usando jQuery datatable plugin. Los botones dicen "Eliminar" y la idea es que al hacer clic en ese botón se borra la fila actual en la tabla.

Cuando llamo a fnDeleteRow parece funcionar la primera vez, pero no hay más tiempo para esa fila, por lo que parece que no está borrando la fila correctamente.

Author: Sam, 2009-12-18

5 answers

Prueba esto:

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

Si no funciona, compruebe el siguiente ejemplo

 61
Author: Jason Orendorff,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-07-03 11:38:07

Digamos que adjuntó una función para ser llamada cuando el usuario hace clic en el botón. La función sería algo como esto

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(); 
}
 2
Author: Sridhar,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2009-12-18 04:48:47

¿qué tal esto:

    // Delete Row
    $('.glyphicon-minus').on("click", function() {
        configTable.row($(this).closest("tr").get(0)).remove().draw();
    });
 1
Author: Ryan Reynolds,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-10-03 22:07:16

De esta página:

$('#example tbody td').click( function () {
    /* Get the position of the current data from the node */
    var aPos = oTable.fnGetPosition( this );

    //...
} );
 0
Author: cobbal,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2009-12-18 04:27:23

Así es como funciona para mí. En la función document ready le asigno la versión convertida de la tabla HTML a una variable y cuando se hace clic en un botón, paso por parents/childs con jQuery y envío la fila que obtiene como parámetro a la función fnDeleteRow() de la biblioteca.

Aquí están los comentarios de la función de biblioteca. Y un ejemplo que se menciona en la biblioteca.

/**
* 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
Author: Cengiz Araz,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-03-21 17:08:05