Transfer all "searched" rows from one table to the second table

Transfer all "searched" rows from one table to the second table

FrancoisLemieuxFrancoisLemieux Posts: 9Questions: 3Answers: 0

Hello, I am struggling with this, i have two tables side by side,

the first one "selector" has all the available items with 5 diffrents drop downs filters like this one:

$("#Location").on('change', function() {
         tableSelector
        .columns( 6 )
        .search( document.getElementById("Location").value )
        .draw();     
});

This all works but when i try to mass transfer with a function, it transfers random data... i can't put my finger on how to transfer all the rows (visible or not because we mostly show only 10 rows but might need to select 100 rows at the time ) to the table "selected"

Here is what i am trying, i was hoping that search applied would have been the way to go but it doesn't works.

$("#RightArrow").click(function() {     
tableSelector.rows({search:'applied'}).every( function ( rowIdx, tableLoop, rowLoop ) { 
   var $row = $(rowIdx); 
    var addRow = tableSelector.row($row); 
    tableSelected.row.add(addRow.data()).draw(); 
    addRow.remove().draw(); 
     });    
}); 

thank you in advance!

This question has accepted answers - jump to:

Answers

  • kthorngrenkthorngren Posts: 20,821Questions: 26Answers: 4,866
    Answer ✓

    If I understand what you are doing you can utilize the API's differently and not loop through each row. Although this example is a bit different than yours it might help you get started.

    http://live.datatables.net/levelije/1/edit

    It uses the global search and a button to transfer the searched rows from one table to the other. It assumes the data structure of both tables is the same. It uses the toArray() to get all the rows using {search:'applied'}. Then uses rows.add() to add them to the second table. Using the same {search:'applied'} it removes the searched rows, using rows().remove() from the originating table. This should work the same for your column search.

    Kevin

  • colincolin Posts: 15,229Questions: 1Answers: 2,593
    Answer ✓

    Hi Francois,

    Take a look at this live example - it's doing what you want. Like you, I couldn't get the add and the remove working in the same every iteration, so I had to break it up into two passes - the transfer, and then the remove. I think the problem is because the indexing goes bad if the rows are being deleted while the every is in progress, but I could be wrong.

    Hope that helps,

    Cheers,

    Colin

  • colincolin Posts: 15,229Questions: 1Answers: 2,593

    Ha, there's service, two examples! Kevin beat me to it :)

    C

  • FrancoisLemieuxFrancoisLemieux Posts: 9Questions: 3Answers: 0
    edited February 2018

    Wow this really helps alot!! i've been stuck on this for hours trying to use the row index which always change and it was throwing me some random data!!!! thank you a thousand times to both of you. I think Kevin's is more lightweight since it runs only once. thanks again!

This discussion has been closed.