Animating Angular on the table row element

I am currently displaying a table with a list of items that are updated via polling using an http get request to the server. The response is rendered only if there have been changes in the data.

My goal is to add animations to the rows of the table and trigger the animation only for new rows. Currently, the animation is triggered every time the response updates and the component re-renders, affecting all rows instead of just the new ones.

In my main.component.ts, I pass down observables to child components. These include the event$ stream with the array of events to display and newSeenPlatformIds$ that emits the maximum value of IDs when there is a change. This is used to trigger the animation for new rows based on this number.

ngOnInit() {
    // Events rendered in the table
    this.events$ = timer(0, 5000).pipe(
      switchMap(() => this.eventsService.fetchLastEvents()),
      distinctUntilChanged(
        (curr, prev) =>
          Math.max(...curr.map(currItem => currItem.id)) === Math.max(...prev.map(prevItem => prevItem.id))
      )
    );
    // Animation triggered every time a new ID is emitted
    this.newSeenPlatformIds$ = this.events$.pipe(
      map(events => Math.max(...events.map(event => event.id))),
      distinctUntilChanged()
    );
  }

This is the table component where the animation logic resides:

import { Component, OnInit, Input } from '@angular/core';
import { Observable } from 'rxjs/internal/Observable';
import { Event } from 'src/app/shared/interfaces/event';
import { trigger, style, transition, animate } from '@angular/animations';
import { tap } from 'rxjs/operators';

@Component({
  selector: 'app-last-events-grid',
  templateUrl: './last-events-grid.component.html',
  styleUrls: ['./last-events-grid.component.scss'],
  animations: [
    trigger('newRowAnimation', [
      transition('* <=> *', [style({ opacity: 0 }), animate('1000ms', style({ opacity: 1 }))])
    ])
  ]
})
export class LastEventsGridComponent implements OnInit {
  @Input() events$: Observable<Event[]>;
  @Input() newSeenPlatformIds$: Observable<number>;
  newSeenPlatformId: number;
  triggerAnimation = false;

  constructor() {}

  ngOnInit() {
    this.newSeenPlatformIds$.pipe(tap(id => console.log(id))).subscribe(id => {
      this.newSeenPlatformId = id;
      this.triggerAnimation = true;
    });
  }
}

Lastly, here's the template code:

<div class="flex justify-center">
  <table class="w-full mx-10">
    <thead class="text-gray-500">
      <tr>
        <th class="cell-main"></th>
        <th class="cell-main">DESCRIPTION</th>
        <th class="cell-main">TIME</th>
        <th class="cell-main">READER</th>
        <th class="cell-main">STATE</th>
        <th class="cell-main">NEXT CHECK</th>
        <th class="cell-main">READINGS LEFT</th>
      </tr>
    </thead>
    <tbody>
      <tr
        [@newRowAnimation]="triggerAnimation && event.id === newSeenPlatformId"
        [ngClass]="{ 'row-nok': event.estado === false }"
        class="rounded overflow-hidden shadow-lg text-xl text-gray-500"
        app-event-item
        *ngFor="let event of events$ | async"
        [event]="event"
      ></tr>
    </tbody>
  </table>
</div>

Answer №1

If anyone else encounters a similar problem, I managed to resolve it by exploring the "trackBy" option mentioned by @tsiro. I simply followed their advice and it fixed the issue:

solution

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

Tips for adjusting the maximum characters per line in tinyMCE 5.0.11

