Error on loading/rendering data from array of objects
Error on loading/rendering data from array of objects
latinunit
Posts: 73Questions: 12Answers: 0
Here is my array of objects containing data structure
[{"firstName":"David Llanos","_schema":"nms:recipient"},{"firstName":"David Test","_schema":"nms:recipient"},{"firstName":"David Test","_schema":"nms:recipient"},{"firstName":"David Test","_schema":"nms:recipient"},{"firstName":"D Test 2","_schema":"nms:recipient"}]
Here is my script
```
var rcpObjData = document.controller.getValue('/ctx/vars/recipients')
alert(rcpObjData);
$('#recipients').DataTable({
data: rcpObjData,
columns: [
{ data: 'firstName' },
],
});
```
here is my html div
<table class="display" id="recipients" style="width: 100%;"></table>
It wont render or recognise firstName
This question has an accepted answers - jump to answer
Answers
I'm not familiar with
document.controller.getValue()
but suspect it might be an asynchronous process. If it is then the Datatables initialization is happening before the data is returned. Is there some sort of success callback you can use to initialize and load the Datatables data?It doesn't look like you have a
thead
defined in HTML for thetable
. If not usecolumns.title
to define the column headers.The problem could be one or both of these issues.
Kevin
Here is my script
jQuery( document ).ready(function() {
});
</script>
The init is being done using a call back on success.
You probably don't want to use
JSON.stringify(objects)
. Datatables expects a Javascript array but its turned into a string usingJSON.stringify(objects)
. I would say that lines 8 and 12 aren't needed and you can just useobjects
in line 15, for example,data: objects,
.Kevin
I changed my script but still the error is the same
Did you read my last post?
You are still using
JSON.stringify(response.data)
which won't work!Kevin
Ok, is fixed by parsing the string as json
JSON.parse(rcpObjData),
You should be able to just use the response object without the need to JSON.stringify() followed by JSON.parse(). This just adds two steps that result in the same value.
Kevin
The thing is, on my application when I grab the value from the following application variable, is as a string, which is why i need to convert it to json
var rcpObjData = document.controller.getValue('/ctx/vars/recipients')
But you are correct, just using the response.data is enough and no conversion needed, thanks
You are making it a string in this statement:
Kevin