【发布时间】:2022-09-30 23:12:01
【问题描述】:
每当它们“重叠”居中的显示列组时,我都会尝试更新我的固定列样式。对于左侧固定列,这很简单,只需检查 onBodyScroll 事件e.left > 0。对于右固定列,这比较棘手,因为我看不到通过 ag-grid api/columnApi 确定正确偏移量的明确方法。我想可以在这里使用 refs,或者在ColumnModel 上使用私有值,但我想知道是否有更好的方法,可能使用 ag-grid 函数?
下面是一个如何完成的示例,但它在ColumnModel 上使用私有值。
//...
const [leftPinnedOverlaying, setLeftPinnedOverlaying] = useState(false);
const [rightPinnedOverlaying, setRightPinnedOverlaying] = useState(true);
const hasMounted = useRef(false);
return (
<div style={{ width: \'100%\', height: \'100%\' }}>
<pre>
{JSON.stringify({leftPinnedOverlaying,rightPinnedOverlaying})}
</pre>
<div className=\"example-wrapper\">
<div
id=\"myGrid\"
style={{
height: \'100%\',
width: \'100%\',
}}
className=\"ag-theme-alpine\"
>
<AgGridReact
defaultColDef={{ resizable: true }}
debug={true}
rowData={rowData}
onGridReady={onGridReady}
onBodyScroll={(e)=> {
// first pass seems to pass non-ideal state
if(!hasMounted.current) {
hasMounted.current=true
return
}
setLeftPinnedOverlaying(e.left > 0);
const columnModel = e.columnApi.columnModel;
// Get around these properties being private by indexing via array
const scrollWidth = columnModel[\"scrollWidth\"] || 0;
const bodyWidth = columnModel[\"bodyWidth\"] || 0;
setRightPinnedOverlaying((e.left+scrollWidth) < bodyWidth)
}}
>
<AgGridColumn
headerName=\"Athlete\"
field=\"athlete\"
width={150}
pinned=\"left\"
cellStyle={leftPinnedOverlaying ? {backgroundColor: \'blue\'}:{backgroundColor: \'white\'}}
/>
// ...
</div>
</div>
</div>)
标签: ag-grid ag-grid-react