How to get selected row data?

How to get selected row data?

prajaktapprajaktap Posts: 3Questions: 1Answers: 0

Alan,

I am trying to migrate from TableTools to Buttons extension.
i had created a custom button which sends the selected row table data to server and returns documents stored in database to put them in a zip file. i was using the fnGetTableData() method to retrieve the values of the selected row which is formatted as a string. and setting the sFieldSeperator attribute to set a delimiter to separate the individual column values.

$(document).ready( function () {
    $('#example').DataTable( {
        "sDom": 'T<"clear">lfrtip',
        "oTableTools": {
            "aButtons": [
                {   
                     "sButtonText": "Add to Zip",                
                    "sFieldSeperator": ";",
                     "bSelectedOnly": "true",
                    "fnClick": function ( nButton, oConfig, flash ) {
                      var sData = this.fnGetTableData(oConfig);    
                    }
                }
            ]
        }
    } );
} );

sData was passed as a parameter to the action class.
Is there a similar function available in Buttons extension which i can use to get the values of all the selected rows in a string format?

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,812Questions: 1Answers: 10,516 Site admin

    Create a custom button where the action is to use table.rows( { selected: true } ).data(); to get the data. You can then do what you want with that data!

    Allan

  • prajaktapprajaktap Posts: 3Questions: 1Answers: 0
    edited August 2016

    Thanks for your reply Allan.
    In the action function, the console.log shows the output as
    data---[object Object]

     action: function ( e, dt, node, config ) {                                 
                                         var data=oTable.rows( { selected: true }).data();
                                          console.log("data---"+data);   
                                     }
    

    With table tools when i was using the fnGetTableData function, the output was in string format. something like:
    data---;Name;Address
    ;Test;1Street
    ;Test2;2 Street

  • allanallan Posts: 63,812Questions: 1Answers: 10,516 Site admin
    Answer ✓

    Its basically and array of the data in the table. You can loop over it using join(); to join the information in the rows and then a join for the outer array.

    This is how the export buttons do it.

    Allan

  • prajaktapprajaktap Posts: 3Questions: 1Answers: 0

    Thank you so much Allan for your input!!

    I ran a loop over the array to get the value for each column and then created a new array with these values and called join() method to get it in string format:

    var data = oTable.rows({selected:  true}).data();
    var newarray=[];        
            for (var i=0; i < data.length ;i++){
               alert("Name: " + data[i][0] + " Address: " + data[i][1] + " Office: " + data[i][2]);
               newarray.push(data[i][0]);
               newarray.push(data[i][1]);
               ...
            }
    
    var sData = newarray.join();
    

    Thanks a lot for your help!

This discussion has been closed.