How can I create an accordion feature and properly align the rows in the table beneath it?

After experimenting with Angular, I decided to incorporate an accordion feature into my table where each row expands to display additional information. However, I am encountering some issues. How can I resolve this?

Typescript Code

constructor(
    ...
    protected labarugiReport: ReportLabarugiService
) { }

ngOnInit(){}
...

isActive(id) {
   return this.selected === id;
}

async getSub(id:any){
     this.selected = (this.selected === id ? null : id);
     let dateEnd = new Date(this.dateEnd.value);
     let dateStart = new Date(this.date.value);
     this.itemSoldSub3 = await this.labarugiReport.getReoportListSoldItemByCategoryAll(this.user.principle.id, id, this.getDate(dateStart), this.getDate(dateEnd));
}

My Scss code

.table-container {
    .activetab {
        display: block !important;
    }

}

HTML code that has been modified from another source

<tbody>
   <tr *ngFor="let data of itemSoldSub2;let i=index" [ngClass]="{activetab: isActive(data.categoryId)}">
         <div (click)="getSub(data.categoryId);">

               <div *ngIf = "data.categoryName">
                        <td style="border-bottom: none;">{{data.categoryName}}</td>
                </div>
                <div *ngIf = "!data.categoryName">
                          <td style="border-bottom: none;">Uncategorized</td>
                        </div>
                      <td style="border-bottom: none;">{{data.sumPrice}}</td>
               </div>
                <!-- </tr> -->
               <table>
                  <tbody [ngClass]="{activetab: isActive(data.categoryId)}">
                      <tr *ngFor="let sub of itemSoldSub3">
                          <td>{{sub.itemName}}</td>
                          <td>{{sub.sumTotalPrice}}</td>
                      </tr>
                  </tbody>
               </table>
   </tr>
</tbody>

Answer №1

Ensure a condition is included on this line

 <div (click)="getSub(data.categoryId);" *ngIf="data.showAccordion">

In the function getSub(), set data.showAccordion to true when the user clicks. You must add a property called showAccordion in your json 'data'. Currently, it is not functioning because there is no apparent condition to hide the entire accordion. If this does not resolve the issue, providing a simple diagram illustrating your desired outcome would be beneficial.

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

Using Ajax to dynamically load jQuery accordion content

I have a container with an accordion: <div id="address_results"></div> When I populate the html (accordion) using an ajax call, it works correctly for the first call. However, subsequent calls do not animate the accordion properly. // Making ...

How to easily access object properties through ngModel select dropdown in Angular 2?

I am facing an issue when trying to access the properties of an object selected from a dropdown list in Angular 2 (RC1). Consider the following code snippet: <select required [(ngModel)]="model.plan"> <option selected="selected" disabled>Pla ...

Moment-Timezone defaults to the locale settings after the global Moment locale has been established

I am currently developing an application using Typescript that requires features from both Moment.js and moment-timezone. To localize the date and timestamps within the application, I have set moment's locale in the main app.ts file to match the langu ...

Tips for displaying and hiding content in Angular2

I have a component that toggles the visibility of elements by clicking a button. This is the snippet of my HTML code: <div *ngFor="let history of histories | sortdate: '-dateModified'"> <p><b>{{ history.remarks }}</b& ...

Error TS2322: Cannot assign type 'Promise<Hero | undefined>' to type 'Promise<Hero>'

I am currently studying angular4 using the angular tutorial. Here is a function to retrieve a hero from a service: @Injectable() export class HeroService { getHeroes(): Promise<Hero[]> { return new Promise(resolve => { // ...

1. Discovering NPM packages easily with Unpkg 2. Unve

Currently, I am struggling with adding an unpkg link for a specific NPM package called 'Angular Calendar' in my system.config.js file. The goal is to be able to run my website on a server without having to build it every time. I have been unable ...

Implementing a Collapse and Expand All feature within an Accordion Component

Hey there! I've been attempting to implement a Collapse All feature on my accordion but am having trouble figuring it out. The resource I've been referencing is this one. I've searched around and noticed that this accordion setup is a bit d ...

MongoDB Node.js throws a RangeError when trying to push a message that exceeds the maximum call stack size

I'm currently delving into the world of building a MEAN app and encountering an issue when attempting to append a message to a messages array within my User Model. While I can successfully create a new message and pass the user object, I face an error ...

A pronounced distinction exists between ionInput and ionChange functionality

Q. Can you explain the difference between (ionInput) and (ionChange) events in Ionic framework? When would it be more appropriate to use one over the other? I have experimented with both event handlers below and found that they produce the same expected r ...

Encountering an issue with Angular2 where it is unable to load a JSON file, presenting the error message: "Cannot resolve all parameters

I've been trying to incorporate a json file into my Angular app, but I can't seem to pinpoint the issue. The error message keeps indicating that it cannot resolve all parameters of my component. (I had no trouble loading data directly from the c ...

Setting up a Mean Stack on AWS Lightsail

I am currently experimenting with AWS Lightsail and struggling to grasp how to set it up properly. I have successfully created a Bitnami MEAN instance. On my local machine, I have Angular 6 running via CLI and a NODE API backend on two different ports: 42 ...

Customize the format of data labels in horizontal bar charts in NGX Charts

I am currently using ngx-charts, specifically the bar-horizontal module. My goal is to format the data labels and add a percentage symbol at the end. I attempted to use the [xAxisTickFormatting] property, but it seems that my values are located within the ...

activating a component by interacting with another component

How can I pass the uuid from parent to child component through a click event in Angular? App.component.ts import { Component } from '@angular/core'; import { v4 as uuid } from 'uuid'; @Component({ selector: 'my-app', tem ...

What is the importance of using ChangeDetectorRef.detectChanges() in Angular when integrating with Stripe?

Currently learning about integrating stripe elements with Angular and I'm intrigued by the use of the onChange method that calls detectChanges() at the end. The onChange function acts as an event listener for the stripe card, checking for errors upon ...

Having trouble installing Angular Material due to a getaddrinfo ENOTFOUND error?

When attempting to execute ng add @angular/material in my Angular project, I encountered the following error: Unable to fetch package metadata: request to http://registry.npmjs.org/@angular%2fmaterial failed, reason: getaddrinfo ENOTFOUND proxy.{companyna ...

Retrieve the information from the router link

I have a question regarding accessing the value passed through a routerlink in an anchor tag. How can I achieve this? https://i.stack.imgur.com/Qf500.png In the screenshot above, you can see that "bob" is the data being passed to the component when the a ...

Using React to iterate through the child components of the parent

I have created a component that can accept either a single child or multiple children. Here is an example with multiple children: <SideDataGridItem> <div id='top'> <div>A1</div> <div>B1</div> ...

Attempting to capture user keyboard input in TypeScript in order to transfer it to the Python script

Greetings! I'm in the process of developing a simple code that captures keyboard input from a typescript file and transmits it to a separate python file. The data transmission appears to be successful as I receive a message in the python terminal when ...

How can you present information with PrimeNG DataView?

I'm working on a personal project using Angular17 and I'm trying to display the items from my database using a PrimeNG component called DataView. I have imported the necessary modules in app.module and made a request to the service to fetch the d ...

What is the reason for the 'ng version' command returning a blank line as a response?

... Along with all other ng commands. Greetings! Recently, I decided to reinstall node.js due to a persistent issue, however, the problem still remains. https://i.sstatic.net/BZpkIszu.png After the reinstallation, I executed the initial commands. Npm i ...