Search code examples
angulartypescriptangular-materialangular-material-table

Property 'table' has no initializer and is not definitely assigned in the constructor


I want following the example in this [link] (https://javascript.plainenglish.io/beautiful-styling-for-drag-and-drop-rows-in-the-angular-datatable-6870768c5a8f) to create a Drag Drop in Material table.

app.component.ts:

   import { Component,ElementRef,ViewChild } from '@angular/core';
import {MatTable} from '@angular/material/table';
import { CdkDragDrop, moveItemInArray,transferArrayItem } from '@angular/cdk/drag-drop';

export interface PeriodicElement {
  name: string;
  position: number;
  weight: number;
  symbol: string;
}



const ELEMENT_DATA: PeriodicElement[] = [
  {position: 1, name: 'Hydrogen', weight: 1.0079, symbol: 'H'},
  {position: 2, name: 'Helium', weight: 4.0026, symbol: 'He'},
  {position: 3, name: 'Lithium', weight: 6.941, symbol: 'Li'},
  {position: 4, name: 'Beryllium', weight: 9.0122, symbol: 'Be'},
  {position: 5, name: 'Boron', weight: 10.811, symbol: 'B'},
  {position: 6, name: 'Carbon', weight: 12.0107, symbol: 'C'},
  {position: 7, name: 'Nitrogen', weight: 14.0067, symbol: 'N'},
  {position: 8, name: 'Oxygen', weight: 15.9994, symbol: 'O'},
  {position: 9, name: 'Fluorine', weight: 18.9984, symbol: 'F'},
  {position: 10, name: 'Neon', weight: 20.1797, symbol: 'Ne'},
];

const ELEMENT_DATA_DEST: PeriodicElement[] = [
  {position: 1, name: 'Nitogen S', weight: 2.0079, symbol: 'H'},
 
];

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
 
  @ViewChild('dataTable') table!: MatTable<PeriodicElement>;
  displayedColumns: string[] = ['position', 'name', 'weight', 'symbol'];
  dataSource = ELEMENT_DATA;
  inbox_lists=ELEMENT_DATA_DEST;

  dropTable(event: CdkDragDrop<PeriodicElement[]>): void {
 
    if (event.previousContainer === event.container) {
      moveItemInArray(event.container.data, event.previousIndex, event.currentIndex);
      this.table.renderRows();
    } 
     else {  

       transferArrayItem(
         event.previousContainer.data,
         event.container.data,
         event.previousIndex,
         event.currentIndex,
       );
       this.table.renderRows();
     }
  }
}

app.module.ts

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';
import { MatTableModule } from '@angular/material/table';
// import { FlexLayoutModule } from '@angular/flex-layout';
import {DragDropModule} from '@angular/cdk/drag-drop';
import {CommonModule} from '@angular/common';
import {BrowserAnimationsModule} from '@angular/platform-browser/animations';

import { AppComponent } from './app.component';

@NgModule({
  imports:      [
    CommonModule,
    BrowserModule,
    BrowserAnimationsModule,
    DragDropModule,
    MatTableModule,
  
  ],
  declarations: [ AppComponent ],
  bootstrap:    [ AppComponent ]
})
export class AppModule { }

app.component.html

   <div class="row" cdkDropListGroup>


    <table mat-table #dataTable [dataSource]="dataSource" [cdkDropListData]="dataSource" cdkDropList (cdkDropListDropped)="dropTable($event)">

  <!--- Note that these columns can be defined in any order.
        The actual rendered columns are set as a property on the row definition" -->

  <!-- Position Column -->
  <ng-container matColumnDef="position">
    <th mat-header-cell *matHeaderCellDef> No. </th>
    <td mat-cell *matCellDef="let element"> {{element.position}} </td>
  </ng-container>

  <!-- Name Column -->
  <ng-container matColumnDef="name">
    <th mat-header-cell *matHeaderCellDef> Name </th>
    <td mat-cell *matCellDef="let element"> {{element.name}} </td>
  </ng-container>

  <!-- Weight Column -->
  <ng-container matColumnDef="weight">
    <th mat-header-cell *matHeaderCellDef> Weight </th>
    <td mat-cell *matCellDef="let element"> {{element.weight}} </td>
  </ng-container>

  <!-- Symbol Column -->
  <ng-container matColumnDef="symbol">
    <th mat-header-cell *matHeaderCellDef> Symbol </th>
    <td mat-cell *matCellDef="let element"> {{element.symbol}} </td>
  </ng-container>

  <tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
  <tr mat-row *matRowDef="let row; columns: displayedColumns;" cdkDrag></tr>
</table>

<br>
<div cdkDropList [cdkDropListData]="inbox_lists" class="example-list" (cdkDropListDropped)="dropTable($event)">
  <div class="example-box" *ngFor="let item of inbox_lists" cdkDrag>{{item.name}}</div>
</div>

</div>

I am getting this error :

Property 'table' has no initializer and is not definitely assigned in the constructor.

30 @ViewChild('dataTable') table: MatTable;


Solution

  • Table is not initialized, only typed for the moment. So to clean this error, you can add a "!" to the property to say to the linter: " Hey, i know it does not hav a value yet, but it will be done later.

    @ViewChild('dataTable') table!: MatTable<PeriodicElement>;