2011-04-15 63 views
9

我有這樣的數據表設置:如何使用jQuery數據表來捕獲選定行中的數據

$(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%' 
    }); 
}); 

當我選擇一排,我想該行的單元格的值複製到某種形式的領域與'sName'屬性命名相同。我有2個問題:

  • 是否有一個TableTools方法用於訪問所選行中單元格的值?像node['sName_whatever'].value會很好。
  • 如何獲取bVisible = false的單元格的值?

ETA解決方案

(而忽略了不重要的東西)

$(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]);   } 
     } 
    }); 
}); 
+0

此行錯誤:`aData = rctable.fnGetData(node);`,應該是`aData = rctable.fnGetData(node [0]); `,因爲`node`是一個數組。 – Bizmarck 2013-04-29 14:24:55

回答

16

我做了以下內容:

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. 
    } 
}); 
+2

你已經指出了我正確的方向,但是我要在`fnRowSelected()`函數內使用`.fnGetData()`函數 – dnagirl 2011-04-15 16:22:24

0

Mutch更好的辦法,而不是掛鉤click事件,只使用jquery和TableTools:

"oTableTools": { 
"sRowSelect": "single", 
"fnRowSelected": function(node) { 
    var row = $(node).find('td'); 
    //all cells 
    $.each(row, function(index, td) { 
     console.log($(td).text()); 
    }); 

    console.log("Specific cell content: " + $(row[2]).text()); 

} 

}

相關問題