Collective picture in the exhibit

I recently created a photo gallery that showcases various images sorted by categories. In this setup, I have one object containing both images and their respective categories, and another object solely dedicated to storing the categories.

The challenge I am facing is properly displaying the images within their corresponding categories without any blank spaces. Currently, as I traverse through the array of categories and evaluate the condition, it leaves empty spaces when false. How can I ensure that only images that meet the specified category condition are displayed?

If anyone could provide some guidance or assistance, I would greatly appreciate it!

Check out the DEMO here

Condition

*ngIf="product.Cat == cat"

html

<div *ngFor="let cat of Cats">
      <div class="row ">
            <span class="">{{cat}}</span> 
      </div>
      <ul class="mdc-image-list my-image-list" style="padding-left: 10px;padding-right: 10px;">
        <li class="mdc-image-list__item" *ngFor="let product of Images; let  j = index;">
          <div *ngIf="product.Cat == cat">
          <div class="mdc-image-list__image-aspect-container">
            <ng-container *ngIf="product.image == null; else productImage">
              <img src="./assets/image-not-found.svg" class="mdc-image-list__image imagenotfound">
            </ng-container>
            <ng-template #productImage>
              <img [src]="product.image" class="mdc-image-list__image">
            </ng-template>
          </div>          
          </div>
        </li>
      </ul>
    </div>

https://i.sstatic.net/tbakl.png

My issue arises when an image does not match its designated category, resulting in unwanted white spaces between images. Any suggestions on how to rectify this problem would be highly appreciated!

Answer №1

Is your issue that a li is being added to the unordered list regardless of whether the image belongs to that category, causing it to take up unnecessary space? To fix this problem, adjust your template as shown below:

<div *ngFor="let cat of Cats">
  <div class="row ">
    <span class="">{{cat}}</span>
  </div>
  <ul class="mdc-image-list my-image-list" style="padding-left: 10px;padding-right: 10px;">
    <ng-container *ngFor="let product of Images; let  j = index;">
      <!-- Only include a li if the product belongs to this category -->
      <li class="mdc-image-list__item" *ngIf="product.Cat == cat">
        <div class="mdc-image-list__image-aspect-container">
          <ng-container *ngIf="product.image == null; else productImage">
            <img src="./assets/image-not-found.svg" class="mdc-image-list__image imagenotfound">
          </ng-container>
          <ng-template #productImage>
            <img [src]="product.image" class="mdc-image-list__image">
          </ng-template>
        </div>
      </li>
    </ng-container>
  </ul>
</div>

Here's a live example you can reference.

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

Implementing dynamic display of div based on dropdown selection in typescript

A solution is needed to display or hide specific div elements based on a dropdown selection using Typescript. Sample HTML file: <select class="browser-default custom-select"> <option selected>single</option> <option value="1"> ...

Challenges encountered when implementing a personal library in a separate project

After updating a library I own, I seem to have encountered an issue when trying to use it in another project. However, the reason for this problem eludes me. A multitude of error logs with a similar theme are appearing: ERROR in ./node_modules/@company-na ...

Strategies for eliminating nested subscriptions in the search for names

I need assistance with refactoring a component I created to search for GitHub users by login. The current implementation contains nested subscribe blocks, and I would like to rewrite it using rxjs operators without nesting them. You can find the live exam ...

The element of type 'OverridableComponent<LinkTypeMap<{}, "a">>' cannot be assigned to a 'ReactNode'

I'm currently working on a project where there's a component named ListItemTextStyle. Within that component, the prop type is defined as follows: import { LinkProps, ListItemButtonProps, } from '@mui/material'; type IProps = LinkP ...

Encountering a TypeScript error while trying to pass the myDecorator function as a decorate prop to React

Encountering a TS error that states: (property) decorate?: ((entry: NodeEntry<Node>) => BaseRange[]) | undefined Type '([node, path]: [node: any, path: any]) => { anchor: { path: any; offset: string | number; }; focus: { path: any; offset: ...

