Using a function to generate child row data
Using a function to generate child row data
My child rows have access to objects that I want to iterate through and concatenate into a field using a javascript function. However, when I call the function in the format function for my child rows, they only display "undefined."
My format row is:
function format(d) {
return "<table class='table'>" +
"<tr>"+
"<td>Litigants:</td>" +
"<td>" + list_litigants(data.litigant_list) + "</td>"
"</tr>" +
"</table>"
}
And the function for list_litigants is:
function list_litigants(data){
data.forEach(function (obj){
concat='Name: ' + obj.name + ', Role: ' + obj.role + '; ';
console.log(concat);
return concat;
})
};
The console.log is showing that the function is triggering just fine. However, the string isn't getting passed through to Datatables. Is there an extra step I need to take for it to render properly?
This question has an accepted answers - jump to answer
Answers
try this
format() had an argument of d therefor anything in that data object being passed would never be defined
Thank you for your quick response.
I apologize, that was a typo on my part when I transcribed my code into the browser window.
The
format()
function was using the same variable as the call tolist_litigants
.so:
The console is spitting out the error:
Uncaught TypeError: Cannot read property 'forEach' of undefined
at list_litigants
with the code you've shown, assuming that data is structured properly, there should be no errors, this:
outputs:
Name: foo, Role: bar;
Name: baz, Role: qux;
I would have to say either the error is in the structuring of the data variable or somewhere else in the process
Hello,
Thank you for replying.
The
console.log(concat)
is indeed showing the appropriate results (theconcat
results). So I think the data is structured appropriately:However, the table cell is still showing
undefined
.When I use your formulation (not pulling data from ajax, but using a
let
function), I get the sameundefined
cell with the appropriateconcat
output toconsole.log
as well.At the top of your
format
function could you doconsole.log( JSON.stringify( data ) )
and show us the result from the console?Thanks,
Allan
I have pasted the results of the
console.log( JSON.stringify (data))
below. As you can see, the litigant list is nested within case. I have calledlist_litigants(data.case.litigants)
and the cell still reads asundefined
, even though theconsole.log(concat)
in thelist_litigants
function works fine.The
litigant_list_concat
is the programmatically created version that I have my django model creating right now as a stop gap until I get the Datatables version working.Also, even though this is court data, it is 700 years old so I don't need to obfuscate the litigants' names or details.
It looks like you would want to use:
Allan
I figured out the problem... it was a simple and very stupid mistake on my part. I wasn't returning the
concat
data properly.My function was:
When it should have been:
Without that extra
return concat
, theconcat
variable had no way to return itself from the function.A completely rookie mistake and I feel like an idiot now...
Thank you everyone for your help!
We all do it, all the time, no matter how long we've been programming!
Good to hear you've got it working now.
Allan