Extend mat-table with 3 expanding columns and 1 clickable column

Is there a way to add different click events for specific columns of an Angular Material Table? Specifically, I would like the left column (Blue/LP) to trigger a click event that passes the LP value, while the right column triggers an expand action. Currently, my table expands across the entire row on click. How can I target individual columns with distinct actions?

https://i.stack.imgur.com/5L0N0.png

HTML:

<table mat-table [dataSource]="dataSource" multiTemplateDataRows class="mat-elevation-z8">
    <ng-container matColumnDef="{{column.name}}" *ngFor="let column of initColumns">
      <th mat-header-cell *matHeaderCellDef> {{column.display}} </th>
      <td mat-cell *matCellDef="let element"> {{element[column.name]}} </td>
    </ng-container>
    <ng-container matColumnDef="expandedDetail">
      <td mat-cell *matCellDef="let element" [attr.colspan]="columnsToDisplay.length">
        <div class="example-element-detail" [@detailExpand]="element.expanded ? 'expanded' : 'collapsed'">
          <table class="detail-table">
            <tr>
              <th>Weight</th>
              <th>Cube</th>
              <th>Zone</th>
            </tr>
            <tr>
              <td>{{element.LPWeight}}</td>
              <td>{{element.LPCube}}</td>
              <td>{{element.Zone}}</td>
            </tr>
          </table>
        </div>
      </td>
    </ng-container>
    <tr mat-header-row *matHeaderRowDef="columnsToDisplay"></tr>
    <tr mat-row *matRowDef="let element; columns: columnsToDisplay;" class="example-element-row"
      [class.example-expanded-row]="element.expanded" (click)="toggleRow(element)">
    </tr>
    <tr mat-row *matRowDef="let row; columns: ['expandedDetail']" class="example-detail-row"></tr>
  </table>

.TS

import { animate, state, style, transition, trigger } from '@angular/animations';
import { Component, OnInit } from '@angular/core';
import { LoadinfoService } from 'src/app/services/loadinfo.service';

@Component({
  selector: 'app-lpstoload',
  templateUrl: './lpstoload.page.html',
  styleUrls: ['./lpstoload.page.scss'], animations: [
    trigger('detailExpand', [
      state('collapsed', style({ height: '0px', minHeight: '0' })),
      state('expanded', style({ height: '*' })),
      transition('expanded => collapsed', animate('225ms cubic-bezier(0.4, 0.0, 0.2, 1)')),
    ]),
  ],

})

export class LpstoloadPage implements OnInit {
  dataSource = ELEMENT_DATA;
  initColumns: any[] = [
    {
      name: 'LP',
      display: 'LP'
    },
    {
      name: 'SourceZone',
      display: 'Source'
    },
    {
      name: 'StopNumber',
      display: 'Stop'
    },
    {
      name: 'CaseCount',
      display: 'Cases'
    }
  ];
  displayedColumns: any[] = this.initColumns.map(col => col.name);
  columnsToDisplay = ['LP', 'SourceZone', 'StopNumber', 'CaseCount'];
  expandedElement: PeriodicElement | null;
  private lpsToLoad: any;
  constructor(private loadInfoService: LoadinfoService) { }

  ngOnInit() {
    //this.lpsToLoad = this.loadInfoService.loadSummary.LPsToLoad;
  }

  toggleRow(element: { expanded: boolean; }) {
    element.expanded = !element.expanded
  }
}

export interface PeriodicElement {
  SG: string;
  LP: string;
  Status: String;
  Zone: string;
  SourceZone: string;
  StopNumber: string;
  CaseCount: number;
  LPWeight: number;
  LPCube: number;
  DBName: string;
}

const ELEMENT_DATA: PeriodicElement[] = [
  {
    "SG": "",
    "LP": "00006844763848356453",
    "Status": "STAGED",
    "Zone": "SSTG",
    "SourceZone": "DRYF",
    "StopNumber": "4",
    "CaseCount": 120.0,
    "LPWeight": 480.0,
    "LPCube": 69.859573364257813,
    "DBName": "IL_DRY-WMS"
  },
  {
    "SG": "",
    "LP": "00006844763871917089",
    "Status": "STAGED",
    "Zone": "SSTG",
    "SourceZone": "DRYF",
    "StopNumber": "4",
    "CaseCount": 27.0,
    "LPWeight": 391.5,
    "LPCube": 95.586883544921875,
    "DBName": "IL_DRY-WMS"
  },
  {
    "SG": "",
    "LP": "00006844763892120215",
    "Status": "STAGED",
    "Zone": "SSTG",
    "SourceZone": "D-LT",
    "StopNumber": "4",
    "CaseCount": 88.0,
    "LPWeight": 510.39199829101563,
    "LPCube": 67.747936964035034,
    "DBName": "IL_DRY-WMS"
  }
];

Answer №1

Just throwing an idea out there, but perhaps you could split a table row into two columns and have an event from one column trigger an action in the other. You could use an *ngIf boolean to display an overlay or any other element on one of the columns.

Instead of using mat-table, it might be worth considering creating a custom directive where rows are generated as if they were in a table. This way, you can fully customize it without things getting messy.

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

Ensure that an HTTP post request is successfully completed before proceeding to execute the next HTTP post request within a loop

