Displaying multiple arrays using ng-repeat

My task is to display a list organized by date, but the problem is that the list is not sorted and I can't figure out when the date changes. This situation is similar to having the following Json:

list = 
{name: first, date: 2014-05-21}, {
{name: second, date: 2014-05-20}, {
{name: third, date: 2014-05-21}

I want to generate the corresponding html structure:

<li><h3>2014-05-21</h3>
 <ul>
  <li>first</li>
  <li>third</li>
 </ul>
</li>
<li><h3>2014-05-20</h3>
 <ul>
 <li>second</li>
 </ul>
</li>

Any suggestions on how to achieve this?

I attempted to create an Array of arrays using two ng-repeat loops like so:

for (i in list){
 if ($scope.listToShow[list.date] == undefined) {
     $scope.listToShow[list.date] = newArray();
 }
 $scope.listToShow[list.date].push(list[i]); 
}

However, I'm struggling to actually display the result.

Answer №1

Take a look at this helpful article, it explains exactly what you need to do.

For another approach, you can utilize the built-in directives

ng-repeat-start and ng-repeat-end
to accomplish the same result. Check out this link for more information.

Answer №2

I was able to create a functional example. It may not be very advanced and there is room for improvement, but you can grasp the basic concept by examining the code. The key is to organize your data (without using any fancy Angular features, just plain old JavaScript) and then present the organized data in the view. I had to maintain a keys property for the grouped object because it seems that AngularJS views do not support Object.keys(obj).

You can check it out here

EDIT: I have updated it with the (key,obj) in grouped syntax, eliminating the need for the keys array.

Answer №3

Exploring an array of arrays within a table led me to some interesting results, although my approach may not be the most efficient.

//controller
$scope.list = [
    [
        {name: 'first', date: '2014-05-21'},
        {name: 'lala', date: '2014-07-32'},
        {name: 'fada', date: '2014-07-32'}
    ], 
    [
        {name: 'second', date: '2014-05-20'},
        {name: 'dada', date: '2014-54-32'},
        {name: 'mama', date: '2014-07-32'}
    ], 
    [
        {name: 'third', date: '2014-05-21'},
        {name: 'nana', date: '2014-78-32'},
        {name: 'rara', date: '2014-07-32'}
    ]
];

//view
<table>
    <tr class="parent" ng-repeat-start="(key, item) in list">
        <td>{{ item[0].name }}</td>
        <td>{{ item[0].date }}</td>
    </tr>
    <tr class="child" ng-repeat-end="", ng-repeat="part in list[key].slice(1, list[key].length)">
        <td>{{ part.name }}</td>
         <td>{{ part.date }}</td>
    </tr>
</table>

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

Angular deep nested router interface

How can I set up nested views in Angular for the following routes? /#/app/dashboard /#/app/product/ /#/app/product/new Here is my current route configuration: $stateProvider .state('app',{ url: '/app', templateUrl ...

Achieving Vertical Centering of Text in Bootstrap 5 Buttons with Flex Box to Prevent Overlapping Icons

How can I prevent the text on a Bootstrap 5 button with horizontally and vertically centered text, along with a right aligned icon, from overlapping when it wraps? Additionally, how do I ensure that the icon stays vertically centered when the button text w ...

What is the most efficient way to insert values into a multidimensional array?

Hello everyone, I am facing an issue and need some assistance. I am trying to insert values into specific array keys, but I am getting a lot of null values inserted. Here is the format I am aiming for: { "20": { "0":10, "1":20, ...

Validating Date Input in HTML and JavaScript Text Fields

When designing my form, I wanted to ensure that users could only input digits and hyphens in a text field where they enter a date. However, I encountered some difficulty implementing this feature. Currently, the field only accepts digits and removes any le ...

What is the proper way to add a line break within a Custom JSX Tag that is imported from a different class in the code

<Experience startYear={2019} endYear={2019} jobName="First Job" jobDescription="1. Providing API calls support for Headless CMS. 2. Provide escalated ticket/incident management support" ...

Issue with jQuery 'on' event not triggering following 'load' event

I am facing an issue on a page where similar events occur but when new content is loaded halfway through, most of the jQuery functionalities stop working. The scenario involves answering questions in a 'game' format using AJAX calls. Once all que ...

Disabling eslint does not prevent errors from occurring for the unicorn/filename-case rule

I have a file called payment-shipping.tsx and eslint is throwing an error Filename is not in camel case. Rename it to 'paymentShipping.tsx' unicorn/filename-case However, the file needs to be in kebab case since it's a next.js page that s ...

Creating an engaging Uikit modal in Joomla to captivate your audience

I need help optimizing my modal setup. Currently, I have a modal that displays articles using an iframe, but there is some lag when switching between articles. Here is the JavaScript function I am using: function switchTitleMod1(title,id) { document.g ...

Angular method for toggling panels open and closed

When working with Angular and having multiple panels similar to tabs, how can you open a panel on click and animate it up? If a different panel is clicked, new HTML content should be loaded in. However, if the same panel that is already open is clicked aga ...

Populate the div with the URL parameter only when another span element is empty after a specified time interval using setTimeout

When displaying a person's name on a page, there are two different methods to consider. It can be extracted from the URL or retrieved from an email form in the CMS used. However, these two approaches sometimes conflict with each other. Currently, I h ...

Refresh the webpage source code for an AJAX request

When using AJAX calls on a page, I have noticed that the page source remains unchanged. This can be problematic if a user performs forward/backward operations in their browser, as the browser will display the original HTML code instead of the updated conte ...

What is the best way to showcase hexadecimal values of a byte array?

Hello, I am currently developing an application in C#. I have a byte array that contains hex values. My goal is to write these values directly to a file without converting them into strings or any other data type. Any assistance on accomplishing this tas ...

Use a filter to narrow down results based on a column containing comma-separated values, as well as another specified

I am encountering an issue when attempting to apply filters on multiple columns within an object. The filters need to be applied based on the selection made in two drop-downs: one is directly linked to the ID, while the other holds the name but needs to be ...

Share user group Node Express with relevant data

I am looking for a way to create and send a customized navigation object to be rendered in an Express application based on user groups. My current approach involves adding middleware to each route: var navMiddleware = function() { return function(req, ...

Evaluating QUnit Test Cases

How do you write a test method in QUnit for validating functions developed for a form? For example, let's consider a form where the name field should not be left blank. If the validation function looks like this: function validNameCheck(form) { if ...

Struggling to make EJS button functional on the template

I am currently facing an issue with a loop that populates a webpage with multiple items, each containing an image, text, button, and a unique ID associated with it. Although I have written a function to retrieve the ID and plan name when the button is clic ...

The Javascript eval method throws a ReferenceError when the variable is not defined

In my constructor, I was trying to create a dynamic variable from a string. Everything was working smoothly until I suddenly encountered this error out of nowhere. I didn't make any changes that could potentially disrupt the system, and the variables ...

Steps for detecting a 401 Unauthorized error in SignalR when the token has expired

I have created a dynamic page that continuously fetches real-time information from my Azure functions backend using SignalR. If I am on the page for an hour and experience a disconnect, the signalr client will attempt to reconnect automatically, which usua ...

I'm looking to replicate this navigation layout using Vuetify's 'navigation drawer' component. How can I go about doing this?

I'm currently utilizing Vuetify and implementing the Navigation Drawer component. I am attempting to replicate a similar navigation layout found here. The fixed menu on the left should maintain its width even when resizing the browser window. Upon ...

Is there any trouble with this controller method?

Every time I use routers without controllers, they work perfectly fine. But the moment I add controllers to them, my routers stop working completely. Can someone please shed some light on what might be causing this issue with my router functions? LOGIN CO ...