[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

How to display records while omitting repetitive fields in AngularJS

Posted on 2016-10-25
6
Medium Priority
?
94 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 59

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
New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

 
LVL 59

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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
The well known Cerber ransomware continues to spread this summer through spear phishing email campaigns targeting enterprises. Learn how it easily bypasses traditional defenses - and what you can do to protect your data.
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…

650 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