1

I am new to angular and need help for the below- I have a json data as follows in my json file:

{
  "records":
  [
    {
    "date":1619038000,
    "name":"Susan",
    "status":"available"
  },
  {
    "date":1419038000,
    "name":"Vinay",
    "status":"not available"
  },
  {
    "date":1419038000,
    "name":"Ajay",
    "status":"available"
  }
  ],
  "record2":[
    {
    "date":1419037000,
    "name":"Soumya",
    "status":"not available"
  },
  {
    "date":1326439320,
    "name":"Harsh",
    "status":"available"
  },
  {
    "date":1419031000,
    "name":"Gopi",
    "status":"available"
  }
  ]
}

this is my js file:

   angular.module("myApp",[]).controller("Control",function($scope,$http){
      $http.get('myData.json').
      success(function(data){
        $scope.tableData = data.records;

      });
    });

I want to display data in the table from the json in such a way that all data having the same "date" are grouped together and shown jst one date in the table.Could you pls help me with this.I want it as follows:

date name status 16th april susan available 17th april vinay not available ajay available

1
  • Its not an array of objects, its array inside array Commented Apr 14, 2017 at 18:29

2 Answers 2

1

To show the result: GroupBy date, follow the below steps.

STEP 1: Add Moment JS library to your HTML file.

http://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.6.0/underscore-min.js

STEP 2: Update your controller file as below:

  var app = angular.module("myApp",[]);

 app.controller("Control",function($scope,$http){
   $http.get('myData.json').
       success(function(data){
                  $scope.tableData = data.records;
                });
});
app.filter('groupBy', function() {
  return _.memoize(function(items, field) {
        return _.groupBy(items, field);
    }
  );
});

STEP 3: To render the data add the following code to your HTML file.

<ul>
        <li ng-repeat="(date, persons) in tableData | groupBy:'date'">
                {{date * 1000 | date:"dd.MM.y"}}
                <ul>
                    <li ng-repeat="person in persons">
                        {{person.name}}
                    </li>
                </ul>
            </li>
 </ul>

Codepen Link

Sign up to request clarification or add additional context in comments.

2 Comments

can you pls tell me what is .memoize in the controller?
Please refer following link to understand these 2 Underscore.js methods. underscorejs.org
0

You need to use 'ng-repeat' Here is a tutorial and examples for your guide https://www.w3schools.com/angular/ng_ng-repeat.asp

For date, here is some guidance https://www.w3schools.com/angular/ng_filter_date.asp

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.