Effortlessly collapsing cards using Angular 2 and Bootstrap

Recently delving into Angular 2 and Bootstrap 4, I set up an about page using the card class from Bootstrap. Clicking on a card causes it to expand, and clicking again collapses it. Now, I want to enhance this by ensuring that only one card is open at a time. When I click on a new card, the previously open card should automatically close.

Here's a snippet of my HTML file:

    <div class="card">
            <div class="card-header" (click)="toggleShowSystemRequirements()">
              <h4>
                System Requirements
                <span class="pull-right" *ngIf="!showSystemRequirements"><i class="fa fa-chevron-down"></i></span>
                <span class="pull-right" *ngIf="showSystemRequirements"><i class="fa fa-chevron-up"></i></span>
              </h4>
            </div>
            <div *ngIf="showSystemRequirements" class="card-block">
              <p>Software will operate on the following computers:</p>
              <ul>
                <li>
                  64-bit or 32-bit computer running Windows OS
                </li>
                <li>
                  64-bit or 32-bit computer running Linux OS
                </li>
                <li>
                  Mac OS X
                </li>
              </ul>
            </div>
          </div>
<div class="card">
        <div class="card-header" (click)="toggleShowPSAT()">
         <h4>
              PSAT
           <span class="pull-right" *ngIf="!showPSAT"><i class="fa fa-chevron-down"></i></span>
           <span class="pull-right" *ngIf="showPSAT"><i class="fa fa-chevron-up"></i></span>
          </h4>
        </div>
        <div *ngIf="showPSAT" class="card-block">
            <p>

            </p>

            <h4>Intended Users</h4>
            <p>

            </p>

            <h4>Inputs</h4>
            <p></p>
            <ul>
              <li>P</li>
              <li>System</li>
              <li>Number</li>
            </ul>

            <h4>Outputs</h4>
            <p>Base</p>
            <ul>

              <li>Optimization 
              </li>
            </ul>
          </div>
        </div>

Additionally, here's the TS file related to this functionality:

import { Component, OnInit } from '@angular/core';


@Component({
  selector: 'app-about-page',
  templateUrl: './about-page.component.html',
  styleUrls: ['./about-page.component.css']
})
export class AboutPageComponent implements OnInit {

  showSystemRequirements: boolean = false;
  showPSAT: boolean = false;
  showPHAST: boolean = false;
  constructor() { }

  ngOnInit() {
  }


  toggleShowSystemRequirements() {
    this.showSystemRequirements = !this.showSystemRequirements;
  }
  toggleShowPSAT() {
    this.showPSAT = !this.showPSAT;
  }
  toggleShowPHAST() {
    this.showPHAST = !this.showPHAST;
  }
}

Answer №1

If you're looking for a solution, one idea that comes to mind is creating a new function called clearAll. This function can reset all your flags to false and be called within your click functions for all cards.

    toggleShowSystemRequirements() {
    if(this.showSystemRequirements == false){
    this.clearAll()
    }

    this.showSystemRequirements = !this.showSystemRequirements;
  }


clearAll(flag){
this.showSystemRequirements  = false;
this.showPHAST = false;
this.showPSAT = false;
}

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

Creating a Modern Application with MEAN stack utilizing Angular 2 and Angular-CLI

Currently, I am in the process of developing a MEAN app using Angular 2 and Angular CLI for building. Everything seems to be running smoothly as my GitHub repository can attest (link here). However, upon trying to access the page, I encounter multiple refe ...

Using Laravel 5.2 passport for secure authentication with Angular 2

Being new to Laravel Passport, I find it quite confusing. I've watched Taylor Otwell's tutorial on Passport but still can't figure out if it's possible to authenticate an Angular app with Laravel Passport. My goal is to develop a Full ...

Utilizing UI-GRID to showcase JSON information

I am currently in the process of fetching data from the server. [ { id:1, name:demo, request: { id: 1, localCompany: { id: 1 } } }] [{ }, { }] This is how my JSON object appears to be structured. After calling ...

Callback for dispatching a union type

I am currently in the process of developing a versatile function that will be used for creating callback actions. However, I am facing some uncertainty on how to handle union types in this particular scenario. The function is designed to take a type as inp ...

Executing a function when a user chooses to exit a webpage using the @HostListener('window:beforeunload') method

