Dynamic table row that expands to show additional rows sourced from various data sets

Is there a way to expand the table row in an angular-material table when it is clicked, showing multiple sets of rows in the same column as the table? The new rows should share the same column header but not necessarily come from the same data source.

When clicking on a row, I would like to see additional rows expanding below in the same column. Here is an example:

Upon clicking a row, the expanded view should look like this:

If clicked again, the expanded rows should collapse back to the default table view:

If two rows are clicked, the layout should appear like this:

I am working with Angular 8 and TypeScript

Answer №1

When working with mat-table, simply add a new property to the array (e.g. "expanded") and then update the condition in the div

<div class="example-element-detail"
   [@detailExpand]="element.expanded? 'expanded' : 'collapsed'">
...
</div>

For the (click) function in each row:

<tr mat-row ...
      [class.example-expanded-row]="expandedElement === element.expanded"
      (click)="element.expanded=!element.expanded">
</tr>

Check out this stackblitz for more details

If you're using your own table, follow the same steps by adding an "expanded" property to each element in the array

NOTE: An alternative option is to create an array of boolean values

expanded:boolean[]=[]

Use this array when iterating through items in *ngFor like so:

(click)="expanded[i]=!expanded[i]"

And

*ngIf="expanded[i]"

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

The chosen option from the dropdown menu fails to register as selected

In my Angular 8 application, I am facing an issue with a dropdown list. The data for the dropdown list is fetched from the backend. However, when I select a value from the dropdown list, it remains undefined or not selected. Interestingly, if I manually ...

Observe how Observable breaks down strings into individual letters (like in Karma and Angular)

Attempting to simulate an HTTP service in Angular tests (Karma), I included the following in the providers array: { provide: service, useValue: { getData: () => new Observable((subscriber) => { subscriber.next('i ...

The React useState Props error message TS2322: Cannot assign type 'string' to type 'number'

I'm attempting to pass Props to React useState Hooks. Both of my props are required and should be numbers, but I keep receiving a Typescript error stating: Type 'string' is not assignable to type 'number'. TS2322 However, I am ...

What is preventing me from using the "@" symbol to substitute the lengthy relative path in my __tests__ directory?

When I initialized my Vue project using vue-cli, I encountered an issue where the use of @ in my src folder was functioning correctly, but not in my __tests__ folder. I have already added configurations to my tsconfig.json file, although I am unsure if i ...

Transferring information to a navigated module using Angular2

I am currently facing a scenario where I have a component being loaded via routing, and my goal is to pass data from the parent component into this child component. How exactly can I achieve this task effectively? Parent Component Class export class Home ...

What is the best way to make my if statement pause until a GET request finishes (GUARD) with the help of Angular?

I am currently working on implementing admin routes for my Angular app, and I have used a role guard to handle this. The code snippet below showcases my implementation: However, I would like the get request to finish executing before the if statement begi ...

Error message "Potential Undefined Object" detected on a variable that is not an object in React with TypeScript

After finding and addressing the other 5-6 questions, I managed to partially fix it by using the "!" operator. Please do not remove this question for duplication purposes. However, what baffles me is that the variable is not recognized as an object. Here i ...

Ways to transfer JavaScript arguments to CSS style

Currently, I am developing a web service using Angular and Spring Boot. In this project, I have implemented cdkDrag functionality to allow users to place images in specific locations within the workspace. My goal is to maintain the placement of these image ...

Angular AutoComplete feature does not accurately filter the list items

I need to implement an auto-complete feature for the county field due to a large number of items in the list causing inconvenience to users who have to scroll extensively. Currently, there are two issues with the code. The first problem is that although t ...

Incorporate Lodash into your Angular2 project within Visual Studio 2015

I've been attempting to incorporate the lodash dependency into my project, but I keep encountering issues during the VS2015 build process. The error message in the build output states "Build: Cannot find module 'lodash'", causing the build t ...

The specified file path '.../node_modules/@nomicfoundation/hardhat-core/src' could not be located

I have successfully set up a TypeScript hardhat project, but I encountered an issue in /***/node_modules/@nomicfoundation/hardhat-chai-matchers/src/tsconfig.json: { "extends": "../../../config/typescript/tsconfig.json", "compil ...

Error encountered when unsubscribing from Angular datatable while closing ngx-bootstrap modal

I am currently facing an issue with Angular DataTable and ngx-bootstrap modal. After closing the modal, the datatable throws an unsubscription error and fails to initialize properly. I have tried various solutions such as re-rendering and unsubscribing o ...

What could be the reason for my router navigate function not functioning properly in Angular 8?

I need help with redirecting to another component in my Angular application. Currently, I have the following code: HomeComponent checkUrl(reference) { if (reference != this.ref) { this.router.navigate(['/еrror']); } } Thi ...

Issue TS2769: No matching overload found for this call. The type 'string | null' cannot be assigned to type 'string | string[]'

export class AuthService { constructor(private http: HttpClient, private webService: WebRequestService, private router: Router) { } login(email: string, password: string) { return this.webService.login(email, password).pipe( shareReplay(), ...

Issues with the ionViewDidLoad() function?

Having some trouble implementing Email Authentication in my project. The method ionViewDidLoad is not being recognized. I also tried using the method ionViewWillLoad() but that didn't work either. Any ideas on what might be causing this issue? Here&a ...

Instructions for utilizing ObjectId with a string _id on the client side

Is there a way to retrieve a document using the _id in string format? Here is an example of the code on the client side: 'use client' ... const Page(){ ... fetch("api/get_data", { method: 'POST', ...

Combine observables from an id using rxjs in Angular with Firestore

In my Firestore database, I have stored information about people and their pets. Each pet is linked to its owner through an owner ID. // Collection / DocumentID: {data} persons / 'person1': { name: 'Romea' } persons / 'person2&ap ...

Exploring Objects with Union Types in TypeScript

Looking to iterate through an object that combines two interfaces. interface Family { cat: string; age: string; family: string; lastYearFamily: string; } interface Model { cat: string; age: string; ...

The "pointer" cursor style is simply not functioning in any way

Here is the angular template code I am working with: <!-- Modal --> <ng-template #levelsmodal let-c="close" let-d="dismiss"> <div class="modal-header"> Select the levels you want to show in the table and chart ...

best practices for choosing items from a dropdown menu using Angular

I have a dropdown list displaying existing tags/chips created by users in the past. However, I'm having an issue where when I select a tag from the dropdown list, it doesn't show up in my input field (similar to the Chart tag currently). I am abl ...