Search code examples
javascripthtmlangularjsangularjs-ng-repeatdynamic-tables

How to dynamically load the nested array attributes in html table using angularjs?


This is the code that I tried but only values of cardno and cardtype why am I not able to access the rest of them? Any suggestions?

<tr ng-repeat="data in myData17.layouts">
  <td ng-show="$index==1">{{data.name}}</td>
  <td ng-show="$index==1">
    <table>
      <tr ng-repeat="card in data.cards">
        <td>{{card.cardNo}}</td>
      </tr>
    </table>
  </td>
  <td ng-show="$index==1">
    <table>
      <tr ng-repeat="card in data.cards">
        <td>{{card.cardType}}</td>
      </tr>
    </table>
  </td>
  <td ng-show="$index==1">
    <table>
      <tr ng-repeat="card in data.cards.ports">
        <td>{{card.portNo}}</td>
      </tr>
    </table>
  </td>
  <td ng-show="$index==1">
    <table>
      <tr ng-repeat="card in data.cards.ports">
        <td>{{card.portName}}</td>
      </tr>
    </table>
  </td>
  <td ng-show="$index==1">
    <table>
      <tr ng-repeat="card in data.cards.ports">
        <td>{{card.portType}}</td>
      </tr>
    </table>
  </td>
  <td ng-show="$index==1">
    <table>
      <tr ng-repeat="card in data.cards.ports">
        <td>{{card.portspeed}}</td>
      </tr>
    </table>
  </td>
  <td ng-show="$index==1">
    <table>
      <tr ng-repeat="card in data.cards.ports">
        <td>{{card["ds-scheduler-node-profile"]}}</td>
      </tr>
    </table>
  </td>
</tr>

This is the my data.

      {
  "name": "twoCardOneEthportAndOne10G",
  "cards": [{
    "cardNo": "1",
    "cardType": "Ethernet",
    "ports": [{
      "portNo": "1",
      "portName": "LAN1",
      "portType": "ethernetCsmacd",
      "portspeed": "eth-if-speed-1gb",
      "ds-scheduler-node-profile": "default"
    }]
  },
    {
      "cardNo": "10",
      "cardType": "Ethernet",
      "ports": [{
        "portNo": "1",
        "portName": "10GE",
        "portType": "ethernetCsmacd",
        "portspeed": "eth-if-speed-10gb",
        "ds-scheduler-node-profile": "default"
      }]
    }]
}

Solution

  • data.cards.ports isn’t a thing.

    The ports property is of a singular card and cards is an array.

    It would have to be accessed like so:

    data.cards[0].ports
    

    So, how would this be structured in your code? You would need something like this (this isn’t a solution to your exact implementation just a visual guide to help you access your data in the way you require)

    <div ng-repeat=“data in myData17.layouts”>
        {{ data.name }}
        <div ng-repeat=“card in data.cards”>
            {{ card.cardNo }}
            <div ng-repeat=“port in card.ports”>
                {{ port.portNo }}
            </div>
        </div>
    </div>
    

    Hope that this gives you a better understanding and good luck