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:

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

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

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

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

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

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

https://i.sstatic.net/98M8J.png

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 parameter type 'never[]' cannot be assigned to the type 'T | (() => T)' in the argument

Is it possible for the useFetch hook to allow any type of array to be used as the data type? hooks/useFetch.ts: const useFetch = <T extends any[]>(dataUrl: string) => { const [data, setData] = useState<T>([]); const [error, setError] = ...

Should ts-node be avoided in a production environment due to potential risks?

My current setup involves using ts-node with express in production and so far, it's been functioning smoothly. Am I missing out on any benefits by not compiling and running .js files instead? ...

Angular 4 - Automatically scroll to specific list item based on search query input

While I can achieve this with custom JavaScript, I'm curious if Angular 4 has any built-in features that could help. I have a list of checkboxes that are scrollable and a search input above them. My goal is to enable users to quickly jump to a section ...

Ways to dynamically configure Angular form data

Below is an Angular form group that I need help with. My goal is to initialize the form and if there is no data coming into the Input() data property, then set the form values as empty strings '' for user input. However, if there is indeed form d ...

What is behind the occurrence of the 'createObjectURL' error?

I am attempting to capture an image using the camera on my laptop. Previously, it was functioning flawlessly, but now I am encountering an error in my console: ERROR Error: Uncaught (in promise): TypeError: Failed to execute 'createObjectURL&apo ...

Deconstructing Angular 2 Custom Pipes

As I delve deeper into learning Angular 2, my recent endeavor involves creating a custom pipe to filter results in my gallery by category. Unfortunately, the resources I've been referring to lack detailed explanations on how custom pipes actually work ...

establish the data type for the key values when using Object.entries in TypeScript

Task Description: I have a set of different areas that need to undergo processing based on their type using the function areaProcessor. Specifically, only areas classified as 'toCreate' or 'toRemove' should be processed. type AreaType ...

unable to get ng-model and ng-options to function properly

Can anyone assist me with my issue? I can't figure out why it's not working. I want to generate a select dynamically from the Level[] retrieved from the database <select> <option>1</option> <option>2</option&g ...

What methods work most effectively for gathering user behavior data in a web application?

While I may not be a seasoned professional web developer, I have gained some experience with front-end frameworks like Angular 4 and Vue 2.0, as well as other tools that have allowed me to create web applications (SPA) and utilize AWS Lambda for the backen ...

Unveiling the method of retrieving a targeted value from JWT in React

I'm struggling to retrieve a specific value from my JWT token in React. I am using the react-jwt library to decode the token, and when I log it, I receive this output: Object { userId: "850dff98-54fb-4059-9e95-e44f5c30be0f", iat: 1698866016 ...

Encountering an Angular 13 ChunkLoadError during application deployment, despite the presence of the respective chunk

We encountered an issue with our application's upgrade from Angular 11 to 13. While running 'ng serve' on the local machine works fine, deploying it to our Azure app service causes the lazy loaded modules to fail loading. The specific error ...

Automatically closing the AppDateTimePicker modal in Vuexy theme after selecting a date

I am currently developing a Vue.js application using the Vuexy theme. One issue I have encountered is with a datetimepicker within a modal. The problem arises when I try to select a date on the datetimepicker component - it closes the modal instead of stay ...

What is the best way to showcase a variable from a typescript file in an HTML file with Angular?

In my TypeScript file, I have the following function: ngOnInit() { if (sessionStorage['loyalPage']) { this.page = Number(sessionStorage['loyalPage']); } this.webService.getLoyalPlayers(this.pag ...

Angular Typed Forms Cannot Assign Values to Incomplete Types

I have created a simple example to demonstrate the goal I am trying to achieve: In essence, there are two types defined as follows: type BaseType = FormGroup<{ user: FormControl<string>; }>; type SomeOtherType = FormGroup<{ user: FormC ...

Differences between HTTP requests made from a Node server and an Angular application

A group of aspiring developers is currently working on a cutting-edge web application using Angular 7 with the EAN (Angular Node Express) stack. Our project involves interacting with around 20-25 REST endpoints to retrieve data, perform necessary manipulat ...

Visual Studio 2017, ASP.NET framework, Typescript programming language, and node package manager

My ASP.net application in Visual Studio used to only utilize JavaScript, but now I am looking to incorporate Typescript. While the installation and transpiling process went smoothly, I encountered an issue when attempting to import modules. I decided to u ...

Best practices for annotating component props that can receive either a Component or a string representing an HTML tag

What is the correct way to annotate component props that can accept either a Component or a string representing an HTML tag? For instance, imagine I have a component that can receive a custom Component (which includes HTML tags like div, p, etc.). The cod ...

Executing an individual .ts file within a Next.js application using ts-node for the purpose of testing

I'm attempting to execute a single ES module .ts file within a Next.js project using the default configuration for quick debugging: npx ts-node lib/my_module.ts However, I encounter the following error: Warning: To load an ES module, set "type&q ...

"Firebase function fails to return Typescript class variable, resulting in 'undefined'

Being someone with a background in python/golang, I am now delving into ionic2. There seems to be an issue that I can't quite figure out due to my current level of knowledge in this stack. Perhaps I just need a way to reference the outer scope of this ...

Looking to display a single Angular component with varying data? I have a component in Angular that dynamically renders content based on the specific URL path

I have a component that dynamically renders data based on the URL '/lp/:pageId'. The :pageId parameter is used to fetch data from the server in the ngOnInit() lifecycle hook. ngOnInit(){ this.apiHelper.getData(this.route.snapshot.params.pageId) ...