Populating Table with an Object Array

Populating Table with an Object Array

MK01111000MK01111000 Posts: 3Questions: 1Answers: 0
edited February 2018 in Free community support

Hello all,

I'm trying to get my table populated with data from an object array.
Arrays are as follows:

COLUMNS
https://ibb.co/dEWpDH

DATA
https://ibb.co/n69y7c

What I've tried so far is:
$("#example").DataTable({
"data": data,
"columns": columns
});

I can't it to work. any ideas?

This question has an accepted answers - jump to answer

Answers

  • colincolin Posts: 15,237Questions: 1Answers: 2,598

    Take a look at this example, it has something very similar to your data structure. It'll show how to define those columns and data format.

    Cheers,

    Colin

  • MK01111000MK01111000 Posts: 3Questions: 1Answers: 0

    Thanks Colin, that comes really close.
    I guess the only difference is that each "row" of data in your example is captured in curly braces, while mine is not.

    And to be honest I have no idea to circumvent this obstacle. any ideas?

    The code that creates the object array from a google firebase database is as follows:

    var data = [];
    firebase.database().ref("DataSet-Schedule").once("value", function(snap){
    snap.forEach(snapshot => {
    Object.keys(snapshot.val()).map(k => {
    data.push(Object.assign({}, {[k]:snapshot.val()[k]}))
    })
    })
    })
    console.log(data)

  • colincolin Posts: 15,237Questions: 1Answers: 2,598
    Answer ✓

    Yep, the data should be an array of objects, so you'll need to get those curly braces into your output format. I don't understand that code, I'm afraid, but it looks like it would need to go around the forEach block.

  • MK01111000MK01111000 Posts: 3Questions: 1Answers: 0

    Thanks for your help Colin, I'll look for a solution.

This discussion has been closed.