remove a selected row

remove a selected row

pikitapikita Posts: 1Questions: 1Answers: 0

Hello,

I'm trying to perform my table in my web interface, but I am asking help for removing a selected row from this table, I'am using this code :
$(document).ready(function() {
var table = $('#example').DataTable();

        $('#example tbody ').on( 'click', 'tr', function () {
            if ( $(this).hasClass('selected') ) {
                $(this).removeClass('selected');
            }
            else {
                table.$('tr.selected').removeClass('selected');
                $(this).addClass('selected');
            }
        } );

       $('#delete').click( function () {
            if (table.rows('.selected').data().length==0){
                alert( table.rows('.selected').data().length +' row(s) selected, you should select the row you want to delete!' );

            }
            alert( table.rows('.selected').data().length +' row(s) selected, are you sure you want to delete this row?' );

            table.rows('.selected').remove().draw(false);
        } );


    } );

the selection is done but the row is not deleted after clicking on the button. am i missing something?
Could every one help me please ?
Thank you so much !

Answers

  • allanallan Posts: 61,667Questions: 1Answers: 10,096 Site admin

    Thanks for your question - however, per the forum rules can you link to a test case showing the issue please. This will allow the issue to be debugged.

    Information on how to create a test page, if you can't provide a link to your own page can be found here.

    Thanks,
    Allan

  • eliotjseeliotjse Posts: 5Questions: 0Answers: 0
    var table = $(".your table class name")["1"]
    var tableClone = $(".your table class name")["3"]
    table.children["1"].children[currentRowId].remove();
    tableClone.children["1"].children[currentRowId].remove();
    

    I hope this will help you. It will not slow down the performance as it is directly trying to remove the rows from your table.
    Thanks

  • allanallan Posts: 61,667Questions: 1Answers: 10,096 Site admin

    You need to use the DataTables API to remove rows from a DataTable - row().remove() specifically. Just removing it from the DOM will not work as DataTables will just put it back when it next draws that row.

    Allan

  • eliotjseeliotjse Posts: 5Questions: 0Answers: 0

    Hi Allan,

    I have tried using row.remove API it actually slows down since having more records so having said i tried directly injecting it. I did not find any issue after injecting it. It was working fine for me. Can you give me a scenario when it will fail.

    Thanks

  • allanallan Posts: 61,667Questions: 1Answers: 10,096 Site admin

    Yes - just redraw the table. i.e. change page or alter the sorting.

    Allan

  • eliotjseeliotjse Posts: 5Questions: 0Answers: 0

    In my case i don't have sorting and pagination. only fixed header and column with add edit and delete operation having 48*668 rows. At that time i tried using Remove API it slows down so i tried injecting. As i have said previously it may be useful sometime not always. Thanks for letting me know :smile:

    Thanks

  • allanallan Posts: 61,667Questions: 1Answers: 10,096 Site admin

    Try just calling the draw() method from the console then. To anyone else reading this, I'd really encourage you to use the API. Using the DOM methods can work, but only in very limited cases.

    Allan

This discussion has been closed.