在jquery Datatable中获取所选行/行的列值

时间:2016-08-03 04:53:29

标签: jquery dom datatable

我坚持使用以下代码。我想获取数据表中所选行/行的列值。我已经使用了这段代码

DataTable的代码:

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

我想获取列的值

1 个答案:

答案 0 :(得分: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);
});
相关问题