0

I am trying to create a table that would look like this:

20       23        25       26
734.53   279.20    936.95   584.50

based on this Object:

{
    "20": 734.5339864003384,
    "23": 279.20378246766563,
    "25": 936.9526667106079,
    "26": 584.5060233468447,
    "27": 279.20378246766563,
    "28": 2055.970661511549,
    "29": 1405.0981690224412,
    "30": 549.4917661928141,
    "31": 2329.1464674575695,
    "32": 147.8822594632703,
    "33": 698.0104349592335
}

Obviously the title of the column needs to be dynamic according to the keys of the JSON and the value should correspond.

I have the following code but i am not sure at all if I am on the right track:

JS

 $scope.modal = {};
    $scope.modal.keys = [];
    $scope.modal.data = [];
    for (var key in assets.total) {
      if (assets.total.hasOwnProperty(key)) {
        $scope.modal.keys.push(key);
        $scope.modal.data.push(assets.total[key]);
      }
    }

HTML

<table class="table table-striped">
  <thead>
    <tr>
      <th ng-repeat="th in modal.key">{{th}}</th>
    </tr>
  </thead>
  <tbody></tbody>
    <tr ng-repeat="x in modal.data">
      <td ng-repeat="th in modal.key">{{x[th]}}</td>
    </tr>
  </tbody>
</table>

It is just not working I get a strange result with only one column.

1
  • 1
    Looks like keys is modal.keys misspelled. Should it be modal.keys ? Commented Jul 3, 2018 at 12:06

1 Answer 1

1

You can try this solution

use ng-repeat="(key, value) in Array" for get key value in object.

I have create a demo on Working Demo on Stackblitz

and remove for (var key in assets.total) loop.

html code

<table class="table table-striped">
    <thead>
        <tr>
        <th ng-repeat="(key, value) in data"> {{key}} </th>
        </tr>
    <tr>
        <td ng-repeat="(key, value) in data"> {{ value| number: 2 }} </td>
        </tr>
    </tbody>
</table>

Js file code

$scope.data = {
      "20": 734.5339864003384,
      "23": 279.20378246766563,
      "25": 936.9526667106079,
      "26": 584.5060233468447,
      "27": 279.20378246766563,
      "28": 2055.970661511549,
      "29": 1405.0981690224412,
      "30": 549.4917661928141,
      "31": 2329.1464674575695,
      "32": 147.8822594632703,
      "33": 698.0104349592335
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.