在 ag-grid 中安装 mat-accordion
Fitting a mat-accordion inside ag-grid
我有一个相当标准的带有嵌套组件的 ag-grid 设置,如下所示:
import { Component } from '@angular/core';
import * as agGrid from 'ag-grid-community';
import { NestedMatExpansionPanelComponent } from './nested-mat-expansion-panel/nested-mat-expansion-panel.component';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.scss']
})
export class AppComponent {
title = 'aggrid-material-expansion-panel';
dataToShow = [
{ name: 'name1', companyCountry: 'UK', reference: 'Ref 1' },
{ name: 'name2', companyCountry: 'Germany', reference: 'Ref 2' },
{ name: 'name3', companyCountry: 'France', reference: 'Ref 3' },
{ name: 'name4', companyCountry: 'UK', reference: 'Ref 4' },
{ name: 'name5', companyCountry: 'USA', reference: 'Ref 5' },
];
columnDefs = [
// tslint:disable-next-line: max-line-length
{ headerName: 'Name', colId: 'name', cellRenderer: 'nestedMatExpansionPanelRenderer', filter: false, sortable: false },
{ headerName: 'Country', field: 'companyCountry', sortable: true, filter: true },
{ headerName: 'Reference', field: 'reference', sortable: true, filter: true }
];
// agG_rid
gridApi: agGrid.GridApi;
gridColumnApi: agGrid.ColumnApi;
gridOptions: agGrid.GridOptions = {};
public defaultColDef: any;
public columnTypes: any;
public context: any;
public frameworkComponents: any;
public sortingOrder: any;
constructor() {
this.initTable();
}
public onGridReady(params: any) {
// this.gridApi = this.gridOptions.api;
// this.gridColumnApi = this.gridOptions.columnApi;
this.gridApi = params.api;
this.gridColumnApi = params.columnApi;
this.gridApi.sizeColumnsToFit();
}
public initTable(): void {
this.defaultColDef = {
flex: 1,
autoHeight: true,
editable: false,
enableBrowserTooltips: true,
resizable: true,
filter: 'agTextColumnFilter',
suppressMenu: true,
floatingFilterComponentParams: { suppressFilterButton: true },
filterParams: { newRowsAction: 'keep' },
sortable: true,
};
this.columnTypes = {
numericColumn: { filter: 'agnumericColumnFilter' },
dateColumn: {
filter: 'agDateColumnFilter',
filterParams: {
newRowsAction: 'keep',
comparator(filterLocalDateAtMidnight, cellValue) {
const dateParts = cellValue.split('/');
const day = Number(dateParts[2]);
const month = Number(dateParts[1]) - 1;
const year = Number(dateParts[0]);
const cellDate = new Date(day, month, year);
if (cellDate < filterLocalDateAtMidnight) {
return -1;
} else if (cellDate > filterLocalDateAtMidnight) {
return 1;
} else {
return 0;
}
}
}
}
};
this.sortingOrder = ['desc', 'asc'];
this.context = { componentParent: this };
this.frameworkComponents = {
nestedMatExpansionPanelRenderer: NestedMatExpansionPanelComponent,
};
}
public onRowClicked($event) {
//
}
}
如你所见,我有
autoHeight: true,
因此每一行应该根据嵌入式手风琴的展开状态自动获得正确的高度但是行的输出高度计算不正确:
如何根据嵌入式扩展面板的状态自动调整每行的高度,以便在打开或关闭每行内的扩展面板时不会出现任何多余的空白space?
基本上,您遇到的问题是 MatExpansionPanel
和 ag-grid 中如何计算 autoHeight
的综合问题。
他们使用 autoHeight 从单元格中获取内容,并将其放入可能位于 document.body
元素处的临时不可见元素中。问题是此时元素没有应用正确的样式,它显示的是您面对的高度。所以,autoHeight
不是这里的解决方案,应该设置为 false
.
那么如何实现手动高度计算。 Ag-grid 在 node
上有一个 setRowHeight
属性。您可以使用它来设置高度。从那里需要通知网格 API 所有行都改变了它们的高度,并且可以通过调用 onRowHeightChanged()
在此基础上重新计算。获得此通信的一种方法是使用普通服务:
@Injectable()
export class GridService {
readonly updateHeight$ = new Subject<void>();
}
从您在模板中创建 ag-grid 的组件中,您应该将此服务器添加到提供程序数组并收听主题(使用去抖动以确保所有行最初都已计算):
@Component({
// ...,
providers: [GridService]
})
export class GridComponent {
gridApi: agGrid.GridApi;
constructor(private gs: GridService) {
this.gs.updateHeight$.pipe(
debounceTime(1)
).subscribe(() => {
this.gridApi?.onRowHeightChanged();
});
}
onGridReady(params: any) {
this.gridApi = params.api;
}
}
现在我们需要在单元格渲染器框架组件中有如下逻辑:
export class ExpansionPanelComponent implements ICellRendererAngularComp, AfterViewInit {
public params: ICellRendererParams;
constructor(private el: ElementRef<HTMLElement>, private gs: GridService) {}
agInit(params: ICellRendererParams): void {
this.params = params;
}
ngAfterViewInit(): void {
// inside setTimeout because the accordion is not properly sized (bug) and it will be too big
setTimeout(() => this.updateHeight());
}
updateHeight(): void {
// inside setTimeout because the accordion is not properly sized (bug) and it will be too big
setTimeout(() => {
this.params?.node.setRowHeight(this.el.nativeElement.offsetHeight);
this.gs.updateHeight$.next();
});
}
refresh(params: any): boolean {
return false;
}
}
我创建了一个有效的拉取请求 here
要删除左侧的填充,您可以将以下内容添加到您的 columnDefs 数组中:
columnDefs = [
{
//...,
cellRenderer: 'nestedMatExpansionPanelRenderer',
cellStyle: { 'padding-left': 0 }
},
// ...
]
我有一个相当标准的带有嵌套组件的 ag-grid 设置,如下所示:
import { Component } from '@angular/core';
import * as agGrid from 'ag-grid-community';
import { NestedMatExpansionPanelComponent } from './nested-mat-expansion-panel/nested-mat-expansion-panel.component';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.scss']
})
export class AppComponent {
title = 'aggrid-material-expansion-panel';
dataToShow = [
{ name: 'name1', companyCountry: 'UK', reference: 'Ref 1' },
{ name: 'name2', companyCountry: 'Germany', reference: 'Ref 2' },
{ name: 'name3', companyCountry: 'France', reference: 'Ref 3' },
{ name: 'name4', companyCountry: 'UK', reference: 'Ref 4' },
{ name: 'name5', companyCountry: 'USA', reference: 'Ref 5' },
];
columnDefs = [
// tslint:disable-next-line: max-line-length
{ headerName: 'Name', colId: 'name', cellRenderer: 'nestedMatExpansionPanelRenderer', filter: false, sortable: false },
{ headerName: 'Country', field: 'companyCountry', sortable: true, filter: true },
{ headerName: 'Reference', field: 'reference', sortable: true, filter: true }
];
// agG_rid
gridApi: agGrid.GridApi;
gridColumnApi: agGrid.ColumnApi;
gridOptions: agGrid.GridOptions = {};
public defaultColDef: any;
public columnTypes: any;
public context: any;
public frameworkComponents: any;
public sortingOrder: any;
constructor() {
this.initTable();
}
public onGridReady(params: any) {
// this.gridApi = this.gridOptions.api;
// this.gridColumnApi = this.gridOptions.columnApi;
this.gridApi = params.api;
this.gridColumnApi = params.columnApi;
this.gridApi.sizeColumnsToFit();
}
public initTable(): void {
this.defaultColDef = {
flex: 1,
autoHeight: true,
editable: false,
enableBrowserTooltips: true,
resizable: true,
filter: 'agTextColumnFilter',
suppressMenu: true,
floatingFilterComponentParams: { suppressFilterButton: true },
filterParams: { newRowsAction: 'keep' },
sortable: true,
};
this.columnTypes = {
numericColumn: { filter: 'agnumericColumnFilter' },
dateColumn: {
filter: 'agDateColumnFilter',
filterParams: {
newRowsAction: 'keep',
comparator(filterLocalDateAtMidnight, cellValue) {
const dateParts = cellValue.split('/');
const day = Number(dateParts[2]);
const month = Number(dateParts[1]) - 1;
const year = Number(dateParts[0]);
const cellDate = new Date(day, month, year);
if (cellDate < filterLocalDateAtMidnight) {
return -1;
} else if (cellDate > filterLocalDateAtMidnight) {
return 1;
} else {
return 0;
}
}
}
}
};
this.sortingOrder = ['desc', 'asc'];
this.context = { componentParent: this };
this.frameworkComponents = {
nestedMatExpansionPanelRenderer: NestedMatExpansionPanelComponent,
};
}
public onRowClicked($event) {
//
}
}
如你所见,我有
autoHeight: true,
因此每一行应该根据嵌入式手风琴的展开状态自动获得正确的高度但是行的输出高度计算不正确:
如何根据嵌入式扩展面板的状态自动调整每行的高度,以便在打开或关闭每行内的扩展面板时不会出现任何多余的空白space?
基本上,您遇到的问题是 MatExpansionPanel
和 ag-grid 中如何计算 autoHeight
的综合问题。
他们使用 autoHeight 从单元格中获取内容,并将其放入可能位于 document.body
元素处的临时不可见元素中。问题是此时元素没有应用正确的样式,它显示的是您面对的高度。所以,autoHeight
不是这里的解决方案,应该设置为 false
.
那么如何实现手动高度计算。 Ag-grid 在 node
上有一个 setRowHeight
属性。您可以使用它来设置高度。从那里需要通知网格 API 所有行都改变了它们的高度,并且可以通过调用 onRowHeightChanged()
在此基础上重新计算。获得此通信的一种方法是使用普通服务:
@Injectable()
export class GridService {
readonly updateHeight$ = new Subject<void>();
}
从您在模板中创建 ag-grid 的组件中,您应该将此服务器添加到提供程序数组并收听主题(使用去抖动以确保所有行最初都已计算):
@Component({
// ...,
providers: [GridService]
})
export class GridComponent {
gridApi: agGrid.GridApi;
constructor(private gs: GridService) {
this.gs.updateHeight$.pipe(
debounceTime(1)
).subscribe(() => {
this.gridApi?.onRowHeightChanged();
});
}
onGridReady(params: any) {
this.gridApi = params.api;
}
}
现在我们需要在单元格渲染器框架组件中有如下逻辑:
export class ExpansionPanelComponent implements ICellRendererAngularComp, AfterViewInit {
public params: ICellRendererParams;
constructor(private el: ElementRef<HTMLElement>, private gs: GridService) {}
agInit(params: ICellRendererParams): void {
this.params = params;
}
ngAfterViewInit(): void {
// inside setTimeout because the accordion is not properly sized (bug) and it will be too big
setTimeout(() => this.updateHeight());
}
updateHeight(): void {
// inside setTimeout because the accordion is not properly sized (bug) and it will be too big
setTimeout(() => {
this.params?.node.setRowHeight(this.el.nativeElement.offsetHeight);
this.gs.updateHeight$.next();
});
}
refresh(params: any): boolean {
return false;
}
}
我创建了一个有效的拉取请求 here
要删除左侧的填充,您可以将以下内容添加到您的 columnDefs 数组中:
columnDefs = [
{
//...,
cellRenderer: 'nestedMatExpansionPanelRenderer',
cellStyle: { 'padding-left': 0 }
},
// ...
]