Solved

How to display records while omitting repetitive fields in AngularJS

Posted on 2016-10-25
6
76 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 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 57

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
What Is Transaction Monitoring and who needs it?

Synthetic Transaction Monitoring that you need for the day to day, which ensures your business website keeps running optimally, and that there is no downtime to impact your customer experience.

 
LVL 57

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

Learn by Doing. Anytime. Anywhere.

Do you like to learn by doing?
Our labs and exercises give you the chance to do just that: Learn by performing actions on real environments.

Hands-on, scenario-based labs give you experience on real environments provided by us so you don't have to worry about breaking anything.

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.
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…
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…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

726 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