I have a loop that sends a HTTP post request each time it iterates. for(let i = 1; i < this.data.length; i++) { let arr = this.data[i]; this.http.post('link here', { name: arr[0], gender: arr[1], course: ar ...

The behavior in Angular where updates to arrays in child components do not reflect the data passed from the parent is known as "

My child component is not listening for changes in data from the parent when using ArrayObjectParser. Any ideas on how to fix this issue? Please assist me. import { Component, OnInit, Inject, ViewChild, Input } from '@angular/core'; import { Form ...

What is the process for dynamically loading a component by its name in Angular 2?

I am currently incorporating dynamic loading of angular components in my application by using the following code snippet. export class WizardTabContentContainer { @ViewChild('target', { read: ViewContainerRef }) target: any; @Input() TabCont ...

Encountering the error message "Subscribe is not a function in Jasmine" while

Encountering an issue where I am receiving an error stating "subscribe is not a function" in Angular unit testing. Here is the call that I have implemented in my component: this.myService.employees.subscribe(emp => this.emp = emp); Despite creating a ...

I am having trouble getting my angular library published successfully

I'm facing an issue while trying to publish my angular package. I keep encountering this error. https://i.stack.imgur.com/nhYMY.png Here is a screenshot of my package.json file https://i.stack.imgur.com/mWsin.png. ...

Unable to inject basic service into component

Despite all my efforts, I am struggling to inject a simple service into an Angular2 component. Everything is transpiling correctly, but I keep encountering this error: EXCEPTION: TypeError: Cannot read property 'getSurveyItem' of undefined Even ...

How to retain the side menu icon in Ionic 2 even after navigating using navCtrl push

Whenever I navigate to a new page using navCtrl.push, the sidemenu icon (hamburger) disappears and is replaced by a back button instead of coexisting with it. What I am aiming for is to retain the sidemenu icon by positioning it on the right side of the i ...

Bidirectional data binding in angular 12 reactive forms

After working with angular for a while, I encountered an issue while trying to implement two-way binding. The code snippet below is where I'm facing difficulty. Since the use of [(ngModel)] has been deprecated in Angular 12 within formGroup, finding ...

Encountering issues with executing transactions in an Ionic 2 application when utilizing Sqlite functionality from Ionic Native

I am currently working on a project that involves setting up and querying a local sqlite database. I am utilizing the cordova-sqlite-plugin along with Sqlite from Ionic Native. Here is the code snippet responsible for opening the database and creating tab ...

Employ ion-grid for a layout reminiscent of Duolingo's design

I am exploring the idea of creating a layout similar to Duolingo's interface. I have an array that specifies which buttons should be displayed, and I want them to be organized in pairs, with any odd element centered within the layout. However, I am s ...

transfer item between a mother and offspring

In my project, I have a convention object that needs to be passed as an input to a child component. The convention ID is displayed correctly in the child's template, however, I encounter an issue where the convention appears as undefined when trying t ...

Karma Unit test: Issue with accessing the 'length' property of an undefined value has been encountered

While running karma unit tests, I encountered a similar issue and here is what I found: One of my unit tests was writing data to a json file, resulting in the following error: ERROR in TypeError: Cannot read property 'length' of undefined a ...

What is the best way to modify URLs in angular?

As a newcomer to Angular, I am currently working on an ecommerce project where my boss has requested URL rewrite behavior. For example. The website domain is abc.com and all the products are listed on the productlisting page abc.com/productlisting So, ...

Conceal the PayPal Button

Currently, I'm facing a challenge where I need to dynamically show or hide a PayPal button based on the status of my switch. The issue is that once the PayPal button is displayed, it remains visible even if the switch is toggled back to credit card pa ...

Nested HTTP requests in Angular using RxJS: Triggering component update after completion of the first HTTP request

I have a requirement to make two http requests sequentially. The values retrieved from the first call will be used in the second call. Additionally, I need to update my component once the first http request is completed and also update it once the second ...

What are the steps to implement SignalR Stream in an ASP.NET Core Angular application?

Attempting to utilize SignalR stream for sending real-time data to clients and displaying it on an Angular component's template. Below is a snippet of my code, where this function is linked to a button (click) event in the template: getMessage(m ...

What is the best way to send a string parameter from an Angular UI to a Node.js backend?

My goal is to transfer a string value from an Angular UI to a Node.js backend API, which will then search in MongoDB using the provided string value as shown below. I am attempting to receive input in enteredValue and pass it on to the http.get call as pa ...

Angular 2 NG Dynamic Grid Columns

Can a grid be created where column widths are not fixed in pixels? This would particularly come in handy when paired with sizeColumnsToFit(). Take, for instance, the scenario in which the ID and Name columns should adjust to content size while the Descrip ...

Decoding request header in Angular during app initialization

Currently, I have three domain names registered with Godaddy and they are all directing to the same server that is hosting my Angular 2 application. I am curious if there is a method to examine the request header in order to identify which of the three d ...

Is there a way to prevent Angular Material Buttons from overlapping a sticky bar when scrolling?

In my Angular application, I have a navigation bar at the top that sticks in place (position: sticky; top: 0;). Beneath the navigation bar is the content, which includes Angular material components such as mat-buttons or mat-cards. The issue arises when ...