0

So I made a simple JavaScript project using AngularJS,

Here's my controller

angular.module('myApp')
  .controller('BillDetail3Ctrl', function ($scope, billDetail3) { 

    $scope.tableData = [];
    $scope.hourCount = 0; 

    billDetail3.getMetList().then(function(data){    

        var no = 0;
        for (var i = 0; i < data.length; i++) {
            for (var j = 0; j == data[0].d.currA.length; j++) {
                $scope.tableData.push({
                    'No': ++no,
                    '_id': data[i]._id,
                    'hourtag': data[i].d.hourtag,  
                    'currA': data[i].d.currA[j]  
                }); 
            }
        }

        $scope.hourCount = no;
    }, function(){

    });
  });

Those controller was fetching data from this JSON: http://amr2.mybluemix.net/getmet/list This is the format of JSON:

[
  {
    "_id": "100010001_20082015_0",
    "_rev": "8-e72619f2c6c05aac2e3692f1ea77eb0f",
    "d": {
      "uSN": "100010001",
      "timetag": 20082015,
      "hourtag": 0,
      "currA": {
        "0": 0,
        "1": 2,
        "2": 5,
        "3": 6,
        "4": 2.3
      },
      "currB": {
        "0": 10,
        "1": 23,
        "2": 52,
        "3": 61,
        "4": 12.3
      },
      "currABC": {
        "0": 0.1,
        "1": 2.2,
        "2": 5.2
      },

Here's my view:

<tr ng-repeat="pop in tableData">
       <td>{{pop.No}}</td>
       <td>{{pop._id}}</td>
       <td>{{pop.hourtag}}</td>
       <td>{{pop.currA}}</td>  //This is doesn't work
 </tr>

But this code <td>{{pop.currA}}</td> doesn't work. So, in JSON I have hourtag = 0, hourtag = 1, etc. In each hourtag, I have "currA": { "0": x, "1" = x, etc. The idea was I want to call all of the currA value which is in the first "0" in every hourtag and put that in my table. How can I do that?

Here's the conclusion enter image description here

2 Answers 2

1

Should it be something like this? http://jsfiddle.net/Lt7aP/1033/ (simplified example)

The key change is in for loop:

for (var i = 0; i < data.length; i++) {
    var curHourtag = data[i].d.hourtag;
    $scope.tableData.push({
        'No': ++no,
        '_id': data[i]._id,
        'hourtag': curHourtag,
        'currA': data[i].d.currA[curHourtag]
    }); 
}
Sign up to request clarification or add additional context in comments.

3 Comments

Yes, little bit closer mate. The hourtag was showing the right result, but currA still showing 1 right result. the others incorrect. in each hourtag I want to show the result from it's currA[0]. Please see my JSFiddle: jsfiddle.net/liverpool06/rLnj7yn7
In your fiddle Angular is not active ? Here is same code with angular engaged: jsfiddle.net/rLnj7yn7/1
Thanks man, really appreciate it. I modified your code a bit and it works.
0

SOLVED Thanks for @Dfr answer, I modified 'currA': data[i].d.currA[curHourtag] line to 'currA': data[i].d.currA[0] and it works fine as I expected.

for (var i = 0; i < data.length; i++) {
    var curHourtag = data[i].d.hourtag;
    $scope.tableData.push({
        'No': ++no,
        '_id': data[i]._id,
        'hourtag': curHourtag,
        'currA': data[i].d.currA[0]
    }); 
}

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.