Concealing objects with ngFor iteration

I need help with filtering out certain items from a list I am receiving from the backend before displaying them in the UI.

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

Specifically, I do not want to show "Conference" and "Reception" in the UI. Here is the HTML code snippet:

<ng-container *ngFor="let space of space_name">
    <div *ngIf="space">
      <a class="dropdown-item text-light" [routerLink]="['/spaces']"
         routerLinkActive="current"
         [queryParams]="{space_name:space}" data-toggle="collapse"
         data-target=".navbar-collapse.show"
         style="background-color: #8d0528;">{{ space | uppercase }}</a>
    </div>
</ng-container>

And here is the TypeScript code snippet:

this.sharedService.getDropdownspace().subscribe(data => {
  this.spaceDropdown = data;
  this.api_data = Object.values(this.spaceDropdown);
  this.space_name = this.api_data[0];
})

Can you please suggest an approach for achieving this filtering requirement?

Answer №1

If you want to achieve the same outcome, you can utilize the array filter method. Simply set up a function within your component and apply it to the *ngFor directive as illustrated below.

Inside your component, establish a filtering function for the desired results.

filter(itemList: room_name[]): room_name[] {
    let result: room_name[] = [];
    result = itemList.filter(item => {
        return (item != "Conference Room" && item != "Reception Room")
    });
    return result;
}

Within your template file

<ng-container *ngFor="let room of filter(room_name)">
    <div *ngIf="room">
        <a class="dropdown-item text-light" [routerLink]="['/rooms']" routerLinkActive="current" [queryParams]="{room_name:room}" data-toggle="collapse" data-target=".navbar-collapse.show" style="background-color: #8d0528;">{{ room | uppercase }}</a>
    </div>
</ng-container>

Check out the code and demo here: https://jsfiddle.net/ybdmrL2z/

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

Issue with Vue.js 2.0 transition not triggering on changing routes dynamically

I've encountered an issue where transitions are not firing on dynamic routes with parameters. For example, when navigating from /chapter/1 to /chapter/2, no transition occurs. However, when going from /chapter/1 to /profile/1, a transition does occur! ...

Set the styling of a div element in the Angular template when the application is first initialized

I have a question about this specific div element: <div class="relative rounded overflow-clip border w-full" [style.aspect-ratio]="media.value.ratio"> <img fill priority [ngSrc]="media.value.src || ftaLogo" ...

Why do I keep receiving the error "jquery_1.default is not a function" while trying to import jQuery using SystemJS

After installing foundation using jspm install foundation, and importing jquery alongside it I encountered an issue where importing jquery with import $ as 'jquery' resulted in the error message "jquery_1.default is not a function." However, imp ...

Issue with Multer s3 upload: File not functioning properly in s3 and size increase of file

I successfully uploaded an mp4 file to s3 using node.js and the code seems to be functioning well as I can see the file in s3. However, there seems to be a discrepancy in the file size. The original file is 860kb but after uploading it, it increases to 1.4 ...

Are there any alternatives to using the $size function in MongoDB for this specific tier in Atlas?

I am trying to create a code that will return certain data only if there are exactly 2 instances of the by field in the data array. The number of _id entries can vary. Unfortunately, using { $size: { data: 2 }, } doesn't work as I am encountering an ...

Checking for undefined based on certain conditions

When looking at the following code snippet type stringUndefined = "string" | undefined; type What<T> = T extends undefined ? "true" : "false"; const no : What<stringUndefined> = ""; The value of ' ...

Is there a way to include a React component within the setContent method of Leaflet?

Is there a way to trigger a React JS component in setContent? I am looking for a solution to add a button within a popup Leaflet, which when clicked will call a React component. While I am aware of the "reactDomserver.rendertostring" method to co ...

If I click on a different VueJS menu, make sure to close the current menu

When using a component menu, each item is displayed independently. However, I would like the open items to close automatically when I click on another item with the menu. I employ a toggle functionality on click to control the opening and closing of the c ...

Is it possible for Promise.all to accept an array containing only non-Promise elements?

Promise.all typically resolves when all promises in its array resolve. However, there is an instance where one element of the input array is not a promise https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Promise/all#Using_Promi ...

Retrieving the latest entry from the MySQL database

I am encountering an issue with a code that involves fetching data from MySQL into an array. I have a form with color and size select boxes, and when an onclick javascript function is triggered it creates two additional select boxes. I have successfully re ...

We are considering implementing Angular 2, however our current application is built with Angular 1.x. Are there any resources available for integrating Angular 2 with Angular 1.x?

Currently, rewriting our app is not an option and we are also hesitant to develop a new app solely for angular 2. We are exploring alternatives to integrate both frameworks gradually for a smoother migration process. ...

Prevent the use of redirection in a post request

I’m struggling to figure out how to display JSON data from the backend on the frontend without triggering a page redirect. Despite my efforts to prevent redirection in this POST request, it keeps automatically navigating to the URL search_results/ and d ...

Can an image or SVG be included with every selectize option?

Currently, I am working on a country list with selectize. However, I am looking to enhance it by including the flag of each country before its name. So far, my research has not yielded any information on this specific feature. I am curious if it is possib ...

Design a webpage using material-ui components

I'm struggling to create a layout using material-ui for a child page. Can someone guide me on how to achieve this design? https://i.sstatic.net/TwYR5.png ...

I am having trouble making this specific type generic. The type "x" is not able to index type "y" ts(2536)

I am currently working on creating a generic type that takes a root-level property name and returns a union type of a property nested underneath it. For instance: interface operations { updateSomething: { "201": { schema: number; ...

Verifying Angular Select List Options

I am new to using Angular and I have a task to implement validation where a button should be hidden if a Dropdown list is not selected. Below is the code snippet that needs attention: Button Function Code <input type="file" class="custo ...

JavaScript - Issues with Cookie Setting

I created a function for setting cookies: function storeCookie(cname, cvalue, exdays){ var d = new Date(); d.setTime(d.getTime() + (1000*60*60*24*exdays)); var expires = "expires="+d.toGMTString(); document.cookie = cname + "=" + cvalue + ...

Unraveling JSON data within an AngularJS controller

I'm facing an issue with exposing a field in my AngularJS controller. The problem arises when a JSON 'owner' object is returned by a webservice, containing a field named 'Cave'. If this 'Cave' field has a null, empty, or ...

No bugs appeared in Next.js

I am currently in the process of migrating a large create-react-app project to NextJS. To do this, I started a new Next project using create-next-app and am transferring all the files over manually. The main part of my page requires client-side rendering f ...

Add a new element dynamically and update its content on the fly

I am looking to clone certain elements and modify the text inside them using a regex. Here is my progress: (working fiddle - http://jsfiddle.net/8ohzayyt/25/) $(document).ready(function () { var divs = $('div'); var patt = /^\d&bso ...