Utilizing @HostListener('window:beforeunload') allows me to detect when a user navigates away from the page, prompting a dialog window to open. I wish for an event to be triggered or a method to be executed if the user chooses to leave the page. ...

The attribute 'checked' is not a valid property for the 'TElement' type

Learning about typescript is new to me. I have a functional prototype in fiddle, where there are no errors if I use this code. http://jsfiddle.net/61ufvtpj/2/ But in typescript, when I utilize this line - if(this.checked){ it presents an error [ts] Pro ...

Encountering a hiccup while attempting to implement ngx-translate in an Angular library

I'm in the process of developing an Angular library, and I need to incorporate ngx-translate. In my other applications, I typically add TranslateModule.forRoot in the app.module file. import .... // AoT requires an exported function for factories exp ...

The element "center" is not a recognized HTML tag in Angular 4

The center tag is not functioning and it says that center is an unknown element. <center> <a><img class="placeholder_img" src="img/placeholder.png"></a> </center> When I used the above HTML tags, it stated that "center i ...

Tips for generating a fixed-length array from multiple arrays with different lengths, focusing on selecting items from each array according to their significance

In order to create a quiz, I am looking to extract 'questions' from various 'topic' arrays. These topics are selected based on the user's preference and are used to populate a question bank for a 20-question quiz. The topics rated ...

Tips for using a TypeScript method decorator while maintaining the expected `this` scope

It was brought to my attention that the issue I encountered was due to the use of GraphQL resolvers in running my decorated method. This resulted in the scope of this being undefined. Nevertheless, the core of the question provides valuable insights for an ...

Error message "Uncaught in promise" is being triggered by the calendar function within the Ionic

Can someone assist me in creating a calendar feature for my app? My concept involves a button with text that, when clicked by the user, opens a calendar. However, I am encountering an error message: ERROR Error: Uncaught (in promise): TypeError: Cannot set ...

Using Typescript, Angular, and Rxjs to retrieve multiple HttpClients

I am looking to send get requests to multiple endpoints simultaneously, but I want to collect all the responses at once. Currently, this is how a single endpoint request is handled: public getTasks(): Observable<any> { this.logger.info('Ta ...

Sharing information between components in Angular 4 and .NET Core applications

I am new to Angular and .NET Core. I have successfully created a web api using .NET Core, which is called from an Angular 4 application. Currently, everything is working smoothly. However, after submitting a form that inserts records into the database, I w ...

Recommendations for Organizing Multiple "Isolated" Applications Using MVC 5 and Angular 2

We are currently managing a large MVC 5 ASP.NET 4.5.1 web application that follows the concept of treating each page as its own application due to the vast areas it covers. The existing pages are built using JQuery, regular Javascript, and Handlebars templ ...

Tips for creating a test to choose a movie from the MuiAutocomplete-root interface

I am currently utilizing Material UI with React using Typescript and I am looking to create a test for the autocomplete functionality using Cypress. Here is the approach I have taken: Identifying the Autocomplete component and opening it, Choosing an opti ...

Implementing Angular 4, modifying the display to reflect changes in the latest model

Hey, I'm having some trouble with a component called 'updateUserInfo'. When I call it from a service, the value in the view doesn't change. Can someone assist me with this, please? @Component({ selector: 'app-menu', t ...

Testing a React component that utilizes RouteComponentPropsTesting a React component with RouteComponentProps

One of my components has props that extend RouteComponentProps defined as follows: export interface RouteComponentProps<P> { match: match<P>; location: H.Location; history: H.History; staticContext?: any; } When I use this component i ...

Guide to customizing Material UI theme using Typescript in a separate file

Trying to customize Material UI theme overrides can be a bit tricky, as seen in the example below: // theme.ts const theme: Theme = createMuiTheme({ overrides: { MuiButton: { root: { display: 'inline-block', fontWeigh ...

Error: Attempting to add types to an object returned from the .map function in JSX Element

When attempting to include the item type in the object returned from the .map function, I encountered a JSX error. I tried specifying item: JSX.Element as the Item type, but this didn't resolve the issue. Can someone provide clarity on this matter? Th ...

Electron Searching for Files in Main Directory

We have developed a web application using Angular 2, but we are facing an issue when trying to run it as an Electron application. After branching out the solution and making changes to package.json to launch Electron on start, we encountered an unexpected ...