Cannot access first cell data, returns object Object (rendered cell)
Cannot access first cell data, returns object Object (rendered cell)
Hello, I'm struggling to access the data from a row's first cell, here's the code:
var table = $('#dataTable').DataTable({
"processing": true,
"serverSide": true,
"lengthChange": false,
"pageLength": 50,
"dom": 'lrtip',
"ajax": "db/sales_invoices.php",
"columns": [
{ "data": null, render: function ( data, type, row ) { return data.prefix+data.number; } },
{ "data": 2 },
{ "data": 3 },
{ "data": 4 },
{ "data": null, render: function ( data, type, row ) { return data.total+" "+data.currency; } }
]
});
Now, within the table I'm using jQuery UI dialog and I want the title of that dialog to be the rendered first cell, as this is the invoice number (generated with a prefix).
The following code just returns object Object:
var thisTitle = table.row( this ).cell(this,0).data();
However, if I change 0 to 1, it will return the data from the second column. I'm so confused it's driving me crazy.
When I check the console log for the .cell(this,0), it returns the data of the entire row and I don't know how to access the invoice prefix and number within that data... this is what console.log(thisTitle) returns:
Object {2: "Test", 3: "19/04/15 - 10:59:54", 4: "UNPAID", DT_RowId: "row_14", prefix: "INV_", number: "00001", total: "120.00", currency: "GBP"}
I just need 'prefix' and 'number' from that data string but regardless of what I try, I can't access it - I'm sure there's some simple solution, could anyone please help? Thank you so much in advance.
This question has an accepted answers - jump to answer
Answers
So the issue is that the first cell doesn't have any data as such - you've got
data: null
. However, DataTables is rendering content into the cell, which you can obtain using thecell().render()
method:Note that you don't need to use
row( this )
for this use case - thecell()
selector is enough.Allan
Hi Allan, that's perfect. I just wanted to say thank you, I came here about 4 years ago looking for support for one of my clients and you were very quick to respond then too, it's nice to see that you're still as dedicated. I really appreciate your help and I will send some more support your way at the end of the month. Thanks again.
Hi,
You are very welcome :-). Great to hear you are still using DataTables!
Allan