Datables 3.1.5 > Accessing Object Properties
Datables 3.1.5 > Accessing Object Properties
u20241229
Posts: 11Questions: 5Answers: 0
Link to test case:
https://live.datatables.net/hixajeto/
Error messages shown:
No error in this case
Description of problem:
I am using this API to try and build a colour explorer:
https://api.color.pizza/v1/docs/
On the test case, I am getting colours from this API example URL - it returns data as an object (I think it's an object):
https://api.color.pizza/v1/?list=mlmc_finnish
This is the function which calls the API:
async function apiReturnColours(list){
const response = await fetch(API + "?list=" + encodeURIComponent(list), {
headers: HEADERS
});
if (!response.ok) throw new Error("HTTP " + response.status);
const data = await response.json();
const colours = data.colors;
console.log({colours});
const table = new DataTable('#colours', {
data: colours,
columns: [
{ data: 'name' },
{ data: 'hex' },
{ data: 'luminance' },
{ data: {
"hsl": "h"
}
},
{ data: 'rgb' },
{ data: 'bestContrast' },
{
data: null,
defaultContent: '',
createdCell: (cell, cellData, rowData) => {
cell.style.backgroundColor = rowData.hex;
}
},
]
});
}
I can't work out how to access the nested object data though, to put into the datatable (e.g. the hsl, rgb, lab and swatchImg attributes)?
I had a bodge-job attempt in the function above - I have had a look at the columns.data doc but I am still stuck.
If anyone has any time to advise please it'd be much appreciated.
Thanks
Jim
This question has accepted answers - jump to:
Answers
See this example for how to access nested objects. I'm not sure what specifically you want to display. I updated your test case to show a couple options:
https://live.datatables.net/kicibutu/1/edit
The HSL column shows how to access a specific nested object. The RGB column shows how to use
columns.renderto access and display each nested object.Kevin
Hi Jim,
Try this: https://live.datatables.net/hixajeto/2/edit .
The key for the
hsl.his to use dotted Javascript notation, while for thergbuse a rendering function to combine the data into a single string. You'll want to do that for thehslas well, but I'll leave that as your homework exercise for the dayAllan
Thank you very much for your help @kthorngren and @allan - what an amazing resource this is!