Utilize Angular to organize data in a single-dimensional array

In my project, I am using Angular and Bootstrap to create a dynamic data grid where users can edit the data. The dataset consists of an array of objects, with each object containing a non-unique group property to categorize records.

For example:

[
  {
    id: 1,
    group: 'A',
    value: 'John'
  }, {
    id: 2,
    group: 'A',
    value: 'Jake'
  }, {
    id: 3,
    group: 'B',
    value: 'Jack'
  }
]

I want Angular to generate output like this:

<div class="row group">
  <div class="col-md-12">A</div>
</div>
<div class="row sample">
  <div class="col-md-4">1</div>
  <div class="col-md-8">John
<div>
...

I attempted to use ng-repeat, but it only allowed nesting arrays within each other. This resulted in the {{ group }} being at the top level and {{ elementOfAGroup }} as its child. However, I need the final markup to be a straightforward collection of DOM elements.

I searched for solutions online, but most were basic components (directives) specifically for creating tables.

Answer №1

To achieve this, I utilized a custom "uniq" filter:

<!doctype html>
<html>
    <head>
        <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.0/angular.js"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/angular-filter/0.1.1/angular-filter.js"></script>
    </head>
    <body ng-app="plunker" ng-controller="MainCtrl">
        <div class="row group" ng-repeat="data in dataset | uniq: 'group'">
            <h3>{{data.group}}</h3>
            <div class="col-md-12" ng-repeat="child in dataset | filter: { group: data.group }">{{child.value}}</div>
        </div>
    </body>
    <script>
        var app = angular.module('plunker', ['angular.filter']);

        app.controller('MainCtrl', ['$scope', '$sce', function($scope, $sce) {
            $scope.dataset = [
                {
                    id: 1,
                    group: 'A',
                    value: 'John'
                }, {
                    id: 2,
                    group: 'A',
                    value: 'Jake'
                }, {
                    id: 3,
                    group: 'B',
                    value: 'Jack'
                }
            ];

        }]);
    </script>
</html>

Keep in mind that it might have performance issues with large datasets.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Developing an interactive menu for mobile devices utilizing a combination of JSON, HTML, JavaScript, and CSS

Creating a custom dynamic menu for mobile platforms using HTML, JavaScript, and CSS with a JSON object downloaded from the server. Not relying on libraries like jQuery. I've come across advice stating that "document.write" should not be used in event ...

How to establish a session in CodeIgniter with the help of JavaScript

Currently, I am trying to save the session in jQuery. Specifically, I am attempting to store all the IDs that are checked via checkboxes in the export_type variable and then save them in the session. However, when running the following code snippet, I en ...

I am looking for solutions to decrease the size of my AngularJS bundle when using Webpack. Can anyone provide

I've been working on developing an AngularJS app (v1.7.x) and I'm facing a challenge with the large production file size. The original dependency in node_module is 1.30 MB and 170 kB (minified). After adding just a basic console.log, the build f ...

Incorporate JSON and JavaScript into your website template

Currently, I am in the process of developing my online resume using a free template that I found and downloaded. The template incorporates bootstrap and showcases a progress bar indicating my skills. However, I want to take it a step further by dynamically ...

Struggling to fetch information from API through Express in NodeJS with MongoDB, currently loading

I am in the process of creating a Rest API using Node.js, Express, and MongoDB. Currently, I am running it on my local host:3000. When I attempt to restart and run the server, I utilize the route http://localhost:3000/drinks To send HTTP requests, I use P ...

Ignoring TypeScript overloads after the initial overload declaration

Issue An error occurs when attempting to call an overload method for a specific function. The call only works for the first defined overload, causing an error if the call is made to the second overload with mismatched typing compared to the first overload ...

Learn how to successfully carry on with event.preventdefault in JavaScript

Is there a way to create a modal that prompts the user to confirm if they want to leave the page without committing changes? These changes are not made within a <form> element, but rather on a specific object. I've attempted to use both $route ...

Using multiple header tags in HTML

I have been tasked with developing a webpage featuring a prominent header at the top of the page. This header will include the navigation bar, logo, and website title. As the user begins to scroll, I want the header to transform into a compact version that ...

Is it possible to use the LINK method/verb with AngularJS $http?

Can the $http method in AngularJS be used to send ajax requests using the LINK/UNLINK method? I have checked the AngularJS documentation and only found a list of available methods: $http.get $http.head $http.post $http.put $http.delete $http.jsonp $http. ...

Guide to converting JSON data into object structures

Question - How do I convert JSON data into objects? You can find the JSON data here Details - After successfully connecting to the API and retrieving the JSON data, I am looking to map two arrays data.hourly.time[] and data.hourly.temperature_2m[] into a ...

Possible undefined object in React Redux

I'm encountering a Typescript issue where Redux object I am utilizing is potentially undefined, even though I have not specified its type as potentially being undefined or set it to be undefined anywhere in my code. /redux/globalSettings/actions.ts ...

Secure access using Android and Node.js

Recently, I have been working on building an Android app with Node.js on the server and using PostgreSQL as the database. A few days back, I implemented a simple authentication feature on a website using Passport. Now, my goal is to maintain the session e ...

the buttonclick won't pause the timer

I'm having trouble with the timer function when a button is clicked. The startpause() method sets the start and stop timers, but when I click the button rapidly multiple times, the timer starts to jump by 2-3 seconds. It seems like more than one timer ...

Automatically adjust multiple images on an HTML webpage

Just starting out with html programming here. Looking to add top margin using a span class, any tips on how to tackle this issue? ...

Problems with implementing JavaScript code in a WebView

I am currently working on an android WebView project where I have managed to change the background color to orange with this code snippet. @Override public void onPageFinished(WebView view, String url) { wv.loadUrl("jav ...

What is the method for generating a new array from an existing array?

I am trying to transform the data in this format: var routes: [{ locations: [ [40.749825, -73.090443], [42.743244, -71.594375], [37.058435, -74.903842] ], color: 'red', opacity: 1 }] from this initial format: var locations: [ ...

Attempting to showcase the information in my customized SharePoint Online list through a Web Part Page utilizing AngularJS

<script> //AngularJS Code goes here var appVar = angular.module('listApp', ['ngRoute']); appVar.controller("controller1", function($scope){}); function FetchEmployeeData($scope, EmployeeList){ var reque ...

Creating Stylish Checkboxes with Bootstrap

I'm attempting to customize a checkbox to have a specific appearance. example of desired checkbox design Here is what I have tried so far: <div class="col-2"> <label for="WitholdingTax">Charge</label> <div class="input-g ...

Steps to update the background color of a highlighted ItemList component in Material-Ui

I recently implemented a selectable component using Material-UI let SelectableInfiniteList = makeSelectable(Infinite); and added ListItems inside. Now, I am trying to customize the default gray color of a selected item. I attempted to do this using a cla ...

Clones are made sortable instead of arranging them in a specific order

I have a list that can be sorted with some customizations. However, I am facing an issue where the items in the list get duplicated every time I try to sort them. When I pull one item to sort it, another copy gets added automatically. I am struggling to u ...