Problem getting row id for remove item

Problem getting row id for remove item

KospiKospi Posts: 6Questions: 4Answers: 0
edited July 2023 in Free community support

Hi everyone,
I have some problem when remove items in a datatable, especially when remove the last item, when remove others no problem. this is part of code:

 columnDefs: [{
                "targets": -1,
                "width": "120px",
                "data": null,
                "render": function (data, type, row, meta) {
                    return '<a class="btn btn-info mr-1" href="javascript:editar_linea(' + meta.row + ')" role="button"><i class="fas fa-user-edit"></i></a>' +
                            '<a class="btn btn-danger" href="javascript:eliminar_linea(' + meta.row + ')" role="button" onclick="return confirm(\'Confirma Eliminar el Item?\')"><i class="fas fa-trash-alt"></i></a>'
                }
            }] 

I am calling function eliminar_linea(' + meta.row + ')" where I am calling a function in server...this is the code:

var eliminar_linea = function (fila) {
    debugger;

    tablaLineas.row(fila).remove();    
    tablaLineas.row(fila).draw();
    var datosTabla = tablaLineas.columns([0, 2, 4]).data().toArray();

    $.ajax({
        type: "POST",
        url: "Venta?action=DeleteItem&tablaLineas="+JSON.stringify(datosTabla)+"&idsesion="+idsesion,
        dataType: 'json',
        success: function (data) {
            actualizar_footer(data);
        },
        error: function (xhr, exception) {
            errorCallback(xhr, exception);
        },
        complete: function (data) {
            //
        }
    });
    
};

the problem that I could see is that the row number (parameter fila) not always corresponding with the row of datatable, especially when someone remove some items previously.
Some idea how could correct this?
Regards!

Edited by Colin - Syntax highlighting. Details on how to highlight code using markdown can be found in this guide.

This question has an accepted answers - jump to answer

Answers

  • colincolin Posts: 15,240Questions: 1Answers: 2,599
    edited July 2023

    It would be worth looking at the Editor extension, this has full support for CRUD (create/read/update/delete) operations on the table's data. There, you would use remove() to delete the row.

    Colin

  • kthorngrenkthorngren Posts: 21,299Questions: 26Answers: 4,945

    My suggestion is to use jQuery delegated events like this example. Use jQuery closest() to get the row of the clicked button. Use this row to remove the row.

    Kevin

  • kthorngrenkthorngren Posts: 21,299Questions: 26Answers: 4,945
    edited July 2023 Answer ✓

    The meta.row parameter is not an ID but a row index, see the row().index() docs for details. Another option is to pass a unique ID from the row data instead of meta.row here of editar_linea(' + meta.row + '). Use rowId to set the appropriate column as the row ID. The pass the string #ID as the row-selector.

    Kevin

  • KospiKospi Posts: 6Questions: 4Answers: 0

    Thank you kthorngren !

Sign In or Register to comment.