cutting the limit of table rows in Angular by half

I have an array called carsList with 5 values. I am trying to slice it 2 by 2.

<table>
    <tr *ngFor="#item of carsList | slice:0:2; #i = index">
        <td>{{i}}. {{item}}</td>
    </tr>
</table>  

The code above is only slicing 2 values at a time, but I actually want to slice 2 by 2.

 Example 

0   1
2   3
4   5

Here is the link to my code for reference:

https://plnkr.co/edit/BZnaSIldgO3Dmwao1mo6?p=preview

If you could please help me find where my mistake is, I would greatly appreciate it.

Answer №1

When using the slice pipe, it may result in an array with 2 elements. However, when using ngFor to iterate over the elements in the array, the expected result may not be achieved.

To get the desired outcome, you can either adjust the structure of the array or implement a solution like this using an additional ngIf with a nested ngFor.

<table>
  <ng-container *ngFor="#item of carsList; #i = index">
    <tr *ngIf="i % 2 === 0">
      <td *ngFor="#item1 of carsList|slice:i:i + 2; #i1 = index" class="car-title">{{i1}}. {{item1}}</td>
    </tr>
  </ng-container>
</table>

Alternatively, you could remove the nested ngFor and manually create 2 td elements.

<table>
  <ng-container *ngFor="#item of carsList; #i = index">
    <tr *ngIf="i %2 === 0">
      <td>{{i}}. {{item}}</td>
      <td>{{ i + 1 }}. {{carsList[i + 1]}}</td>
    </tr>
  </ng-container>
</table>

Just so you know: ng-container can be utilized to group elements that are not included in the DOM tree.

Check out the updated plunker link.

Answer №2

The concept proposed by Pranav in his response touches upon the approach for achieving the desired outcome, although it appears that his plunker demonstration did not precisely mirror what was originally requested. Additionally, it seemed to be a bit overly complex.

A simpler solution, as shown in the original post's plunker (verified to work there), would be:

<ul>
 <ng-container *ngFor="#item of carsList; #i = index">
  <li *ngIf="i % 2 === 0">
   <span class="car-title">{{i}}. {{item}}</span>
  </li>
 </ng-container>
</ul>

Answer №3

Give this a go -

<ul>
 <ng-container *ngFor="#item of carsList; #i = index">
  <li *ngIf="i % 2 === 0">
   <span class="car-title">{{i}}. {{item}}</span>
   <span class="car-title">{{i+1}}. {{carsList[i + 1]}}</span>
  </li>
 </ng-container>
</ul>

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

Utilizing Typescript Decorators to dynamically assign instance fields within a class for internal use

I am interested in delving into Typescript Decorators to enhance my coding skills. My primary objective is to emulate the functionality of @Slf4J from Project Lombok in Java using Typescript. The concept involves annotating/decorating a class with somethin ...

Date Boxes in a Tangled Web

Looking to showcase multiple dates on a screen, I'm encountering an issue where clicking on a date button opens a datepicker. If the user clicks out of the box, the datepicker closes properly. However, when another datepicker button is clicked, instea ...

What is the most streamlined way to send data from HTML forms using solely JavaScript?

I currently employ jQuery to transmit data from my HTML forms and then I utilize PHP to save it in my database. Below is the code snippet that I am using: HTML FORM: <form id="formpost" action="" method="post" onsubmit="return false"> <input t ...

Is there a way to eliminate duplicates from an array in JavaScript by utilizing a set and placing each element in an li tag? Here is the code

