Returning cell data from selected rows

Returning cell data from selected rows

bensjfraserbensjfraser Posts: 2Questions: 1Answers: 0
edited January 2020 in Free community support

Trying to select only column '0' data from selected rows. This is what I've got so far but the output shows every row data and not just selected..

Any help would be massively appreciated.

$('.datatable').DataTable( {
    "dom": 'Bfrtip',
    "buttons": [
        {
            "extend": 'selected',
            "text": 'Send Invites',
            "action": function ( e, dt, node, config ) {
                var table = $('.datatable').DataTable();
                table.rows({selected:true}).map( function ( rowIdx, tableLoop, rowLoop ) {
                    var data = this.column(0).data();
                    new_data = data.join(',');
                    console.log(new_data);
                    //window.location.replace(new_data);
                });
            }
        },
    'selectAll',
    'selectNone'
    ],
    "select": 'multi',
    "paging": false,
    "bInfo" : false,
    "scrollX": false,
    "searching": true,
    "responsive": true,
    "order": [[0, 'asc']]
});

Thanks in advance!

Ben

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 21,166Questions: 26Answers: 4,921
    edited January 2020 Answer ✓

    column(0).data();

    This will grab all the data in column 0 not just the row in the loop.

    My suggestion is to use rows().every() with {selected:true} to loop the selected rows. The documented example shows how to get the data for the row. From there you can access column 0 and append to an array or whatever you want to do. For example:

    "action": function ( e, dt, node, config ) {
        var table = $('.datatable').DataTable();    
        var columnData = [];
        table.rows( {selected: true} ).every(function ( rowIdx, tableLoop, rowLoop ) {
          var data = this.data();      
          columnData.push(data[0]);
        });
        new_data = columnData.join(',');
    }
    

    Kevin

  • bensjfraserbensjfraser Posts: 2Questions: 1Answers: 0

    Absolutely spot on Kevin, works perfectly - I appreciate your help very much!

    Not all heroes wear capes (unless you do wear a cape, in which case, my statement is incorrect, and heroes also wear capes)

    Happy New Year
    Ben

This discussion has been closed.