4829

Angular Material - group and expand Rows based on ID column

<h3>Question</h3>

I am using angular-material Mat-Table to display list of records in grid format. My requirement is to group and expand rows based on ID column value in the grid.

I am fetching data from API and assigning it to mDataSource . To achieve the group and expand behavior, I assigned mDataSource to another MatTableDataSource. So that I can maintain original records in one datasource and grouped data in another datasource. But I am not sure on how to achieve this behavior in code and html.

studentcomponent.ts

public mDataSource: MatTableDataSource<StudentClass>; this._studentService.getStudentClass(requestStudent) .subscribe(data => { this.responseStudentClasses = data; let gridList; gridList = this.responseStudentClasses .StudentClass .map(item => new StudentClass(item)); this.mDataSource = new MatTableDataSource(gridList); }, exc => { this.ErrorMessage = this.ErrorMessage.setError(exc.message, null); });

mDataSource data will look lke this.

mDataSource = {Id:1, name: 'ABC', weight: 10 }, {Id:1, name: 'ABC', weight: 14 }, {Id:1, name: 'ABC', weight: 16 }, {Id:2, name: 'DEF', weight: 23 }, {Id:2, name: 'DEF', weight: 22 }, {Id:4, name: 'GHI', weight: 44 }, {Id:4, name: 'GHI', weight: 41 },

My Html:

<mat-table [dataSource]="mDataSource" multiTemplateDataRows matSort> <ng-container matColumnDef="Id"> <mat-header-cell *matHeaderCellDef mat-sort-header>ID</mat-header-cell> <mat-cell *matCellDef="let element">{{element.ID}}</mat-cell> </ng-container> <ng-container matColumnDef="name"> <mat-header-cell *matHeaderCellDef mat-sort-header>Name</mat-header-cell> <mat-cell *matCellDef="let element">{{element.name}}</mat-cell> </ng-container> <ng-container matColumnDef="weight"> <mat-header-cell *matHeaderCellDef mat-sort-header>Weight</mat-header-cell> <mat-cell *matCellDef="let element">{{element.weight}}</mat-cell> </ng-container> <mat-header-row *matHeaderRowDef="mGridDisplayCols"></mat-header-row> <mat-row *matRowDef="let row; columns: mGridDisplayCols;" (mouseover)="row.gridHovered = true" (mouseout)="row.gridHovered = false"> </mat-row> </mat-table>

My Grid will initiallay looks like this.

ID Name Weight 1 ABC 40 2 DEF 45 3 GHI 85

When I click on first row, it should expand and display rows like: (i.e., 10+14+16 =40)

ID Name Weight 1 ABC 40 1 ABC 10 1 ABC 14 1 ABC 16 2 DEF 45 3 GHI 85

I would be very grateful, if someone help me to group and expand rows.

来源:https://stackoverflow.com/questions/61831832/angular-material-group-and-expand-rows-based-on-id-column

Recommend

  • How do you model a Java for each loop on a UML sequence diagram?
  • Reloading an object not working in rspec
  • Start a JobIntentService Implicitly
  • Import named range data between Google Workbooks
  • Read Bluetooth RSSI on Windows 7/Vista/XP
  • JSF Datatable link to another page
  • How to marry NioServerSocketChannelFactory and WorkManager
  • How to ensure all properties have loaded in Silverlight ViewModel pattern (Concurrency Control?)
  • AWS Sagemaker | how to train text data | For ticket classification
  • Netty WebSocket Client Channel always gets inactive on Linux Server
  • Dialog with radio button save sharedpreferences but not the behaviour
  • How multiple executors are managed on the worker nodes with a Spark standalone cluster?
  • Custom url support in Android email
  • Using scrapy command “crawl” from django
  • One class instance used automatically throughout process
  • how to implement WSDL web services in iphone sdk
  • How to sort the union datastream of flink without watermark
  • macOS Swift Safari-like tabs
  • Expression Language in conditional routing - service in context
  • iText Java - add header to an existing pdf
  • Find text in file and set it as a variable. Batch file
  • Intent-Filter within a Service
  • How to get the remaining session timeout using SessionState?
  • Recognize Patterns of images JPG or PNG
  • Texture streaming in DirectX11, Immutable vs Dynamic
  • Working with django : Proxy setup
  • Getting specific color for specific value using JSON data
  • jQuery YQL SELECT FROM rss variable
  • Network communication options in Java (Client/Server)
  • MayAVI install on Python 3.6 [duplicate]
  • how to specify different css for ie
  • Content-Type alternative in MQTT
  • How to turn off notice reporting in xampp?
  • Cloud Code: Creating a Parse.File from URL
  • how to run ejabberd with Erlang on Heroku?
  • Terminal run dalvikvm with am.jar
  • How to handle div that is created dynamically in a table
  • Make checkout phone field optional for specific countries in WooCommerce
  • Call Microservice from another Microservice within Docker
  • ReferenceError: TextEncoder is not defined