2016-08-03 94 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

相关问题