3

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

5
  • have you tried alert(selectedRows[i]) ? Commented Aug 3, 2016 at 5:03
  • yeh it's return an object. doent know how read that object. And I tried with ( selectedRows[i].length ) it shows undefined in alert Commented Aug 3, 2016 at 5:06
  • can you post object which you are getting Commented Aug 3, 2016 at 5:28
  • alert shows [object Object] Commented Aug 3, 2016 at 5:31
  • see the answer posted Commented Aug 3, 2016 at 5:55

2 Answers 2

2

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);
});
Sign up to request clarification or add additional context in comments.

Comments

0

You can access selected rows in server side rendering by cols name from Datatable

$('#button').click(function (){
   let rows = $('#tableElement').rows( { selected: true } ).data().map(x=>x.cols_name).toArray();
         console.log(rows);
  });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.