Nested object data
Nested object data
Hi,
I am having some questions about manipulation of Json data into a table. My Json is like this one:
[
{
id: 1
name: 'My Product',
language: [
{
'description': 'My description',
'language': {
name: 'fr'
}
},
{
'description': 'My alternative description',
'language': {
name: 'en'
}
}
]
},
{
id: 2
name: 'My Second Product',
language: [
{
'description': 'My description',
'language': {
name: 'fr'
}
}
]
},
]
And my table is like:
<table id="Table">
<thead>
<th>ID</th>
<th>name</th>
<th>description</th>
</thead>
<tbody>
</tbody>
</table>
And I want to display inside the description field, the description in a certain language, for example were language.name is 'fr'.
As you can see, every product object has a language array, and a language item contains a description, and language information. So for example, for the first product, I should have in my table the following:
<table id="Table">
<thead>
<th>ID</th>
<th>name</th>
<th>description</th>
</thead>
<tbody>
<tr>
<td>1</td>
<td>My Product</td>
<td>My Description</td>
</tr>
<tr>
<td>2</td>
<td>My Second Product</td>
<td>My description</td>
</tr>
</tbody>
</table>
I looked at this example: https://www.datatables.net/examples/ajax/objects_subarrays.html
But I can't find how I could make conditions on the content of that... Maybe I should use createdRow
instead but I am not quite sure...
If someone could light me up, I would appreciate it a lot!
Thanks in advance :)
Answers
You can use a function to define the data in a column.
https://datatables.net/reference/option/columns.data
I would suggest using
columns.render
rather thancolumns.data
since the former doesn't need to act as a setter, making it much easier to use. However, this is the correct thing to do - you need to write a function that will output the data in the format you want.Allan