Solved

How to display records while omitting repetitive fields in AngularJS

Posted on 2016-10-25
6
51 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
6 Comments
 
LVL 27

Assisted Solution

by:BigRat
BigRat earned 100 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 250 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 54

Assisted Solution

by:Julian Hansen
Julian Hansen earned 150 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 54

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Introduction JSON is an acronym for JavaScript Object Notation.  It is a text-string data transport mechanism, capable of representing simple or complex data structures in a consistent and easy-to-read manner.  Similar in concept to XML, but more e…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

777 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