在AgGrid Angular的单元格渲染器中,使用下拉层时可能会遇到问题。这个问题通常表现为下拉层无法正确地显示在单元格中,导致渲染器无法正常工作。为了解决这个问题,可以参考以下两个步骤:
import { Component, ViewChild } from '@angular/core'; import { AgRendererComponent } from 'ag-grid-angular';
@Component({
selector: 'app-custom-renderer',
template:
,
})
export class CustomRendererComponent implements AgRendererComponent {
@ViewChild('container', { static: true }) container;
params: any; options: string[] = ['Option 1', 'Option 2'];
agInit(params: any) { this.params = params;
this.params.cellHeight = 100;
this.params.dropdownHeight = 200;
}
ngAfterViewInit() { const dropdown = document.createElement('ag-dropdown'); dropdown.style.width = '100%';
const selected = document.createElement('div');
selected.style.fontSize = '16px';
selected.style.padding = '10px';
selected.style.fontWeight = 'bold';
selected.innerHTML = 'Please select an option';
dropdown.addEventListener('selected', (event) => {
const value = event['detail'].value;
selected.innerHTML = this.params.value = value;
this.params.api.stopEditing();
});
dropdown.addEventListener('opened', () =>
this.params.api.setPopupParent(this.container.nativeElement)
);
dropdown.addEventListener('closed', () =>
this.params.api.setPopupParent(null)
);
this.options.forEach((option) => {
const item = document.createElement('ag-grid-list-item');
item.style.padding = '10px';
item.setAttribute('value', option);
item.innerHTML = option;
dropdown.appendChild(item);
});
dropdown.appendChild(selected);
this.container.nativeElement.appendChild(dropdown);
this.params.api.setPopupParent(this.container.nativeElement);
this.params.api.startEditingCell({
rowIndex: this.params.rowIndex,
colKey: this.params.column.getColId(),
charPress: null,
keyPress: null,
});
}
refresh(params) { this.params = params; } }
如果上述方法无法解决问题,您可以尝试使用CSS修改下拉层样式。您可以尝试修改下拉层的“position”属性,或者为其添加一个定位父元素。以下是一个示例:
.ag-theme-balham .