Icons are now toggling for all items, not just one

Within my Angular 2 application, I have implemented a collapsible and expandable menu. The issue I am facing is with the toggling of icons - when expanding one menu item, the icon changes correctly but it also changes for all other menu items that are not expanded.

How can I ensure that the icon toggle only affects the clicked menu item? Additionally, how can I make sure that the previously expanded item collapses before expanding the new one, so that only one menu item is expanded at a time?

Below is the HTML snippet within a loop:

<a data-toggle="collapse" [attr.aria-controls]="itemType.id" [attr.data-target]="'#'+itemType.id" (click)="toggleCollapse()">
    <span>{{itemType.name}}</span>
    <span class="win-icon" [ngClass]="collapse == 'open' ? 'win-icon-ChevronDown':'win-icon-ChevronLeft'"></span>
</a>

<ul [id]="itemType.id" data-toggle="buttons">
    <li class="btn" routerLinkActive="active">
        <input type="radio">
            <span (click)="...">
                item 1
            </span>
    </li>
    [...]
</ul>

In my component:

public collapse: string = "closed";

public toggleCollapse() {
    this.collapse = this.collapse == "open" ? 'closed' : 'open';
}

Answer №1

To change the state of all items, you can toggle using this.collapse. Alternatively, you can utilize a temporary property in itemType such as itemType.collapse to achieve the same effect. Loop through the main array and set its collapse property to true to collapse all other items when the current one is opened.

<a data-toggle="collapse" [attr.aria-controls]="itemType.id" [attr.data-target]="'#'+itemType.id"
    (click)="toggleCollapse(itemType, itemsArr)">
     <span>{{itemType.name}}</span>
     <span class="win-icon" [ngClass]="!itemType.collapse ? 'win-icon-ChevronDown':'win-icon-ChevronLeft'"></span>

In Component:

public toggleCollapse(itemType, itemsArr) {
    itemsArr.forEach((item) => { item.collapse = true; }); // Collapse all other items when the current one is opened
    itemType.collapse = !itemType.collapse;
}

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

Leveraging the flexibility of an undefined variable as a useEffect dependency

Summary: Need help using listRef.current.clientWidth as a dependency in useEffect. I'm working on creating a dynamic list that automatically adjusts the width of its items based on the list's width. I've almost got it working, but I'm ...

Concealing the footer on a webpage embedded within an iframe

<script> $(document).ready(function(){ $('.mydivclass').hide(); }); </script> Looking to embed Microsoft Forms in my HTML using an iframe. Can the footer of the Microsoft Forms be removed? ...

Properly maintaining child processes created with child_process.spawn() in node.js

Check out this example code: #!/usr/bin/env node "use strict"; var child_process = require('child_process'); var x = child_process.spawn('sleep', [100],); throw new Error("failure"); This code spawns a child process and immediately ...

Utilizing JavaScript and jQuery libraries in conjunction with periods

I am a bit puzzled about when to include the period before referencing class names. For instance, in this code snippet, why is a period included before the first use of the 'active-slide' class but not for the other two instances? var primary = ...

From gathering user input on an HTML form, processing it using PHP, storing the data in a MySQL database, retrieving it with PHP, converting it to JSON

Description of issue: I am attempting to retrieve a value from an HTML input using PHP and execute a query on a MySQL database: SELECT * FROM WHERE (value = ID in the HTML input). MY APPROACH: I have created an HTML input: <input id="akt_djubrenje" ...

Comparing the properties of objects in two arrays can be done most effectively by utilizing the most efficient method available

In Angular2, I am looking for a more efficient way to check if an object's property in one array matches a property in another array and return the value. Is there a method similar to using .contains in Swift? doSomething(){ for (let element1 of ...

The event listener is failing to trigger the JavaScript function upon click

I am experiencing an issue with an onClick listener triggering an AJAX call to a PHP script. It seems that the click is not being recognized. Any suggestions on what could be causing this? Index.html <html> <head> <script src="//ajax.googl ...

I am experiencing an issue with my service provider when it comes to displaying multiple navigator stacks

Currently, I am developing a provider to manage the user's state across different views. The primary function of this provider is to display either one stack navigator or another based on whether a certain variable is filled or empty. This setup allow ...

How do I retrieve my compiled template from a directive in Angular?

Having a directive structured as follows: return { scope:{ divid: '@' }, template: '<div id="{{divid}}"></div>' } Here is an example instance: <direct divid="some-id"></direct> The goal is to execut ...

What is the process for encrypting and decrypting image files over the internet?

I'm currently developing a web application that requires loading images into a canvas object, followed by extensive manipulation. My goal is to conceal the original source image file (a jpeg) in such a way that users on the client side cannot access i ...

Using JavaScript to Establish Default Homepage in FireFox and Internet Explorer

How can I set a web address as the default homepage for a client? Specifically looking for solutions for (pageload) on IE8 and Firefox 3 (or newer versions). Thank you in advance for your help. Best regards ...

What are some ways to adjust the page being served in node.js?

I have set up my server.js file with necessary dependencies and routing for handling POST requests. However, I am looking to dynamically update the webpage served by this server when a POST request is received on /message endpoint. These requests are trigg ...

Ways to compare two arrays based on a specific field using JavaScript

I have two arrays, known as array 'a' and array 'b'. var a = [ [1,'jake','abc',0 ], ['r', 'jenny','dbf',0] , ['r', 'white','dbf',0] ] var b = [ ['s&ap ...

Duplicating a DIV that contains numerous input fields using Jquery Mobile

Just starting out with javascript and running into a little issue that I thought would be easy to solve. Using Jquery Mobile, I want to clone a div and update the IDs of the elements inside. The cloning part works fine, but I'm having trouble with a ...

What is the best way to align <h2> and <img> elements that are set to display as inline-block?

I've been experimenting with aligning h2 and img (icon) elements and although I added display: inline-block, they ended up shifting to the left of the section. Is there a way I can center them instead? HTML: <h2>Coffee</h2> <img src=&q ...

Can a jQuery/JavaScript script be run standalone?

I've got a bunch of HTML pages saved on my computer and I'm looking to create a JavaScript script that can extract specific text or elements from those pages. I found some jQuery code snippets on Stack Overflow that do what I need, but I'm n ...

Real-time Broadcasts Straight to Your Web Browser

Feeling a bit frustrated with my current situation. I am eager to stream a live video broadcast to a web browser. At the moment, I am utilizing ffmpeg to stream a directshow live source as a webm stream to node.js, which then sends the stream to the http ...

Ways to deactivate selecting rows in a datatable

Is there a way to deactivate the select feature specifically within datatables? An example of using ctrl+a for disabling select all function is available here https://i.stack.imgur.com/RQNXT.png ...

Error: The element 'scrollable' is not recognized in Angular2

I recently updated my angular2 project to the final release after previously using angular2 RC5 for development. However, I encountered an error message stating "scrollable is not a known element." If I change <scrollable><!--- angular code -- ...

Splitting the page in half

Is there a way to ensure that the LeftArea and wuiMainPageNav sections stay side by side regardless of the browser window size? When I resize the window, the layout gets messed up with wuiMainPageNav moving down. Any suggestions on how to address this issu ...