const numbers = [" 4", "9", "16"," 4", "9", "16"," 4", "9", "16"] <ul> {(<li> {[...new Set(numbers)]} </li>)} </ul> const numbers = [" 4", "9", "16"," 4", "9", "16"," ...

Retrieve fresh information every 30 seconds utilizing the useQuery hook in React

I am utilizing '@tanstack/react-query' to retrieve data in my React + Typescript application. Below is a snippet of my code, where I aim to fetch metric data every 30 seconds import { useQuery } from '@tanstack/react-query'; import ...

It appears that the font in style.css is not being updated properly and seems to resemble

My issue lies within my CSS code. The text on my website is not displaying correctly and seems to be ignoring the styling that I have applied. Even though I have used similar styling on other buttons which look fine, this specific text element is causing p ...

Angular Express encountering URL mismatch when retrieving files post-refresh

Currently, I am developing a small MEAN stack application. One of the features is that when an image is clicked, it displays a partial containing information about the image. Everything was working fine initially. However, after refreshing the page, Angula ...

Updating React state using a form input

Seeking assistance on retrieving form values and storing them in state. Despite following various guides (mostly in class style react), I keep encountering the same error: "Nothing was returned from render. This usually means a return statement is m ...

Unable to retrieve button value with material-ui package

My task requires me to retrieve the value of a button, as it contains an ID essential for further steps. Initially, I used a standard button with Bootstrap styling and everything functioned correctly. <button value={row.vacationRequestID} c ...

ES6 Update: Manipulating Nested Arrays with JavaScript

I have the following list of items: [ { idItem: "1", name: "apple", itemLikes: [{ id: "1", idItem: "1" }] } ] My goal is to simply add a new object to the itemLikes array. Here is my ...

Utilizing a router to control a GET request for accessing an image file

Currently utilizing node.js in conjunction with the express framework, I am attempting to initiate a get request for an image by appending it to the body through $('body').append('<img src="images/image.gif?34567">'). Despite rece ...

Having trouble with image loading in NextJS after replacing an old image with a new one?

I have been attempting to swap out my current banner with different images to test if they work, but every image I try leads to an error when using next/image or even a simple <image> tag. The error message states that "The requested resource isn&apo ...

Multiple file uploads are causing the issue of req.file being undefined

After going through all the suggested solutions, I am still struggling to identify the root cause of my issue. The problem arises when trying to upload multiple files and form inputs - the req.file is always undefined and the image data ends up in the req. ...

Is there a way to easily transfer your home address to Google Maps without having to manually input

Is there a way to automatically display home addresses directly on Google Maps (maps.google.com) and show it in the search bar of Google Maps using pure JAVASCRIPT, pulling the addresses from another website (UAT)? I'm new to this programming language ...

Unable to pass property to child component

When trying to pass a string array prop in my child component, I encountered an error message: "Cannot destructure property 'taskList' of 'this.state' as it is null.". This error occurred when using destructurization. What am ...

Executing the executeScript method in Microsoft Edge using Java and WebDriverWould you like a different version?

I'm currently attempting to execute the following code in Microsoft Edge using WebDriver ExpectedCondition<Boolean> jsLoad = driver -> ((JavascriptExecutor) driver).executeScript("return document.readyState").toString().equals(&quo ...

Finding it challenging to adapt an AngularJs component-based modal to TypeScript

When creating an AngularJS component in JavaScript and displaying it as a modal using ui-bootstrap, you need to pass bindings that the modal instance can use for dismissing or closing itself: app.component("fringeEdit", { controller: "FringeEditCont ...

Is it feasible to utilize VAST for delivering HLS streams? Can m3u8 files be incorporated into VAST tags for delivery?

We are integrating a video player that supports VAST pre-roll, mid-roll, and post-roll video ads. I'm wondering if it's feasible to include m3u8 files in VAST tags? I've reviewed the vast specification and examples, but haven't come ac ...

What could be the reason behind the unexpected behavior of my Bootstrap 3 carousel?

I'm attempting to replicate this visual effect in my own carousel, featuring semi-transparent images on the left and right sides. However, I'm encountering a negative result with my project when transitioning between slides at : here. This is th ...

Encountering a Vercel deployment failure due to a TypeError: The 'split' property cannot be read from undefined within several objects

I'm having trouble deploying my web application for the first time and encountering this error on Vercel: TypeError: Cannot read property 'split' of undefined at Object.3qS3 (/vercel/path0/.next/serverless/pages/[collection]/[templateId].j ...