2016-08-03 90 views
3

我被下面的代碼卡住了。我想要在數據表中獲取選定行/列的列值。我有使用此代碼獲取jQuery數據表中選定的行/行的列值

代碼的數據表:

var table = $('#tableId').DataTable({ 
     "ajax": { 
      "url": "Content", 
      "dataSrc": "", 
      data: {sectionUrl: "", siteUrl: siteurl} 
     }, 
     "columns": [ 
//   {"defaultContent": "<input type='checkbox' name='vehicle' id='checkID'>"}, 
      {"data": "postIMAGE", "render": function (data) { 
        return '<img src=' + data + ' width="154" height="115"/>'; 
       }}, 
      {"data": "postTITLE"}, 
      {"data": "postURL", "render": function (data) { 
        return '<a href=' + data + ' target="_blank"/>' + data + '</a>'; 
       }}, 
      {"data": "postSection"} 

     ] 
    }); 

$('#tableId tbody').on('click', 'tr', function() { 
    $(this).toggleClass('selected'); 
}); 

$('#button').click(function() { 

    var selectedRows = table.rows('.selected').data(); 
    var results = ""; 

    for (i = 0; i < selectedRows.length; i++) { 
     alert(); 
    } 
}); 

我想獲得列

+0

你嘗試過警報(selectedRows [1])? – sunil

+0

是的,它返回一個對象。 Doent知道如何閱讀該對象。我試着用(selectedRows [i] .length)它顯示undefined在提醒 –

+0

你能發佈你得到的對象嗎 – sunil

回答

1

您可以從對象訪問值的值,

$('#button').click(function() { 

    var selectedRows = table.rows('.selected').data(); 

//if you are getting array of objects inside main object 
    alert(selectedRows[0].postTITLE); 
    alert(selectedRows[0].postURL); 

    // if you are getting just plain object you can access it as 
    alert(selectedRows.postTITLE); 
    alert(selectedRows.postURL); 
}); 
+0

thanx工作 –

+0

最受歡迎的好友 – sunil

相關問題