how to repeat the column headers dynamically from json data

how to repeat the column headers dynamically from json data

kasaranenisrikasaranenisri Posts: 4Questions: 2Answers: 0
edited April 2019 in Free community support

HI I am new to datatables and i am trying to dynamically add the json data which i got from ajax response to datatbale.

Below is my json data:

{
  Solution: "MobileBroadband",
  Operator: "MTN"
  
  VNF: [
    {
      "vendor": "vendor1",
      "name": "product1",
      "release": "1.0"
    },
    {
      "vendor": "vendor3",
      "name": "prodc3",
      "release": "3.0"
    },
    {
     
      "vendor": "saef",
      "name": "vEPG",
      "release": "2.4"
    }
  ]
 },
{
  Solution: "CLoud",
  Operator: "Airtel"`enter code here`
  
  VNF: [
    {
      "vendor": "vendor1",
      "name": "product1",
      "release": "1.0"
    },
    {
      "vendor": "vendor3",
      "name": "prodc3",
      "release": "3.0"
    }    
  ]
 },

So i want to take the highest no of vnf's (say for example 3 sets) then vnf-vendor,vnf-name,vnf-release columns should repeat thrice.

Below is the expected output.

Answers

  • kasaranenisrikasaranenisri Posts: 4Questions: 2Answers: 0
    edited April 2019

    Pls let me know if this is even possible or not.If not what would be the ideal solution.can i do something like

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

    Hi @kasaranenisri ,

    This here will give you a starting point - it's showing how you can extract values from the object that you have.

    Cheers,

    Colin

  • kasaranenisrikasaranenisri Posts: 4Questions: 2Answers: 0
    edited April 2019

    Thanks @colin .I found this helpfull,But when i implement the same i am getting an error:

    { "data": "vnf",
                        
                      render: function (tempValue, type, row, meta) {
                                     var cell=' '
                                         tempValue.forEach(function(item) {
                                       
                                             cell += JSON.stringify(item);
                                 });
                                 return cell;
                      }
    }
    

    Error i am facing:
    Uncaught TypeError: Cannot read property 'forEach' of undefined
    at render.

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

    Hi @kasaranenisri ,

    You've got lower-case vnf in your columns.data line, in your data structure, and my example, this is capitalised.

    Cheers,

    Colin

This discussion has been closed.