Traverse rows in sorted order

Traverse rows in sorted order

bjorn.paulstrombjorn.paulstrom Posts: 5Questions: 2Answers: 0
edited August 2016 in Free community support

Hello,
I have a table where the first column is a checkbox, which enables you to select several rows. With these rows selected you are then able to do certain actions.

However i have troubles figuring out which rows have been selected, they dont seem to correspond to the viewing order.

    var checkboxes = document.getElementsByClassName("chk");
    for (i = 0; i < checkboxes.length; i++)
    {       
        if(checkboxes[i].checked==true)
        {
            console.log(i);
        }   
    }

Gives me the indexes of the selected rows in the viewing order, however when i try to delete one of them using this index:

           table.rows(i).remove()

another row gets removed. I used this to see if the values corresponded:

    var checkboxes = document.getElementsByClassName("chk");
    for (i = 0; i < checkboxes.length; i++)
    {       
        if(checkboxes[i].checked==true)
        {
            console.log(i);
            console.log(table.row(i).data());
        }   
    }

they did not :(

So my question is, how can I use the checkboxes to remove the correct rows?

This question has accepted answers - jump to:

Answers

  • allanallan Posts: 63,075Questions: 1Answers: 10,384 Site admin

    Use the selector-modifier option which you can pass into rows():

    table.rows( { order: 'applied' } )
    

    Allan

  • bjorn.paulstrombjorn.paulstrom Posts: 5Questions: 2Answers: 0

    Thank you for your answer however this does not work for me, it gives me the same result as before :(

    I tried the following code:

    var r = confirm("Are you sure you want to delete these reports?");
    var number = 0;
    table.rows( { order: 'applied' } );
    if(r == true)
    {
        var checkboxes = document.getElementsByClassName("chk");
        for (i = 0; i < checkboxes.length; i++)
        {       
            if(checkboxes[i].checked==true)
            {
                console.log(i);
                console.log(table.row(i, { order: 'applied' }).data());
            }   
        }
    }
    

    This prints the indexes of where the checkboxes have been checked, this seems to work correctly. But when using this index to get a row, it fetches the wrong row :(

  • allanallan Posts: 63,075Questions: 1Answers: 10,384 Site admin
    Answer ✓

    But when using this index to get a row, it fetches the wrong row

    That is correct and expected. When using an integer as the selector, it uses the data index (see row-selector). The-type selector modifier` effects the order that the rows are returned in, not how they are selected.

    For the case above I would suggest you simply the code:

    var rows = $( 'input[type=checkbox]:selected', table.rows( { order: 'applied' ).nodes() );
    
    rows.each( function () {
      var tr = $(this).closest('tr');
      var data = table.row( tr ).data();
    
      console.log( data );
    } );
    

    Allan

  • bjorn.paulstrombjorn.paulstrom Posts: 5Questions: 2Answers: 0

    Thank you so much for this, is greatly appreciated :).

    One thing i noticed though,

    input[type=checkbox]:selected

    should be

    input[type=checkbox]:checked

    Except for that it worked out of the box, again thank you

  • allanallan Posts: 63,075Questions: 1Answers: 10,384 Site admin
    Answer ✓

    Yup - good point. Thanks for correcting that!

    Allan

This discussion has been closed.