Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to capture the data in a selected row using jQuery DataTables

I have this datatable setup:

$(document).ready(function() {
    $('#RectifiedCount').dataTable( {
        "bJQueryUI": true,
        "bProcessing": true,
        "aLengthMenu": [[10, 25, 50, -1], [10, 25, 50, "All"]],
        "bStateSave": true,
        "sDom": '<"H"if>tr<"F"lTp>',
        "aoColumns":[
                     {'sname':'count_id', 'sType':'numeric', 'bVisible':false},
                     {'sName':'count_type', 'sType':'string','bVisible':true},
                     {'sName':'count_date', 'sType':'date','bVisible':true},
                     {'sName':'count_count', 'sType':'numeric','bVisible':true},
                     {'sName':'count_notes', 'sType':'string','bVisible':true}
                     ],
        "oTableTools": {
            "sRowSelect": "single",
            "sSwfPath": "media/swf/copy_cvs_xls_pdf.swf",
            "aButtons": [ {sExtends :'select_none' , 'sButtonText':'Clear Selection'}],
            "fnRowSelected": function(node){
                var s=$(node).children();
                if($(s[0]).text()=='Delivery') return ;
                $('select[name="count_type"]').val($(s[0]).text());
                $('input[name="count_date"]').val($(s[1]).text());
                $('input[name="count_count"]').val($(s[2]).text());
                $('textarea[name="count_notes"]').val($(s[3]).text());
            }
        },
        'sScrollX':'100%'
    });
});

When I select a row, I want to copy the values of the cells of that row into some form fields that are named the same as the 'sName' attributes. I have 2 questions:

  • is there a TableTools method for accessing the value of a cell in a selected row? Something like node['sName_whatever'].value would be nice.
  • how can I get the value of the cells where bVisible=false?

ETA solution

(leaving out the unimportant stuff)

$(document).ready(function() {
    rctable=$('#RectifiedCount').dataTable( {
        "aoColumns":[
                     {'sname':'count_id', 'sType':'numeric', 'bVisible':false},
                     {'sName':'count_type', 'sType':'string','bVisible':true},
                     {'sName':'count_date', 'sType':'date','bVisible':true},
                     {'sName':'count_count', 'sType':'numeric','bVisible':true},
                     {'sName':'count_notes', 'sType':'string','bVisible':true}
                     ],
        "oTableTools": {
            "sRowSelect": "single",
            "fnRowSelected": function(node){
                aData = rctable.fnGetData(node); //nice array of cell values
                if(aData[0]=='Delivery') return ;
                $('select[name="count_type"]').val(aData[0]);
                $('input[name="count_date"]').val(aData[1]);
                $('input[name="count_count"]').val(aData[2]);
                $('textarea[name="count_notes"]').val(aData[3]);            }
        }
    });
});
like image 469
dnagirl Avatar asked Apr 15 '11 14:04

dnagirl


1 Answers

I did the following:

 oTable = $('#RectifiedCount').dataTable( ....);

 $('#RectifiedCount tbody tr').live('click', function (event) {        
    var aData = oTable.fnGetData(this); // get datarow
    if (null != aData)  // null if we clicked on title row
    {
        //now aData[0] - 1st column(count_id), aData[1] -2nd, etc. 
    }
});
like image 99
a1ex07 Avatar answered Oct 17 '22 07:10

a1ex07