Solved

How to display records while omitting repetitive fields in AngularJS

Posted on 2016-10-25
6
30 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
Comment Utility
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
Comment Utility
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 51

Assisted Solution

by:Julian Hansen
Julian Hansen earned 150 total points
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 51

Expert Comment

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

Expert Comment

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

Author Closing Comment

by:YZlat
Comment Utility
Thank you! Worked perfectly
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

771 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now