0

 export interface Statususers 
 {
     sysid:          Number                  ;
     LASTNAME:       String                  ;
     FIRSTNAME:      String                  ;
     FIRST_AND_LAST: String                  ;
     REGISTRATION:   String                  ;
     CHEATER_ID:     String                  ;
     UNIQUE_NUMBER:  Number                  ;
     TYPE:           String                  ;
     PASSWORD:       String                  ;
     USERNUMBER:     Number                  ;
 }

 import { Component }              from '@angular/core';
 import { Input }                  from '@angular/core';
 import { OnInit }                 from '@angular/core';
 import { ViewChild }              from '@angular/core';
 import { AfterViewInit }          from '@angular/core';
 import { Observable }             from 'rxjs/Observable';
 import { DataSource }             from '@angular/cdk/collections' ;
 import { MatPaginator }           from '@angular/material';
 import { MatTableDataSource}      from '@angular/material';
 import { PageEvent }              from '@angular/material';
 import { MatSort }                from '@angular/material';
 import { HttpClientModule }       from '@angular/common/http';
 import { HttpClient }             from '@angular/common/http';
 import { Statususers }            from '../../models/statususers.model';

 @Component({
   selector: 'app-statususers',
   templateUrl: './statususers.component.html',
   styleUrls: ['./statususers.component.css'], 
 })
 export class StatususersComponent implements OnInit, AfterViewInit 
 {
   displayedColumns  = [ 'registration', 'type'] ;
   dataSource        = new MatTableDataSource <Statususers>() ;  
   aircraftreg: string = 'None' ;

   @ViewChild(MatSort) sort: MatSort ;
   @ViewChild(MatPaginator) paginator: MatPaginator;

   constructor( private http: HttpClient  ) { }

   ngOnInit()                    // 'https://jsonplaceholder.typicode.com/users' 
    { 
      this.http.get<Statususers[] > ('http://huckleberrypp.com/angular/php/status_users.php').subscribe (statususers => { this.dataSource.data = statususers ;  } ) ;
    }  

   ngAfterViewInit()  
   {
     this.dataSource.sort      = this.sort ;
     this.dataSource.paginator = this.paginator ;
   }

   doFilter (filterValue: string ) 
   { this.dataSource.filter = filterValue.trim().toLowerCase() ; }

 selectRow(row) 
  {
  // alert(row['registration']);
   console.log(row['registration']);
  //  this.aircraftreg = 'C-GHHZ'   ;
    this.aircraftreg = (row['registration']);

  }

 }
 <div class="myAircraftBrowse">  
   <div class="example-header">
     <mat-form-field>
       <input matInput type= "text" (keyup)="doFilter($event.target.value)" placeholder="Enter aircraft filter here">
     </mat-form-field>
   </div>

   <h1 class="centerText"> {{aircraftreg}} </h1>

   <div class="example-container mat-elevation-z8">
     <mat-table #table [dataSource]="dataSource" matSort>
       <!--- Note that these columns can be defined in any order.  The actual rendered columns are set as a property on the row definition" -->

       <!-- Registration Column -->
       <ng-container matColumnDef="first_and_last" >
  <mat-header-cell *matHeaderCellDef mat-sort-header> First and Last </mat-header-cell>
  <mat-cell *matCellDef="let statususers"  > {{statususers.FIRST_AND_LAST}} </mat-cell>
       </ng-container>

       <!-- Type Column -->
       <ng-container matColumnDef="registration" >
    <mat-header-cell *matHeaderCellDef mat-sort-header > Registration </mat-header-cell>
  <mat-cell  #aircrftRegistration *matCellDef="let statususers" > {{statususers.REGISTRATION}}</mat-cell> 
       </ng-container>

       <!-- Hours Column -->
       <ng-container matColumnDef="type" >
  <mat-header-cell *matHeaderCellDef mat-sort-header> Type </mat-header-cell>
  <mat-cell #aircrftType *matCellDef="let statususers"  > {{statususers.TYPE}} </mat-cell>
       </ng-container>

       <mat-header-row *matHeaderRowDef="displayedColumns"></mat-header-row>
       <mat-row (click)="selectRow(row)" *matRowDef="let row; columns: displayedColumns;"  ></mat-row>

     </mat-table>
        <!--   [showFirstLastButtons]="true">   this was removed from list below -->
     <mat-paginator #paginator    
  [pageSize]="8"
  [pageSizeOptions]="[5, 10, 20]">
     </mat-paginator>

   </div>
 </div>  

