In an array where the first 3 images have been filtered using an if statement, how can I show the image at the 3rd index (4th image) starting from the beginning?

In my personal web development project, I'm using AngularJS for the front-end and .NET Core for the back-end to create a website for a musical instrument retailer. The model I'm working with is called "Guitar" and all the guitar data is stored in the Guitar table in the database, each with its own main image. Within the .ts file, I have an array of guitars that currently holds 5 entries. Index 0, 1, and 2 represent Martin guitars, while index 3 and 4 are Gibson guitars. To organize this information, I've divided Martin and Gibson guitars into separate components within Angular, giving each their own page and URL. Additionally, I've created a guitar-card component to style images using Bootstrap's card feature. Below is how I utilize Angular's condition statement to filter out Gibson guitars and display them exclusively:

<div *ngIf="guitar.brand == 'Gibson'">
  <app-guitar-card [guitar]="guitar"></app-guitar-card>
</div>

The issue arises when the images for indices 0, 1, and 2 still take up space on the webpage even though they are not displayed, causing the 3rd index image (Gibson) to appear displaced towards the right. My goal is to prevent these first 3 images from occupying space on the page unless they are being displayed.

Check out a screenshot of the problem here

Below is gibson-list.component.html:

<div class="container mt-5">
  <div class="row">
    <div *ngFor="let guitar of guitars" class="left col-lg-3 col-md-6 col-sm-12">
      <div *ngIf="guitar.brand == 'Gibson'">
        <app-guitar-card [guitar]="guitar"></app-guitar-card>
      </div>
    </div>
  </div>
</div>

And here's the corresponding gibson-list.component.ts file:

export class GibsonListComponent implements OnInit {
  guitars: Guitar[];

  constructor(private guitarService: GuitarService, private alertify: AlertifyService) { }

  ngOnInit() {
    this.loadGuitars();
  }

  loadGuitars() {
    this.guitarService.getGuitars().subscribe((guitars: Guitar[]) => {
      this.guitars = guitars;
    }, error => {
      this.alertify.error(error);
    });
  }
}

I attempted to resolve this spacing issue using CSS but was unsuccessful. Any assistance would be greatly appreciated. Thank you!

Answer №1

Divs are being added through *ngFor with empty content due to the presence of *ngIf. Consider using the following solution instead:

ts file

filter(guitars,brand){
  return guitars.filter(g=>g.brand===brand)
}

html file

<div *ngFor="let guitar of filter(guitars,'Gibson')" class="left col-lg-3 col-md-6 col-sm-12">

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

Why isn't it working? Looking for a script that adds a class when the element is empty?

I have a script that should add the class "leftmax" to the li element with id "spect" when the div element with id "tab2" is empty, but it doesn't seem to be working. Can someone provide some assistance? <script type="text/javascript"> $(funct ...

Repositioning the initial location of mat-slider

Currently, I am working on the mat-slider component and I have a specific requirement. I need to position the thumb in the middle and allow it to slide both left and right. Here is my code: https://stackblitz.com/edit/angular-9unenq-utcytk?file=app%2Fslid ...

The problem of SVG rendering order requires a solution that does not rely on javascript

I am new to experimenting with inline svg, and I wanted to share my code: <svg xmlns="http://www.w3.org/2000/svg" version="1.1" width="350" height="350"> <defs> <style> .ring { transform-origin: 175px 175px; } ...

tips for automatically adjusting the height and width of an image within a div

I'm struggling with fitting an image that is 1587*666 into a div that has a height of 600px and width of 300px. Can someone provide me with an example to achieve this?https://i.sstatic.net/4SWi5.jpg Here's the code snippet: <div id="myDiv" s ...

Can someone point out the mistakes in my CSS Layout?

I've been struggling to make progress on this task over the past couple of days, encountering roadblocks with no clear solution in sight. The maze of nested divs I'm navigating through might be playing tricks on my mind, so having a fresh pair of ...

What could be the reason behind Next.js attempting to import a non-existent stylesheet?

Lately, I've come across an issue where Nextjs is attempting to import a non-existent stylesheet (refer to the images below). I'm looking for guidance on how to resolve this error. Hoping to find a solution here, thank you Web browser console W ...

Creating a JQuery slider that efficiently loads and displays groups of elements consecutively

Currently, I am in the process of developing an infinite horizontal tab slider using jQuery. My main objective is to optimize loading time by having the slider display only the first 10 slides upon initial page load. Subsequent slides will be loaded as the ...

How to prevent page refresh when hitting enter in jQuery form?

Currently, my form does not refresh the page when I click the button to submit it. However, if I press Enter while a text input is selected, the page will refresh. Is there a way to make pressing Enter on the text input perform the same action as clicking ...

Refining a Collection of Possible Options

If I have an array of type Maybe<int>[] and want to extract only the values that are not None, what is the most efficient approach while ensuring TypeScript recognizes the output as int[]? It seems like declaring the result type as int[] is the way ...

Ways to address the issue of "$ is not a function"

Whenever I attempt to upload an image, this error message pops up: $ is not a function The source of the error can be found here: $(document).height(); ...

"Experience a unique website layout with varying designs on desktop and mobile devices while using the React technology to enable desktop

Just starting out with React and I've noticed something strange. When I view my website on a PC, everything looks normal. However, when I switch to the desktop site on my phone, things appear differently. It seems like moving an element by 20px on mob ...

Android TV with Ionic

I am working on an APK for Ionic on Android TV and I am facing an issue with the arrow controls. The APK runs on the device but the behavior is not correct. When using arrows to shift the app, the focus does not work as expected. It skips some divs. Here ...

Creating a horizontal line with text centered using Angular Material's approach

Implementing Angular Material, my objective is to design a horizontal line with a word positioned in the center. Similar to the one showcased here. I experimented with this code, achieving a close result, but I aim to align the lines vertically to the mid ...

Tips for resetting form fields and clearing previous values before resubmitting the form, allowing for a fresh start with empty fields

Welcome to my website! If you visit , you'll notice a feature I've added that clears form field values after submission. However, I'm encountering an issue where the fields remain filled when revisiting the page for another appointment. I at ...

Adjust the top margin of content to account for the height of a fixed header

When dealing with a fixed header that has been removed from the HTML flow, it can be tricky to adjust the content below it. The challenge arises because the height of the header can vary based on screen size. How can we dynamically adjust the margin-top fo ...

Display the percentage text behind a filling Bootstrap progress bar

I'm working on a bootstrap progress bar that displays a percentage along with the numerical value. Currently, the text fits next to the progress bar up until around 85%, after which it blocks the bar from growing further. You can see the issue in the ...

Tips on adjusting the height of divs in a simple layout

I'm trying to make two divs fill the page with a ratio of 70% and 30%. However, I don't want to set the size of the "html" or "body" elements. How can I achieve this without affecting the height of the text within the divs? Currently, it only dis ...

Is it possible to activate the jQuery .click() function for a button with specific text?

Here's a dilemma I'm facing: $('.add_to_cart span:contains("Choose a Size")').click(function() { console.log("it has been clicked") }); <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></s ...

What are the new features for listening to events in Vue 3?

Currently, I am manually triggering an event: const emit = defineEmits<{ (e: 'update:modelValue', value: string | number): void }>() // [..] <input type="text" :value="modelValue" @input="emit(&apos ...

Tips for Utilizing Border-Radius in Safari

What is the best way to hide a child element's corners within its parent (#div1) Ensuring that the child does not overflow its parent container? ...