Showing off the latest products at the top of the list

Typically, when utilizing ngFor, the most recent item is displayed underneath the initial element.

For instance, a list containing: [Apple, Orange, Banana]

If we use ngFor to display this list:

  • Apple
  • Orange
  • Banana

I am interested in learning a method to reverse the order and showcase the very last element (or any newly added elements that appear at the top while the older ones are automatically shifted below). For example:

  • Banana
  • Orange
  • Apple

HTML:

<button (click)="deposit()">Deposit</button><br>
<button (click)="withdraw()">Withdraw</button>
<p>{{balance}}</p>

  <p *ngFor="let element of transactions">{{element}}</p>


  <router-outlet></router-outlet>

CSS:

.example {
    background-color: #eee;
    width: 200px;
    height: 100px;
    border: 1px dotted black;
    overflow: scroll; /* Add the ability to scroll */
    -ms-overflow-style: none;
    scrollbar-width: none;
    direction: ltr;
  }

  .example::-webkit-scrollbar {
    display: none;
}

I would appreciate suggestions for a CSS attribute that causes the oldest elements below the overflown list to gradually fade away and only become visible when scrolling down.

Answer №1

I'm not completely sure what your goal is, but if you're looking to maintain the original sequence while displaying the last element first, you can achieve this by using array slicing to extract the last element:

<p>{{transactions[transactions.length - 1]}}</p>
<p *ngFor="let element of transactions.slice(0, transactions.length - 2)">{{element}}</p>

If you prefer to completely reverse the order, you can do so with this code:

<p *ngFor="let element of transactions.slice().reverse()">{{element}}</p>

Answer №2

To achieve this effect, you can utilize CSS.

<div class="wrapper">
  <p *ngFor="let item of data">{{item}}</p>
</div>
.wrapper {
  display: flex;
  flex-direction: column-reverse;
}

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

typescript: best practices for typing key and value parameters in the forEach loop of Object.entries()

I have a specific object with key/value pairs that I need to iterate over using the entries() method of Object followed by a forEach() method of Array. However, I'm struggling to understand how to avoid a typescript error in this situation: type objTy ...

Creating flexible layouts in Angular 2+ by dynamically adjusting element width based on available space

When working with Angular 2+, I always take into consideration the proper setting of element widths. My initial approach involves assessing the available space within the parent element and then adjusting the width of child elements to evenly fill this all ...

Data from HTML not being transferred by Angular Forms

I am facing an issue with transferring input data from HTML's <select> element to Angular Forms. Let's take a look at my code first. File Name: home-page.component.html <form [formGroup]="rForm" (ngSubmit)="addPaste(rForm.value)"> ...

Ways to contrast HTML without considering whitespace?

I'm currently testing an HTML builder through unit testing. My goal is to confirm that the output content matches the expected content, while allowing for some leniency when it comes to white space. More specifically, I am not concerned with whether ...

Transferring data from JavaScript to PHP with the help of AJAX

I am encountering issues with my code, as I cannot seem to successfully send coordinates from JavaScript to PHP using AJAX. Although I can retrieve values from JavaScript into a textbox, the values are not being transferred to PHP. Any assistance on resolv ...

What is the best way to implement an object literal method for making an HTTP GET request to retrieve JSON data?

I'm facing an issue with the HTTP GET method when trying to retrieve JSON data. Although the HTTP GET method is successfully fetching the JSON data, I'm struggling to connect it with the object literal. For better clarity, here's the specif ...

An entire line of boxes has been checked off

Currently, I am working on adding an additional column with checkboxes to the Mat table example. However, I noticed that when I click on one checkbox in a column, the checkboxes in other columns also get selected. How can I implement this so that only th ...

Error: Property '$filter' is not defined and cannot be read

Whenever a user clicks on a link, I display the json object on the UI. However, an exception is being thrown with the following message: TypeError: Cannot read property '$filter' of undefined Check out the demo here: http://plnkr.co/edit/5NOBQ3 ...

The `Required<Partial<Inner>>` does not inherit from `Inner`

I stumbled upon a code snippet that looks like this: type Inner = { a: string } type Foo<I extends Inner> = { f: I } interface Bar<I extends Inner> { b: I } type O<I extends Partial<Inner>> = Foo<Required<I>> & B ...

Change in navigation bar appearance on scroll down

I am attempting to create a fixed navigation bar by customizing the Bootstrap CSS file. The goal is to change the navbar's color and make it stick to the top when scrolling down. Although I followed the instructions in this article, the JS code I add ...

In PHP, divide a file into pairs of $key => $value while handling duplicate keys

My brain is in a freeze trying to understand this situation. I have a document where each line is divided by "=" into different pieces of data: // This is an example file. Format: <key>=<Value> key1=value1 key1=value2 key1=value3 key2=value1 ...

Managing boolean responses and errors correctly in Angular 6

Here is the code I have written, but I am uncertain about its correctness. My service method returns a boolean value. What happens if there is an error returned in the subscription? this._service .UpdatesStatus(this.transaction) .subscribe((response: ...

`Can you guide me on the proper path for designing this website layout?`

As I embark on creating my first website, I have a specific vision in mind. I want the full display on a computer screen to show all the information and links without any need for scrolling. However, when the window is resized to smaller screens, I would l ...

Enabling clients to access all static files from a Node.js + Express server

My index.js file serves as a node.js server : var express = require('express'); var app = express(); const PORT = process.env.PORT || 5000; var serv = require('http').Server(app); app.get('/', function(req, res) { res.sen ...

Incorporating a new text tag

Is there a way to add a text label for every circle that can be rotated and have its color changed? I'm looking for a solution that doesn't involve JSON data, but instead retrieves the necessary information from somewhere else. var w = 3 ...

Putting VueJS, typescript, and jest to the test: examining the contents of a dropdown list web-component

Currently, I am in the process of testing a dropdown list within a web component utilized in a VueJS application. My main focus is on checking whether the dropdown list actually contains items fetched through an HTTP query (handled in a vuex store) once t ...

Incorporate an external library

I am currently facing a challenge in my angular2 project where I need to import a 3rd party library. Here are the steps I have taken so far: ng new myproject npm install --save createjs-easeljs npm install @types/easeljs However, I am stuck at this poin ...

I am interested in incorporating various forms within this code

I followed an online tutorial to create this code, and I've made some edits myself, mainly related to the buttons that display information. However, I'm still learning and struggling with adding different forms to each section. I would really app ...

What is preventing me from accessing the sub-elements of an AngularJS controller?

I know this question has probably been asked countless times before, but I'm struggling to find the right words to explain my specific issue. Essentially, I'm having trouble accessing something like $ctrl.thing.subelement. However, the following ...

Ensure the browser stays anchored at the bottom of the page while employing jQuery to reveal a div

This piece of code allows me to toggle the visibility of a div: <a href="#" class="show_hide">Show/hide</a> <div class="slidingDiv"> My content...... <a href="#" class="show_hide">hide</a></div> <script src="http:// ...