I have an angular 8 application that utilizes tinyMCE, and I am looking to limit the maximum number of characters per line in the textArea of tinyMCE. My search for a solution has been unsuccessful so far despite extensive googling efforts. (image link: [ ...

Tips for confirming a date format within an Angular application

Recently, I've been diving into the world of Angular Validations to ensure pattern matching and field requirements are met in my projects. Despite finding numerous resources online on how to implement this feature, I've encountered some challenge ...

Encountered a type error during project compilation: "Type '(e: ChangeEvent<{ value: string[] | unknown; }>) => void' error occurred."

I recently started working with react and I'm facing an issue with a CustomMultiSelect component in my project. When I try to call events in another component, I encounter the following error during the project build: ERROR: [BUILD] Failed to compile ...

What is the reason for observables not being subscribed to in NgRx effects?

My understanding was that in Angular, observables will not run unless they are subscribed to. However, when examining rxjs effects, the coding often appears like this: getProcess$ = createEffect(() => this.actions$.pipe( ofType(ProcessActions. ...

Unable to provide any input while utilizing npm prompts

After installing npm prompts, I encountered a strange issue. When trying to run the example code for npm prompts, I found that I couldn't enter any input at all. The underscore in the input field would blink for a few seconds, then the cursor would ju ...

Tips on implementing npm's node-uuid package with TypeScript

Whenever I attempt to utilize node-uuid in TypeScript, I encounter the following issue: Cannot find module uuid This error occurs when I try to import the uuid npm package. Is there a way to successfully import the npm uuid package without encountering ...

The call to react.cloneElement does not match any overloads

I'm encountering a typescript error in my React code when using React.cloneElement with the first parameter "children", and I am unsure of how to resolve it. As a newcomer to typescript, I believe that the type definitions in index.d.ts for cloneElem ...

Adjust the colors dynamically based on specific data within a loop

My task involves populating a table with data. Specifically, I am focusing on coloring the data in the first year column according to certain conditions. The desired outcome is as follows: +--------+------------+------+------+ | YEAR | 2022 | 2021 ...

Ensuring Koa ctx.query is valid prior to invoking the handler function

I'm currently working on a basic route that looks like this: router.get('/twitter/tweets', async (ctx) => { const { limit, page, search } = ctx.query ctx.body = { tweets: await twitter.all(limit, page, search), } }) The issue I ...

Display data in a template upon receiving a response from the server

Hello, I am currently in the process of developing my very first Angular application and could use some assistance. Specifically, I am working on a component that serves as an image search box. When a user inputs a search query, a request is sent to an API ...

Tips for modifying the data type of a property when it is retrieved from a function

Currently, I have a function called useGetAuthorizationWrapper() that returns data in the format of { data: unknown }. However, I actually need this data to be returned as an array. Due to the unknown type of the data, when I try to use data.length, I enc ...

Guide to releasing a NestJs library on npm using the nrwl/nx framework

Struggling with creating a publishable NestJS library using NX. Despite reading numerous documentations, I still can't figure it out. I've developed a NestJS library within an NX monorepository and now I want to publish just this library on NPM, ...

What is the best way to incorporate the "child_process" node module into an Angular application?

Trying to execute a shell script in my Angular application has been quite the challenge. I came across the "child process" node library that might be able to help me with this task. However, every attempt to import the library led me to the error message: ...

Is the return value a result of destructuring?

function display(): (number, string) { return {1,'my'} } The code above is displaying an error. I was hoping to use const {num, my} = print(). How can I correctly specify the return type? ...

Troubleshooting problems with Angular 2 reactive forms

I am looking for help with Angular 2 reactive forms. I have two specific goals for my reactive form view: To set a default value in the dropdown menu for country name. To update the country code input field based on the selected country name. See the ...

Is it possible to use string indexes with jQuery each method in Typescript?

When running a jQuery loop in Typescript, I encountered an issue where the index was being reported as a string. $.each(locations, (index, marker) => { if(this.options && this.options.bounds_marker_limit) { if(index <= (this.opt ...

What causes the entire set of dynamically created cards to collapse when using the toggle collapse button in ngb-bootstrap's Collapse control?

I am currently implementing the ngb-bootstrap collapse feature in my Angular 9 application. Incorporating the ngb-bootstrap collapse functionality within a Bootstrap card, I have multiple cards being generated. Each card is equipped with a collapse button ...

Using type as an argument in a hook in a similar fashion to how it is

My custom hook utilizes Zustand and is capable of storing various data types. However, I am looking to specify the type of data that will be stored similar to how it is done with the useState hook. import { Profile } from "@/types"; import { crea ...

The request for XMLHttpRequest has been restricted from accessing ASP.NET CORE 2.2.0 with Angular 8 and signalr1.0.0 due to a failure in the CORS policy (Access-Control-Allow-Origin)

nugetPackage on .net core2.2.0: signalr 1.0.0 + ASP.Core2.2.0 I am utilizing angular to utilize signalr: package.json: "@aspnet/signalr": "1.1.0", my front-end Angular code: import { Component } from '@angular/core'; import * as signalR fro ...

Can you help me identify any issues with this code for uploading an image and quickly displaying it in Angular?

Located within the profile.component.html file <label for="profile">Profile Photo</label> <input type="file" [(ngModel)]='uploadedPhoto'/> <img [src]='uploadedPhoto'> Contained in the profile.component.ts file ...