您需要将 columnwidth
与 一起设置[columnMode]="force"
,如下所示:
app.component.html
<ngx-datatable
class="material"
[rows]="rows"
[loadingIndicator]="loadingIndicator"
[columns]="columns"
[headerHeight]="50"
[footerHeight]="50"
[rowHeight]="'auto'"
[reorderable]="reorderable"
columnMode="force">
</ngx-datatable>
app.component.ts
columns = [
{ name: 'Name', prop: 'name'},
{ name: 'Gender', prop: 'gender'},
{ name: 'Company', prop: 'company', sortable: false }
];
rows = [
{name: "A", gender: "male", company: "abc"},
{name: "B", gender: "female", company: "abc"},
{name: "C", gender: "male", company: "abc"}
];
columnWidths = [
{column: "name", width: 50},
{column: "gender", width: 100},
{column: "company", width: 150}
]
ngOnInit() {
this.columns.forEach((col: any) => {
const colWidth = this.columnWidths.find(colWidth => colWidth.column === col.prop);
if (colWidth) {
col.width = colWidth.width;
}
});
}