Get TR data from selected rows

Get TR data from selected rows

arnorbldarnorbld Posts: 123Questions: 23Answers: 1

Hi guys,

I have a table with multi-select.

I need to loop through the selected rows and get the TR element as it has data attributes I need to get to. For example:

<tr class="job-list-row odd" data-jobnumber="job1" data-job_id="1" data-comp_num="900" role="row">

</tr>
var rowsSelected = jobTable.rows( { selected: true } ).count();

This gives me the correct number of selected rows.  But if I try to iterate those using:
jobTable.rows( { selected: true } ).each(function(index){ console.log('row'); });
This only ever seems to execute once - I only get "row" once in the console.  

I'm also not finding the proper syntax to access the elements in the node()  If I use something like this inside the .each()
var tr = jobTable.row(index).node(); var trstr = JSON.stringify(tr); console.log('Row: ' + trstr);

I see that it contains the data elements on the TR, but again, I only ever get ONE row, even if multiple rows are selected.

I'm sure the solution is staring me squarely in the face but I just cannot see it!

Essentially I need to loop through the selected rows and get the TR (node) and also some of the column elements (data) . What is the best way to do this?

Best regards,

This question has an accepted answers - jump to answer

Answers

  • arnorbldarnorbld Posts: 123Questions: 23Answers: 1

    Hi guys,

    Something got seriously messed up with this message I just sent - something went wrong with the first code block and the rest got all messed up. Lets try again:

    I need to loop through the selected rows and get the TR element as it has data attributes I need to get to. For example:

    <tr class="job-list-row odd" data-jobnumber="job1" data-job_id="1" data-comp_num="900" role="row">
    
    </tr>
    

    If I use:

    var rowsSelected = jobTable.rows( { selected: true } ).count();
    

    This gives me the correct number of selected rows. But if I try to iterate those using:

    jobTable.rows( { selected: true } ).each(function(index){ 
      console.log('row'); 
    });
    

    This only ever seems to execute once - I only get "row" once in the console.

    I'm also not finding the proper syntax to access the elements in the node() If I use something like this inside the .each()

    var tr = jobTable.row(index).node(); 
    var trstr = JSON.stringify(tr); 
    console.log('Row: ' + trstr);
    

    I see that it contains the data elements on the TR, but again, I only ever get ONE row, even if multiple rows are selected.

    I'm sure the solution is staring me squarely in the face but I just cannot see it!

    Essentially I need to loop through the selected rows and get the TR (node) and also some of the column elements (data) . What is the best way to do this?

    Best regards,

  • kthorngrenkthorngren Posts: 21,443Questions: 26Answers: 4,974
    Answer ✓
  • arnorbldarnorbld Posts: 123Questions: 23Answers: 1

    Hi,

    THANK YOU! I knew this was staring me in the face! Will try it and let you know!

    Best regards,

  • arnorbldarnorbld Posts: 123Questions: 23Answers: 1

    Hi,

    This worked perfectly for what I needed! Thank you again!

    .Best regards,

Sign In or Register to comment.