?
Solved

How to display records while omitting repetitive fields in AngularJS

Posted on 2016-10-25
6
Medium Priority
?
84 Views
Last Modified: 2016-11-02
I have the following record I am retrieving from Web API:
    FullRecord
    _______
    RecordID
    RecordDate
    PersonName
    RecordNote
    LineOrder

Open in new window

The data comes from a Web API in JSON format.

The content is something like this:

    RecordID	RecordDate	PersonName	RecordNote	LineOrder
	1	        10/25/2016 	John Doe	test1		1
	1	        10/25/2016 	John Doe	test2		2
	1	        10/25/2016 	John Doe	test3		3
	2	        10/25/2016	Jane Doe    	testing23	1
	2	        10/25/2016	Jane Does	testing27	2

Open in new window


I am displaying it withi my AngularJS application as follows:

     <tbody>
              <tr ng-repeat="Record in Records">
                        <td>{{Record.RecordID}}</td>
                        <td>{{Record.RecordDate}}</td>
                        <td>{{Record.PersonName}}</td>
                        <td>{{Record.RecordNote}}</td>
                        <td>{{Record.LineOrder}}</td>
     		</tr>
     </tbody>

Open in new window


This way the first 3 fields are repeating for each record with the same record ID. I was wondering if I can somehow only display RecordID, RecordDate, and Person Name in a row if it is not repeating.

For example:
 
    RecordID	RecordDate	PersonName	RecordNote	LineOrder
	1	10/25/2016 	John Doe	test1		1
						                test2		2
						                test3		3
	2	10/25/2016	Jane Doe   	testing23	1
						                testing27	2

Open in new window


How can I accomplish that with Angular?
0
Comment
Question by:YZlat
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 27

Assisted Solution

by:BigRat
BigRat earned 400 total points
ID: 41860137
Well you want the same number of records as before but the fields RecordId, RecordDate and personName to be surpressed, so the best way would be to construct a Javascript array of objects where the objects have the relevant fields missing. Presumably if you are loading via a service then either add the function to the service or create the array in scope after calling the service. The Javascript is straightforward - one looks at the previous array entry to see if the RecordId is the same or not.

The only other problem is that the table cells would refer to a missing object field. So the best way would be to create, in the new array, the fields with a blank or a non-breaking space so that the table gets rendered properly.
1
 
LVL 82

Accepted Solution

by:
leakim971 earned 1000 total points
ID: 41860622
Test page : https://jsfiddle.net/fg7esfmx/

<table ng-app="myApp">
<tbody ng-controller="MyCtrl">
              <tr ng-repeat="Record in Records">
                        <td><span ng-show="Records[$index-1].RecordID != Record.RecordID">{{Record.RecordID}}</span></td>
                        <td><span ng-show="Records[$index-1].RecordID != Record.RecordID">{{Record.RecordDate}}</span></td>
                        <td><span ng-show="Records[$index-1].RecordID != Record.RecordID">{{Record.PersonName}}</span></td>
                        <td>{{Record.RecordNote}}</td>
                        <td>{{Record.LineOrder}}</td>
     		</tr>
     </tbody>
</table>

Open in new window


angular.module("myApp", [])
.controller("MyCtrl", ["$scope", function($scope) {
	$scope.Records =
    [
      {RecordID:1, RecordDate:"10/25/2016", PersonName:"John Doe", RecordNote:"test1", LineOrder:1 },
      {RecordID:1, RecordDate:"10/25/2016", PersonName:"John Doe", RecordNote:"test2", LineOrder:2 },
      {RecordID:1, RecordDate:"10/25/2016", PersonName:"John Doe", RecordNote:"test3", LineOrder:3 },
      {RecordID:2, RecordDate:"10/25/2016", PersonName:"Jane Doe", RecordNote:"test23", LineOrder:1 },
      {RecordID:2, RecordDate:"10/25/2016", PersonName:"Jane Does", RecordNote:"test27", LineOrder:1 }
    ];


}]);

Open in new window

2
 
LVL 58

Assisted Solution

by:Julian Hansen
Julian Hansen earned 600 total points
ID: 41860643
An implementation of BigRat's suggestion
HTML
   <table class="table">
      <tr ng-repeat="Record in ctrl.Records">
        <td>{{Record.RecordID}}</td>
        <td>{{Record.RecordDate}}</td>
        <td>{{Record.PersonName}}</td>
        <td>{{Record.RecordNote}}</td>
        <td>{{Record.LineOrder}}</td>
      </tr>
    </table>

Open in new window

Angular
<script>
(function(angular) {
  'use strict';
  angular.module('app',[])
    .controller('mainCtrl', mainController)
    .factory('RecordService', recordSVC);

  function mainController(RecordService)
  {
    var ctrl = this;
    
    RecordService.query().then(function(items) {
      var result = [];
      var n;
      for(var i=0;i < items.length;i++) {
        n = { };
        for(var j in items[i]) {
          var v = items[i][j];
          n[j] = (i ==0  || items[i][j] != items[i-1][j]) ? items[i][j] : '';
        }
        result.push(n);
      }
      ctrl.Records = result;
    });
  }
  
  function recordSVC($q)
  {
    var data = [{
      "RecordID":1,
      "RecordDate":"10/25/2016 ",
      "PersonName":"John Doe",
      "RecordNote":"test1",
      "LineOrder":"1"
    },{
      "RecordID":1,
      "RecordDate":"10/25/2016 ",
      "PersonName":"John Doe",
      "RecordNote":"test2",
      "LineOrder":2
    },{
      "RecordID":1,
      "RecordDate":"10/25/2016 ",
      "PersonName":"John Doe",
      "RecordNote":"test3",
      "LineOrder":3
    },{
      "RecordID":2,
      "RecordDate":"10/25/2016",
      "PersonName":"Jane Doe",
      "RecordNote":"testing23",
      "LineOrder":1
    },{
      "RecordID":2,
      "RecordDate":"10/25/2016",
      "PersonName":"Jane Does",
      "RecordNote":"testing27",
      "LineOrder":2
    }];
    
    function query()
    {
      return $q(function(resolve, reject) {
        resolve(data);
      });
    }
    
    return {
      query: query
    }
  }
})(window.angular);
</script>

Open in new window

Working sample here
0
Major Serverless Shift

Comparison of major players like AWS, Microsoft Azure, IBM Bluemix, and Google Cloud Platform

 
LVL 58

Expert Comment

by:Julian Hansen
ID: 41860651
I prefer Leakim's solution - does not change the underlying recordset.
0
 
LVL 27

Expert Comment

by:BigRat
ID: 41860675
That is true Julien, but I would have done this server side if I could.
1
 
LVL 35

Author Closing Comment

by:YZlat
ID: 41870582
Thank you! Worked perfectly
0

Featured Post

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

We will take a look at the d3.js library for visualizations.  I will provide a walkthrough of a short bar graph example as well as introduce you to the d3.js API.  We also will explore links of other examples and further information regarding SVG's.
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses
Course of the Month12 days, 1 hour left to enroll

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question