I have a table in Angular Material and I would like to click on a row and have it return a cell element of my choice in to a variable so that I can use it elsewhere. The code below will display all the column names and the content of the clicked row on the console, but I want to just select one of the columns in Type Script and save it to a variable.

selectRow(row) 
  {
    console.log(row) ;
    this.aircraftreg = 'A row was clicked'   ;  
    // alert ('Aircraft selected') ;
  }

Thank-you for your assistance. enter image description here

Brian Fleming
  • 63
  • 1
  • 6

1 Answers1

2

You can simply get the column content from selected row like row['column Name']

For example my displayed columns are

displayedColumns = ['id', 'name', 'progress', 'color'];

I can access value of column name from selected row.

selectRow(row) {
  alert(row['name']);
 }

Here is the HTML to get the event from data-table row:

<mat-row (click)="selectRow(row)" *matRowDef="let row; columns: displayedColumns;">
</mat-row>

Edit: Another problem is, your displayedColumns is in lowercase while Statususers data coming with uppercase.

While accessing use like row['REGISTRATION']

mohit uprim
  • 4,288
  • 2
  • 20
  • 25
  • Hello, thank-you for your reply, but my code is identical to yours except for the alert(row['name']); but in my case when I substitute my field name, the alert and console both say undefined, even though the table is working fine. – Brian Fleming Mar 06 '18 at 06:04
  • can you share the HTML and TypeScript code related to data-table? – mohit uprim Mar 06 '18 at 08:10
  • Here are the HTML and TypeScript files, I am trying to capture the 'registration' field, and I want it to end up in 'aircraftreg' . – Brian Fleming Mar 06 '18 at 10:38
  • I can not seem to upload the text files as they are too big. – Brian Fleming Mar 06 '18 at 10:40
  • problem could be with parsing json data, can you share Statususers model also? – mohit uprim Mar 06 '18 at 11:04
  • export interface Statususers { sysid: Number ; LASTNAME: String ; FIRSTNAME: String ; FIRST_AND_LAST: String ; REGISTRATION: String ; CHEATER_ID: String ; UNIQUE_NUMBER: Number ; TYPE: String ; PASSWORD: String ; USERNUMBER: Number ; } – Brian Fleming Mar 06 '18 at 11:18
  • You can see the actual data by loading this link in your browser http://huckleberrypp.com/angular/php/status_users.php – Brian Fleming Mar 06 '18 at 11:26
  • The problem is, your displayedColumns is in lowercase while Statususers data coming with uppercase – mohit uprim Mar 06 '18 at 15:53
  • try this alert(row['REGISTRATION']); – mohit uprim Mar 06 '18 at 15:54
  • I found something which I think is interesting. Check out the image I just uploaded. If I right click and view all occurrences of registration , it says no results which would explain the undefined error message. Do you think maybe there is an import missing? – Brian Fleming Mar 06 '18 at 15:55
  • did u tried with Uppercase?, i am able to replicate the problem because your model and displayed columns in different case – mohit uprim Mar 06 '18 at 15:57
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/166334/discussion-between-mohit-uprim-and-brian-fleming). – mohit uprim Mar 06 '18 at 15:58
  • Ok, I just uploaded a new PHP called debug.php I have it hard coded to only show data for C-GIZO here is the link huckleberrypp.com/angular/php/… – Brian Fleming Mar 06 '18 at 17:01
  • 1
    accept this answer as it is resolved ..will continue the discussion for https://stackoverflow.com/questions/49117169/angular-material-how-do-i-pass-a-parameter-to-php – mohit uprim Mar 07 '18 at 04:07