在Angular PrimeNG中,可以使用p-columnToggler指令实现列选择功能。以下是一个包含代码示例的解决方法:
{{ col.header }}
{{ car[col.field] }}
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'app-your-component',
templateUrl: './your-component.component.html',
styleUrls: ['./your-component.component.css']
})
export class YourComponentComponent implements OnInit {
cars: any[];
cols: any[];
ngOnInit() {
this.cars = [
{ brand: 'BMW', color: 'Blue', year: 2015 },
{ brand: 'Audi', color: 'Red', year: 2013 },
{ brand: 'Mercedes', color: 'Green', year: 2017 }
];
this.cols = [
{ field: 'brand', header: 'Brand' },
{ field: 'color', header: 'Color' },
{ field: 'year', header: 'Year' }
];
}
}
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { TableModule } from 'primeng/table';
import { ColumnTogglerModule } from 'primeng/columntoggler';
import { YourComponentComponent } from './your-component/your-component.component';
@NgModule({
declarations: [YourComponentComponent],
imports: [
CommonModule,
TableModule,
ColumnTogglerModule
],
exports: [YourComponentComponent]
})
export class YourModule { }
这样,你就可以在Angular PrimeNG中实现列选择功能了。用户可以通过点击p-columnToggler组件来选择要显示的列。