simple object key to table question, although I can't figure it out.

simple object key to table question, although I can't figure it out.

GerritKuilderGerritKuilder Posts: 7Questions: 2Answers: 0

Hello All,

have been struggling with the following and could not find an elegant solution yet.

A simple mapping of key and value

my data (shortened for clarity):

{
    "data": [
        {
            "kernel": "5.4.0-1022-raspi",
            "openssl": "1.1.1d",
            "systemOpenssl": "1.1.1f",
            "systemOpensslLib": "OpenSSL",
            "node": "10.19.0",
}
    ]
}

My table:

$('#tableVersions').DataTable( {
        "processing": true,
        "serverSide": true,
        "ajax": {
            "url": "/api/table/?name=versions",
            "dataSrc": 'data'
        },
        "columns": [
          { "data": "????"},   //<- part I am struggling with would like to output 'kernel' here, so the key
          { "data": "kernel" }, // <!--- dynamic too, I do get output (the value) so I am getting the data. but would like the value
        ],
    } );

I am struggling to get the key/value working in a simple way, I have seen some examples but they look like hacks.
I know I am getting the correct data (as it outputs the 'kernel' value.

I have the idea that I could do something in the ajax call but, again, no clear examples.

tried "0", etc and looked at all the examples I could find (not limited too:
https://datatables.net/manual/ajax#JSON-data-source
https://datatables.net/examples/ajax/objects.html.

No luck so far,

Any tips welcome.

Kind Regards,

Gerrit Kuilder

This question has an accepted answers - jump to answer

Answers

  • GerritKuilderGerritKuilder Posts: 7Questions: 2Answers: 0

    This is the closest example I could find: http://plnkr.co/edit/kVMprP8BZ6aik2DRjh7D?p=preview&preview

  • colincolin Posts: 15,142Questions: 1Answers: 2,586

    I'm not clear what you want to go into that first column. Column 2 is clear, kernel, but can you say what you expect for 1?

    Colin

  • GerritKuilderGerritKuilder Posts: 7Questions: 2Answers: 0
    edited November 2020

    Hello Colin,

    apologies for missing that part:, the end result should be that the first collum contains the name and the second the value
    So

    "kernel": "5.4.0-1022-raspi",
     "openssl": "1.1.1d",
    

    ends up as

    <tr><td>kernel</td><td>5.4.0-1022-raspi</td></tr>
    <tr><td>openssl</td><td>1.1.1d</td></tr>
    

    I tested with 'kernel to ensure/show that I have data...

    Regards,

    Gerrit Kuilder

  • kthorngrenkthorngren Posts: 20,275Questions: 26Answers: 4,765
    edited November 2020 Answer ✓

    Datatables expects this to be in one row:

            {
                "kernel": "5.4.0-1022-raspi",
                "openssl": "1.1.1d",
                "systemOpenssl": "1.1.1f",
                "systemOpensslLib": "OpenSSL",
                "node": "10.19.0",
    }
    

    You don't need to display all of them but this is one row of data.

    Datatables doesn't support displaying the above in multiple rows. You could restructure your data to match how you want to display it. Something like this:

    {
        "data": [
            {
                "key": "kernel",
                "value": "5.4.0-1022-raspi"
            },
            {
                "key": "openssl",
                 "value": "1.1.1d"
             }
        ]
    }
    

    Then define your columns like this:

            "columns": [
              { "data": "key"}, 
              { "data": "value" }, 
            ],
    

    Kevin

  • GerritKuilderGerritKuilder Posts: 7Questions: 2Answers: 0

    Hi All,

    thanks for the input, I can indeed better do some preprocessing of the data just to keep the tables a little cleaner.

    Regards,

    Gerrit Kuilder

This discussion has been closed.