0

I am using angular material table for displaying data. I am having string array of displayColumns but I get 'Could not find column with id 'Name'' error. Here is my error. Help is appreciated.

import { Component, OnInit, ViewChild } from '@angular/core';
import { DataService } from '../core/services/data.service';
import { People, PeopleDetails, PeopleDetailRequest, PeopleWithTotals } from "../models/datamodelsObj";
import { DataSource } from '@angular/cdk';
import { MdPaginator } from '@angular/material';
import { BehaviorSubject } from 'rxjs/BehaviorSubject';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/operator/startWith';
import 'rxjs/add/observable/merge';
import 'rxjs/add/operator/map';

@Component({
  selector: 'app-peoplesummary',
  templateUrl: './people-summary.component.html',
  styleUrls: ['./people-summary.component.scss'],
  providers:[]
})
export class PeopleSummaryComponent implements OnInit {


  displayedColumns = ['Name', 'Address', 'Email', 'Phone'];
  psDatabase : PeopleSummaryDatabase|null;
  dataSource: PeopleSummaryDataSource | null;

  @ViewChild(MdPaginator) paginator : MdPaginator;

  constructor(private dataService: DataService,private pagi:MdPaginator) { 
    this.psDatabase=new PeopleSummaryDatabase(dataService);
    this.dataSource=new PeopleSummaryDataSource(this.psDatabase,pagi);
  }

  ngOnInit() {
    this.dataSource = new PeopleSummaryDataSource(this.psDatabase, this.paginator);
    console.log(this.dataSource);
  }

}
export class PeopleSummaryDatabase {
  people: People;
  public dataChange: BehaviorSubject<PeopleDetails[]> = new BehaviorSubject<PeopleDetails[]>([]);
  get data(): PeopleDetails[] { return this.dataChange.value }

  private _dataService: DataService



  constructor(_dataService: DataService) {
    _dataService.getPersonDetails(this.assetDetailRequestObj).subscribe(data=>this.dataChange.next(data));
   }

}
export class PeopleSummaryDataSource extends DataSource<any> {

    constructor(private _groupDatabase :PeopleSummaryDatabase, private _paginator: MdPaginator){
      super();
    }

    connect(): Observable<PersonDetails[]> {
      const displayDataChanges = [
        this._groupDatabase.dataChange,
        this._paginator.page
      ];
      return Observable.merge(...displayDataChanges).map(() => {
        const data = this._groupDatabase.data.slice();
        console.log(data);

        const startIndex = this._paginator.pageIndex * this._paginator.pageSize;
        return data.splice(startIndex, this._paginator.pageSize);
      })
    }

    disconnect() {}
  }

and here is my html

<md-table #table [dataSource]="dataSource">

      <ng-container *cdkColumnDef="Name">
        <md-header-cell *cdkCellDef>Name</md-header-cell>
        <md-cell *cdkCellDef="let row"> {{row.Name}} </md-cell>
      </ng-container>

      <ng-container *cdkColumnDef="Address">
        <md-header-cell *cdkCellDef>Address</md-header-cell>
        <md-cell *cdkCellDef="let row"> {{row.Address}} </md-cell>
      </ng-container>

      <ng-container *cdkColumnDef="Email">
        <md-header-cell *cdkCellDef>Email</md-header-cell>
        <md-cell *cdkCellDef="let row"> {{row.Email}} </md-cell>
      </ng-container>

      <ng-container *cdkColumnDef="Phone">
        <md-header-cell *cdkCellDef>Phone</md-header-cell>
        <md-cell *cdkCellDef="let row"> {{row.Phone}} </md-cell>
      </ng-container>


      <md-header-row *cdkHeaderRowDef="displayedColumns"></md-header-row>
      <md-row *cdkRowDef="let row; columns: displayedColumns;"></md-row>

    </md-table>

    <md-paginator #paginator
                  [length]="psDatabase.data.length"
                  [pageIndex]="0"
                  [pageSize]="10"
                  [pageSizeOptions]="[5, 10, 25, 100]">
    </md-paginator>

When I debug the I see data but don't understand why I get column not found error. Thank You

Stephen Paul
  • 37,253
  • 15
  • 92
  • 74
user3154990
  • 555
  • 4
  • 13
  • 27
  • Out of curiosity, if you switch the order of your `displayedColumns` or column template definitions, does the error change from `Name` to something else? – Will Howell Sep 15 '17 at 20:51

1 Answers1

3

Try removing the asterisks from the beggining of your "cdkColumnDef" attributes.

<md-table #table [dataSource]="dataSource">

  <ng-container cdkColumnDef="Name">
    <md-header-cell *cdkCellDef>Name</md-header-cell>
    <md-cell *cdkCellDef="let row"> {{row.Name}} </md-cell>
  </ng-container>

  <ng-container cdkColumnDef="Address">
    <md-header-cell *cdkCellDef>Address</md-header-cell>
    <md-cell *cdkCellDef="let row"> {{row.Address}} </md-cell>
  </ng-container>

  <ng-container cdkColumnDef="Email">
    <md-header-cell *cdkCellDef>Email</md-header-cell>
    <md-cell *cdkCellDef="let row"> {{row.Email}} </md-cell>
  </ng-container>

  <ng-container cdkColumnDef="Phone">
    <md-header-cell *cdkCellDef>Phone</md-header-cell>
    <md-cell *cdkCellDef="let row"> {{row.Phone}} </md-cell>
  </ng-container>


  <md-header-row *cdkHeaderRowDef="displayedColumns"></md-header-row>
  <md-row *cdkRowDef="let row; columns: displayedColumns;"></md-row>

</md-table>

<md-paginator #paginator
              [length]="psDatabase.data.length"
              [pageIndex]="0"
              [pageSize]="10"
              [pageSizeOptions]="[5, 10, 25, 100]">
</md-paginator>
Paullus Nava
  • 70
  • 2
  • 9
  • 2
    I am glad that this worked for you. The funny part of my answer is that I reached your question, just 8 hours before, trying to solve the exact same problem. As soon as I figured out how to solve it I came back here to share. =) – Paullus Nava Sep 17 '17 at 02:16
  • can you please describe what removing the asterisk does? – Nandun Aug 09 '18 at 02:01
  • To be honest I never thought about that. I just discovered that it is a "syntatic sugar" and we can create our own directives which can use it. I found this link here on [Stackoverflow](https://stackoverflow.com/questions/40078189/angular-2-why-asterisk) that has some explanations, and [here](https://angular.io/guide/structural-directives) the content on Angular documentantion. sorry for not being able to give you any answers directly. I hope that helps you. – Paullus Nava Aug 09 '18 at 11:24
  • 1
    I had the same error but due to a different issue. Thought I’d share in case it helps someone: make sure your cdkColumnDef elements are actually being rendered! Mine weren’t, because they were inside an ng-container with an *ngFor loop, which wasn’t iterating due to the source array being empty. – Jonathan Mar 28 '19 at 16:18
  • 1
    @Jonathan thanks. Your comment saved me from a lot of head scratching. I had an ngIf which was conditional on the table data not being empty. When it was empty then the column defs basically weren't present. The errors given by angular were frankly quite poor. – Paul Rooney May 28 '19 at 05:00