Arranging List Based on Date

I am looking to create a list that is grouped by date, similar to the image below. However, I am unsure how to implement this feature.

https://i.sstatic.net/7vjz0.png

You can see my code example and demo on Stackblitz

app.component.html

<div *ngFor="let monthData of products">
<div class="upcoming-container">
      <div class="upcoming-container-title-section color">
        Group Month | {{monthData.date | date : 'MMMM yyyy' }}
      </div>
      <div class="upcoming-container-card-section">
        <div *ngFor="let card of products" class="col-xl-2 col-lg-3 col-md-4 col-sm-6 col-6 upcoming-container-card-item">
            <app-card [listData]="listData"></app-card>
        </div>
      </div>
    </div>
</div>

card.component.ts

 _listData: listData;

  @Input()
  set listData(data: listData) {
    this._listData = data;
  }
  get listData() {
    return this._listData;
  }

  constructor() { }
}

card.component.html

<div class="cards-list">

<div class="card 1">
  <div class="card_image"> <img src="https://i.redd.it/b3esnz5ra34y.jpg" /> </div>
  <div class="card_title title-white">
    <p>{{listData.date | date: 'd MMM yyyy'}}</p>
  </div>
</div>

</div>

Answer №1

Regrettably, the *ngFor directive does not include a built-in group by feature. You will need to adjust your approach in order to achieve this functionality.

In your component.html:

<div *ngFor="let monthData of resultData">
<div class="upcoming-container">
      <div class="upcoming-container-title-section color">
        Group Month | {{monthData.date | date : 'MMMM yyyy' }}
      </div>
      <div class="upcoming-container-card-section">
        <div  *ngFor="let card of monthData.products" class="col-xl-2 col-lg-3 col-md-4 col-sm-6 col-6 upcoming-container-card-item">
            <app-card [listData]="card"></app-card>
        </div>
      </div>
    </div>
</div>

In component.ts:

let data = new Set(this.products.map(item => item.date))
data.forEach((date)=>{
     this.resultData.push({
        date: date, 
        products: this.products.filter(i => i.date === date)
      })
})

Check out the live example here: demo

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

How to update nested properties in Typescript using bracket notation

Imagine there is an interface and object with nested properties as shown below: interface Iobj { a: { a2:string }; b: string; } const obj: Iobj = { a:{ a2: "hello" } b: "world" }; Now let's say we have strings that ...

Issue encountered while utilizing combineReducers: "Error: The assetsReducer returned an undefined value during initialization."

Issue: The "assetsReducer" has returned an undefined value during initialization. When the state passed to the reducer is undefined, it must explicitly return the initial state, which cannot be undefined. If no value is set for this reducer, consider using ...

When debugging in Visual Studio 2013, Typescript variables/fields consistently show up as undefined

What is the reason behind the properties/field variables in Typescript being consistently undefined during debugging in Visual Studio 2013? ...

Passing a Typescript object as a parameter in a function call

modifications: { babelSetup?: TransformationModifier<babel.Configuration>, } = {} While examining some code in a React project, I came across the above snippet that is passed as an argument to a function. As far as I can tell, the modifications p ...

Yes, indeed - Entering the schema of a retrieved field from an Object schema

After deciding to upgrade from Yup version 0.29 to 1.2, I encountered some challenges with its types. Seeking assistance in finding the best solution for typing yup schemas. In version 0.29, the universal type Schema fit everywhere, but now it no longer d ...

Unit testing Jest for TypeScript files within a module or namespace

Recently, I've joined a mvc.net project that utilizes typescript on the frontend. There are numerous typescript files wrapped within module Foo {...}, with Foo representing the primary module or namespace. All these typescript files are transpiled in ...

I am unsure why it is displaying these errors

I created an auto-fill form that populates data based on ng-select information automatically. However, I am encountering an issue when attempting to delete selected data as it is throwing a Cannot read property 'pincode' of null error. Any help i ...

Step by step guide on integrating current locations in Leaflet OpenStreetMap within an Angular application

I am currently working on an Angular application that incorporates a map using Leaflet OpenStreetMap. I want to display the real-time latitude and longitude for the map, which should update based on the location. Can someone advise me on how to add the cur ...

Linting error: Unable to access properties of an undefined object (isStrict)

While setting up ESLint in an Angular project, I encountered an error when running the linter: Oops! Something went wrong! :( ESLint: 8.56.0 TypeError: Cannot read properties of undefined (reading 'isStrict') Occurred while linting C:\User ...

Enhance user experience with Angular Material and TypeScript by implementing an auto-complete feature that allows

Currently facing an issue with my code where creating a new chip triggers the label model to generate a name and ID. The problem arises when trying to select an option from the dropdown menu. Instead of returning the label name, it returns an Object. The ...

How can we incorporate methods using TypeScript?

I'm currently diving into TypeScript and encountering some challenges when trying to incorporate new methods into the DOM or other pre-existing objects. For instance, I'm attempting to implement a method that can be utilized to display colored te ...

When executed, the Node application successfully compiles

I have a TypeScript application that runs smoothly in development mode using ts-node. However, after building the application, I encounter some unexpected warnings and errors. This is my tsconfig.json: { "compilerOptions": { "incremen ...

Using Node.js with Typescript and RedisJSON allows for a powerful and efficient

I've recently started delving into nodejs, typescript, and redis for programming. However, I've encountered an issue with redis: when defining a data interface to be stored in redis, the system throws errors as soon as I try to specify the data t ...

Ways to activate a click event using typescript

Having trouble getting the click event to trigger in TypeScript. I have a table with a div inside it. Initially, the table data is not loading correctly. However, manually clicking on the table refreshes the data. How can I programmatically trigger the cli ...

Changing the color of the pre-selected date in the mat-datepicker Angular component is a key feature that can enhance the

Is there a way to adjust the color of the preselected "today" button in mat-datepicker? https://i.stack.imgur.com/wQ7kO.png ...

Error encountered when using withRouter together with withStyles in Typescript on ComponentName

Building an SPA using React with Typescript and Material UI for the UI framework. Stuck on a recurring error across multiple files - TS2345 Typescript error: Argument of type 'ComponentType<Pick<ComponentProps & StylesProps & RouteCompo ...

Tips for fixing the HTTP error 431 in Next.js with Next-Auth

I am encountering an issue with rendering a photo in jwt via token. Tools utilized: nextjs, typescript, next-auth, keycloak, LDAP The image is retrieved from LDAP and passed to the keycloak user. My application is responsible for storing the jwt token po ...

Tips for creating a tailored Express.js request interface using Typescript efficiently

I have been working on designing a custom Express request interface for my API. To achieve this, I created a custom interface named AuthRequest, which extends Request from Express. However, when attempting to import my interface and define req to utilize t ...

Using RxJs to apply a `filter` to an Array of Observable<T>, and emitting the value as soon as it meets a certain condition

Currently working on an application built with Angular2 in TypeScript, utilizing rxjs 5. Edit: Just to clarify, I am still relatively new to the Rx library and looking for the best practices. I did try to find some answers in the documentation before seek ...

redux-toolkit extraReducers not triggering

I've been experimenting with createAsyncThunk, but I'm having trouble getting the extraReducers to trigger. This is what my current setup looks like: export const getAllAgents = createAsyncThunk( "getAllAgents", async (token: string ...