generating declaration files for components with accurate type definitions from propTypes

I am in the process of creating a react/js library using rollup. My approach involves utilizing typescript to generate declaration files for my components. Despite having propTypes defined for my components, I have noticed that the emitted .d.ts files as ...

What is the TypeScript syntax for indicating multiple generic types for a variable?

Currently working on transitioning one of my projects from JavaScript to TypeScript, however I've hit a roadblock when it comes to type annotation. I have an interface called Serializer and a class that merges these interfaces as shown below: interfa ...

What steps can I take to troubleshoot and repair my accordion feature within an Angular project?

As a newcomer to Angular, I recently attempted to create an accordion component but encountered unexpected behavior. Here is the HTML code for my attempt: <div class="faq-item-container"> <h1 class="mt-1 mb-5"><strong>Frequently A ...

Checking a sequence using a list of strings

I have an array containing a list of IDs: var listId: string[] = []; var newId: boolean; for (let i in data.chunk) { listId.push(data.chunk[i].aliases[0]); } My objective is to compare a new ID with the entire list. If the new ID is found in the list ...

Issue with NGRX: component does not reflect state changes

Whenever I click on a button, it is supposed to open up a Google map. Upon clicking, I can see Google scripts being inserted, which triggers a callback that sends a message to my reducer. After receiving the message, I can confirm through logging that the ...

Using type values in TypeScript

I am trying to assign interfaces as values within a config object: export interface RouterConfig { startEvents?: typeof RouterEvent[]; completeEvents?: typeof RouterEvent[]; } The intended usage is as follows: private config: RouterConfig = { star ...

Ways to invoke a function in HTML aside from using the (click)="function()" syntax

How can I display data from a GET request to the WordPress API as the page loads in an Ionic app using Angular? I am able to retrieve my desired post list, but only when I use a button click event to call the method in the HTML. Since this is my first att ...

React JS hosted externally along with just plain Javascript and HTML page

We are looking to implement a common widget on multiple services using ReactJS. The goal is to write client-side code for this widget as an external hosted JavaScript file that can be included in pages across different frameworks such as Angular, Inferno, ...

Creating an array of JSX elements or HTMLElements in a React TypeScript rendering

Currently in the process of developing a custom bootstrap card wrapper that allows for dynamic rendering of elements on the front and back of the card based on requirements. Here is the initial implementation: import React, { useState, ReactElement } from ...

Keying objects based on the values of an array

Given the array: const arr = ['foo', 'bar', 'bax']; I am looking to create an object using the array entries: const obj = { foo: true, bar: true, bax: false, fax: true, // TypeScript should display an error here becau ...

Incorporating a particular JavaScript library into Angular 4 (in case the library doesn't have a variable export)

I am attempting to display the difference between two JSON objects in an Angular 4 view. I have been using a library called angular-object-diff, which was originally created for AngularJS. You can view a demo of this library here: Link I have trie ...

Combining Bootstrap admin template with SCSS styling in Angular 2

Currently seeking a complimentary bootstrap template featuring scss style to incorporate into my angular project. Is there anyone who can guide me on the correct procedure for this task? I stumbled upon a potential template on github but I am having diffic ...

Return true for cucumber datatable in typescript without fail

I am facing an issue where the following step definition always returns true even for incorrect data from the dataTable. Can someone assist me in correcting the syntax in TypeScript with Chai assertions? Then(/^Verify the following details in report$/, a ...

Struggling to make even the most basic example work with TypeScript and npm modules

After stumbling upon this repository that made using npm modules within a Typescript program look easy, I decided to give it a try by forking it and making some changes. My goal was to add another package to get a better understanding of the process. So, I ...

UI5 Tooling generated an error stating that "sap is not defined" after a self-contained build

Having successfully developed an application using SAPUI5 1.108, I encountered a setback when attempting to deploy it to a system running SAPUI5 version 1.71. The older version lacks certain features, causing the application to fail. In order to address th ...