0

In the past I've always used this to get a hidden column's data. I would hide the column with a css class, but the responsive feature doesn't work well with these.

var td = $('td', this);
var ID = $(td[0]).text();

So I found an alternative, by hiding the columns with these classes with the responsive feature.

"columnDefs": [
    //Responsive classes
    { className: 'never', targets: 0 }, //Hide on all devices
    { className: 'all', targets: 1 },   //Show on all devices
]

and then I use either one of these.

var rowData = oTable1.fnGetData(this);
var rowData = oTable1.api().row(this).data();

//Grab the first indexed item in the list
var ID = rowData[0];

That works well if you don't have an AJAX source. It will return a comma separated list of the row data. However, when I try to use this with an AJAX source I just get [object Object] back (instead of a comma separated list) if I output the rowData variable in an alert.

How do I get the row data out of a table with an AJAX source?

3 Answers 3

1

It seem to be stored as string so [1, 2, 3] became [object Object] when you turn it into string. Do yourString = yourList.join(',') and store yourString to keep the coma-separated string.

For an object:

yourString = (function () {
  var list = [];
  for(var i in yourList)
    if(yourList.hasOwnProperty(i))
      list.push(yourList[i]);
  return list.join(',');
})();

The function is not needed, it's just to limit the variables scope.

Sign up to request clarification or add additional context in comments.

4 Comments

I think I'm getting closer, but I'm getting this TypeError: rowData.join is not a function
If I output to console, I can see the object formatted as Object { id="123456", full_name="Sarah Smith", Last_name="Smith"} for example. I just need to pull out those individual values.
I edit it with an object example. If it's not what you want, please precise exactly what you have in input and what you what as output.
Thanks a lot for your help. Looking in the console log helped me figure this out. I thought it was just a comma separated string I needed, which is what you helped me create, but I need an array. What was being returned without the AJAX source was an array. That's why I could use something like alert(rowData[0]); to get the first value. I was confused looking at rowData in an alert instead of the console log. So, with an Ajax source,my function returns an object instead of an array, which I then convert to an array so I can get an individual value. I'll explain more in my answer.
0

I ended up using an answer I found here. Converting a JS object to an array

I can pull the entire row data from the table with this.

 var rowData = oTable1.api().row(this).data();

In the console log I can see that it returns a javascript object like this.

 Object { id="123456", full_name="Samuel Smith", Last_name="Smith" }

I use this function to convert the object into an array.

var array = $.map(rowData, function (value, index) {
        return [value];
    });

In the console log, my array would appear like this.

["123456", "Samuel Smith", "Smith"]

I can then extract any item from the array like this.

alert(array[0]);

Comments

0

Simplifying madvora's example:

var rowData = oTable1.api().row(this).data().to$();
rowDataArray = rowData.toArray();

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.