export class DataTableColTogglerDemo implements OnInit {
cars: Car[];
cols: any[];
columnOptions: SelectItem[];
constructor(private carService: CarService) { }
ngOnInit() {
this.carService.getCarsSmall().then(cars => this.cars = cars);
this.cols = [
{field: 'vin', header: 'Vin'},
{field: 'year', header: 'Year'},
{field: 'brand', header: 'Brand'},
{field: 'color', header: 'Color'}
];
this.columnOptions = [];
for(let i = 0; i < this.cols.length; i++) {
this.columnOptions.push({label: this.cols[i].header, value: this.cols[i]});
}
}
}
<p-dataTable [value]="cars">
<header>
<div style="text-align:left">
<p-multiSelect [options]="columnOptions" [(ngModel)]="cols"></p-multiSelect>
</div>
</header>
<p-column *ngFor="#col of cols" [field]="col.field" [header]="col.header"></p-column>
</p-dataTable>