Set the column span on a particular column within a dynamic table utilizing the mat-table component

I have a dynamic mat-table where columns are created dynamically. I am looking to implement an expand column feature using [attr.colspan]. This functionality should apply the attribute when a click event is detected on a specific column header.

When I insert this feature into the HTML template, it only affects the first column. How can I make the [attr.colspan] apply dynamically to each individual column?

<ng-container
matColumnDef="{{ column }}"
*ngFor="let column of displayedColumns">
<th mat-header-cell *matHeaderCellDef (click)="headerEvent(column)">{{ column }}</th>
<td mat-cell *matCellDef="let element" [attr.colspan]="1">{{ element[column] }}</td>
</ng-container>

If you'd like to see an example, check out this StackBlitz.

Answer №1

Absolutely, feel free to utilize any expression, for example:

<th mat-header-cell *matHeaderCellDef (click)="headerEvent(column)">
  {{ column }}
</th>
<ng-container mat-cell *matCellDef="let element">
  <td
    *ngIf="!element.colspan || column != 'name'"
    [attr.colspan]="column == 'position'?element.colspan:null"
  >
    {{ element[column] }}
  </td>
</ng-container>

Furthermore, here's an example of the data you might have:

 [
  {position: 1, name: 'Hydrogen', weight: 1.0079, symbol: 'H'},
  {position: 2, name: 'Helium', weight: 4.0026, symbol: 'He',colspan:2}
 ]

Feel free to check out your version at this edited stackblitz link

Notice how a cell is "removed" when a colspan is created.

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 compatibility between `webpack-streams` and `@types/webpack`

I encountered an issue with my gulpfile while building it with TypeScript. Everything was working fine until I included @types/webpack-stream. Suddenly, I started getting a series of errors that looked quite ugly: node_modules/@types/webpack-stream/index.d ...

Encountered an issue while attempting to load the TSLint library for the document within Visual Studio Code

After setting up the latest versions of Visual Studio Code, Node.js, and Typescript on my Windows 10 system, I encountered an issue when trying to utilize TSLint in the terminal. A message appeared stating: Failed to load the TSLint library for the documen ...

Changing an item in a refined list

Here is an array written in Typescript: [ { "size": "100.34 KB", "name": "Phone.jpg", "documentoContentType": "image/jpeg", "type": "object" }, { "size": "606.34 KB", "name": "Tulips.jpg", "documentoContentType": "image/jpeg", "type": "flower" }, ...

Verify the data type of the returned information from the graphql query

Within my code, I am utilizing a graphql query through a hook that has been automatically generated by Codegen. Codegen not only creates return types but also all data types required. According to the types defined by codegen, the expected return type of m ...

How to revert TypeScript version in Visual Studio to 1.7

After updating VS2015 to Update 2, which also updated TypeScript to version 1.8, I encountered some issues with my code that required me to uninstall it. However, rolling back Update 2 did not revert TypeScript back to the previous version. Despite TypeScr ...

What is the best way to send an email with a time-sensitive code (token) using Firebase?

Currently, I am developing an application that requires sending a verification code to users before they can proceed with certain actions. For instance, when users log in to the app, they need to enter their email, password (authenticated using Firebase au ...

modify pseudo element's style in Angular based on a particular condition

I am trying to change the style of :before based on a condition. I attempted to implement the solution provided at , but it did not work as expected. Here is my code: .sender:before { content: ""; width: 0px; ...

Is it possible to capture and retain data, then transmit it x seconds after the initial data capture?

RxJS 4: I am attempting to capture and emit values after a certain interval of time has passed since the first value was received from a websocket. Essentially, once the first value is received, a timer will start to store subsequent incoming values and t ...

International replacement of external interface exportation

Currently, I am utilizing the @react-native-firebase/messaging library and invoking the method messaging().onNotificationOpenedApp(remoteMessage => ....) I am aiming to replace the property data within the type of remoteMessage in order to benefit from ...

Leveraging the p-steps module from PrimeNG for smaller screen sizes

I am facing an issue with resizing the p-steps component from PrimeNG to avoid overflow in smaller screen resolutions. The current code I am using is as follows: <div class="d-flex card mb-4"> <p-steps [model]="items" ...

Troubleshooting the failure of the addEventListener mouseEvent in an Angular environment

Recently, I've been encountering an issue with adding addEventListener to dynamically created HTML canvas elements. Everything was working fine before, but now none of the events seem to be triggered. Below is the code snippet I am currently using: t ...

There seems to be an issue with the React Native FlatList: It appears that there is no overload matching this call and some

I am currently learning React Native and attempting to create a basic chat room application. I am facing an issue with the FlatList component that I can't seem to resolve. Even though I have provided both the data prop and renderItem prop to the FlatL ...

Accessing base class properties in Typescript: A comprehensive guide

Although I have seen similar questions on this topic, my issue is unique. I have checked my class and am using it in the same way as others who have encountered similar problems. I extended class A into class B, but for some reason I cannot access A's ...

Is it feasible to conditionally set a function parameter as optional?

type TestType<A> = [A] extends [never] ? void : A class Singleton<T, A> { private ClassRef: (new (...args: A[]) => T) private args: TestType<A> private _instance?: T constructor(ClassRef: (new (...args: A[]) => T), ...

Bring in jspm libraries to your project via typescript

While researching how to import jspm packages into typescript, I noticed that most examples assumed the use of SystemJS for loading and interpreting them in the browser. However, I prefer using tsc to compile commonjs modules and only import the js code, a ...

Avoid connecting redux to a component in TypeScript and React

I am having an issue with the "connect" function not wrapping my App component, causing Redux to not work. I have tried cloning some repositories with react+redux+typescript and they all work fine, but my application does not. As a result, I am unable to ...

Passing a function variable from one Angular 2 component to another

Exploring Angular 2 and seeking assistance. I have developed a modal component named ModalContent, which includes the following function: @Component({ selector: 'modal-content', templateUrl: './app/d-modal/d.modal.content.component.html&apo ...

Guide to incorporating third-party JavaScript files and functions into my Angular web app

I have been trying to integrate external code (HTML, JS, and CSS files) into my Angular web application. Within this external code, the structure of the HTML file is as follows: index.html <html> <header> </header> <body> </bo ...

Unable to locate module, encountered a webpack alias issue while using typescript and react

I'm currently in the process of setting up aliases in webpack. My goal is to make importing components in App.js easier by replacing: ./components/layout/Header/Header with: @components/layout/Header/Header This way, I can avoid potential issues w ...

Converting a stringified array object to an object using Expressjs

When working with Angular, I am sending stringified data using FormData. Here is an example: this.formData.append('data', JSON.stringify(this.collections)) Now my challenge is converting this string back to an object in my Express backend. The d ...