Home > Blockchain >  Angular Error: Error: Could not find column with id when it's in TS
Angular Error: Error: Could not find column with id when it's in TS

Time:01-27

In my app, I have a table with multiple columns. I'm trying to fill it with the respected data but when the page opens I get the error Could not find column with id "PublishedParty" yet, it's in the TS file. Here is my code, what is wrong with it?

HTML:

<ng-container *ngIf="isPublishedParty == true" matColumnDef="PublishedParty">
    <th mat-header-cell *matHeaderCellDef mat-sort-header style="min-width: 100px !important;" [ngStyle]="{'color': 'black'}" style="text-align: center;">
      <b>
        Planlanan Miktar
      </b>
    </th>
    <td mat-cell *matCellDef="let row; let i = index" style="min-width: 100px !important;" style="text-align: center;">
      <div>
        Miktar: {{row.PublishedPartyQuantity | number}}
      </div>
      <div>
        Parti: {{row.PublishedPartyCount | number}}
      </div>
    </td>
</ng-container>

TS:

isPublishedParty = false;
private _materialPlan: IMaterialPlan = {};
@Input() 
set MaterialPlan(prm: IMaterialPlan){
  this._materialPlan = prm;
}

get MaterialPlan(): IMaterialPlan {
  return this._materialPlan;
}
displayedColumns: string[] = [
  'StockIntegrationCode',
  'ReceiptName',
  'PublishedParty',
];

dataSource: MatTableDataSource<IMaterialPlanReceiptResult>;
@ViewChild(MatPaginator, { static: true }) paginator: MatPaginator;
@ViewChild(MatSort, { static: true }) sort: MatSort;


constructor(
) {
  if(this._materialPlan.IncludePlannedParty == true){
    this.isPublishedParty == true;
  }
}

CodePudding user response:

I think, the problem is, that you hide the ng-container when the condition in your constructor is false for "this._materialPlan.IncludePlannedParty".

So the solution could be, to remove the column name from the displayColumns array:

private _materialPlan: IMaterialPlan = {};
@Input()
set MaterialPlan(prm: IMaterialPlan){
    this._materialPlan = prm;
}

get MaterialPlan(): IMaterialPlan {
    return this._materialPlan;
}
displayedColumns: string[] = [];

dataSource: MatTableDataSource<IMaterialPlanReceiptResult>;
@ViewChild(MatPaginator, { static: true }) paginator: MatPaginator;
@ViewChild(MatSort, { static: true }) sort: MatSort;


constructor(
) {
    this.displayedColumns = [
        'StockIntegrationCode',
        'ReceiptName',
    ];
    if(this._materialPlan.IncludePlannedParty == true){
        this.isPublishedParty == true;
        this.displayedColumns.push('PublishedParty');
    }
}

CodePudding user response:

For angular to render the table correctly isPublishedParty should be initialized as true. Otherwise *ngIf removes it from the template and you would get such an error. You could set isPublishedParty to false at later time, perhaps in ngAfterViewInit() cycle.

  •  Tags:  
  • Related