Search code examples
jquerydomdatatable

Getting Column Values of Selected Rows/Row in jquery Datatable


I'm stuck with following code. I wanted to get columns values of selected Rows/row in a datatable. i have use this code

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

i want get the values of columns


Solution

  • You can access values from object as,

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