Como excluir a linha atual com o plugin jQuery datatable
-
20-09-2019 - |
Pergunta
Eu tenho uma coluna com botões em uma tabela que estou usando o plug -in JQuery Datatable. Os botões dizem "Remover" e a idéia é que, quando você clica nesse botão, ele exclui a linha atual na tabela.
Quando eu ligo fnDeleteRow
Parece funcionar da primeira vez, mas não há mais tempo para essa linha, por isso parece que não está realmente excluindo a linha corretamente.
Solução
Experimente isso:
var row = $(this).closest("tr").get(0);
oTable.fnDeleteRow(oTable.fnGetPosition(row));
Se não funcionar, verifique o seguinte exemplo
Outras dicas
Digamos que você anexe uma função a ser chamada quando o usuário clicar no botão. A função seria algo assim
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();
}
Que tal agora:
// Delete Row
$('.glyphicon-minus').on("click", function() {
configTable.row($(this).closest("tr").get(0)).remove().draw();
});
a partir de esta página:
$('#example tbody td').click( function () {
/* Get the position of the current data from the node */
var aPos = oTable.fnGetPosition( this );
//...
} );
É assim que funciona para mim. Na função pronta do documento, atribui uma versão convertida da tabela HTML a uma variável e, quando um botão no clique, passo por pais/filhos com jQuery e envio a linha que você recebe como um parâmetro para a função fNDeleterow () da biblioteca.
Aqui estão os comentários da função da biblioteca. E um exemplo mencionado na 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);
});