How to select the matching th value for each td value

How to select the matching th value for each td value

AppsbyICEAppsbyICE Posts: 6Questions: 2Answers: 0
edited May 2015 in TableTools

I am using the following selection to select a row of values

"fnRowSelected": function ( nodes ) {
            var oTT = TableTools.fnGetInstance( 'accounts_id' );
            var aSelectedData = oTT.fnGetSelectedData();            
            alert( aSelectedData );
        }       

I get a nice list of values for the selected row. I would like to match each value to its corresponding column header <th> value.
How do I get the parent table header values?

Answers

  • AppsbyICEAppsbyICE Posts: 6Questions: 2Answers: 0

    This is the approach that I took:

    $(document).ready(function() {
        // Instantiate the dataTables object and the TableTools plugin and
        // specify the following options:
        //     single row select
        //     export (not yet working)
        //     buttons copy and csv
        var table = $('#accounts_table').DataTable();
        var tt = new $.fn.dataTable.TableTools( table, {
            "sRowSelect": 'single',
            "sSwfPath": "/swf/copy_csv_xls_pdf.swf",
            "aButtons": [ "copy", "csv" ],
            
            // If the user clicks on a row:
            //     get the selected data
            //     display the edit form
            //     populate the form using the header names and values from the selected row
            "fnRowSelected": function ( nodes ) {
                var oTT = TableTools.fnGetInstance( 'accounts_table' );
    
                // It took me days to realize that fnGetSelectedData() returns a 2D array, since tableTools 
                // can select multiple rows. I've set it to single select so I'm only interested in aSelectedData[0] 
                var aSelectedData = oTT.fnGetSelectedData();
    
                $('#form_account_update').show();
    
                // The headers array enables us to match each value from the selected
                // row to the corresponding form input.
                var headers = [];   
                var form_inputs = [];
                $('#accounts_table th').each(function(i) {
                    headers[i] = $(this).text();
                });
                for (i = 0; i < headers.length; i++) {
                    form_inputs[ headers[i] ] = aSelectedData[0][i];
                } 
                for (var inputfield in form_inputs) {
                    $('#' + inputfield).val(form_inputs[inputfield]);
                }       
            }       
        } );
    
This discussion has been closed.