滚动 ag-grid div 时,我无法将表头保留在 div 顶部。是否有特定的农业类别可用于获得此信息?
谢谢
编辑:
有代码:
HTML:
<md-tab ng-repeat="tab in mbpTabs">
<md-tab-label>{{tab.title}}</md-tab-label>
<md-tab-body>
<div ag-grid="tab.mbpTable.table" class="ag-dark ag-scrolls"></div>
</md-tab-body>
</md-tab>
JS网格定义如下:
this.cols = [{headerName: "Security",marryChildren: true,
children: [{headerName: "CTRL", field: this.FIELD_KEY_CTRL,hide: true,cellRenderer: MbpTable.prototype.ctrlRenderer.bind(this)},
{headerName: "Id", field: this.FIELD_KEY_ID,width: 0,hide: true},
{headerName: "Issuer", field: this.FIELD_KEY_ISSUER,width: 100},
{headerName: "Cusip", field: this.FIELD_KEY_CUSIP,width: 80,},
{headerName: "Board Label", field: this.FIELD_KEY_BOARD_LABEL,width: 150}]
},
{headerName: 'Best',marryChildren: true,
children: [{headerName: "Bid Size", field: this.FIELD_KEY_BEST_BID_SIZE, width: 125,cellClass:"mbp-ag-cell-best-bid",cellRenderer: MbpTable.prototype.szPriceRenderer.bind(this)},
{headerName: "Bid Price", field: this.FIELD_KEY_BEST_BID_PRICE, width: 125,cellClass:"mbp-ag-cell-best-bid",cellRenderer: MbpTable.prototype.szPriceRenderer.bind(this)},
{headerName: "Ask Price", field: this.FIELD_KEY_BEST_ASK_PRICE, width: 125,cellClass:"mbp-ag-cell-best-ask",cellRenderer: MbpTable.prototype.szPriceRenderer.bind(this)},
{headerName: "Ask Size", field: this.FIELD_KEY_BEST_ASK_SIZE, width: 125,cellClass:"mbp-ag-cell-best-ask",cellRenderer: MbpTable.prototype.szPriceRenderer.bind(this)}]
},
{headerName: this.headerMyOrder,marryChildren: true,
children: [{headerName: "Bid Size", field: this.FIELD_KEY_ORDER_BID_SIZE, width: 80,cellClass:"mbp-ag-cell-order",editable: true,cellRenderer: MbpTable.prototype.bidOrderRenderer.bind(this),cellEditor: "text",newValueHandler: MbpTable.prototype.sizeValueHandler.bind(this)},
{headerName: "Bid Price", field: this.FIELD_KEY_ORDER_BID_PRICE, width: 80,cellClass:"mbp-ag-cell-order",editable: true,cellRenderer: MbpTable.prototype.bidOrderRenderer.bind(this),cellEditor: "text",newValueHandler: MbpTable.prototype.priceValueHandler.bind(this)},//cellEditor: NumericCellEditor},
{headerName: "",headerCellTemplate:this.headerMyBidOrder, field: this.FIELD_KEY_ORDER_BID_ACTION,minWidth:18,maxWidth:18,width:18,cellClass:["mbp-ag-cell-order","mbp-ag-cell-order-action"],cellRenderer: MbpTable.prototype.bidOrderRenderer.bind(this)},
{headerName: "Ask Price", field: this.FIELD_KEY_ORDER_ASK_PRICE, width: 80,cellClass:"mbp-ag-cell-order",editable: true,cellRenderer: MbpTable.prototype.askOrderRenderer.bind(this),cellEditor: "text",newValueHandler: MbpTable.prototype.priceValueHandler.bind(this)},//cellEditor: NumericCellEditor},
{headerName: "Ask Size", field: this.FIELD_KEY_ORDER_ASK_SIZE, width: 80,cellClass:"mbp-ag-cell-order",editable: true,cellRenderer: MbpTable.prototype.askOrderRenderer.bind(this),cellEditor: "text",newValueHandler: MbpTable.prototype.sizeValueHandler.bind(this)},
{headerName: "",headerCellTemplate:this.headerMyAskOrder, field: this.FIELD_KEY_ORDER_ASK_ACTION,minWidth:18,maxWidth:18,width:18,cellClass:["mbp-ag-cell-order","mbp-ag-cell-order-action"],cellRenderer: MbpTable.prototype.askOrderRenderer.bind(this)}]}];
this.table={
columnDefs: this.cols,
rowData: MbpTable.prototype.createRndRows(numRows,offset,this),
rowSelection: 'multiple',
enableColResize: true,
enableSorting: true,
enableFilter: true,
groupHeaders: true,
...
};
出于商业原因,我在此处省略了表定义中的以下选项。
CSS 中的 ag-header 类没有被修改。
最佳答案
即使您像这样在窗口中向下滚动,我也希望表格中的标题始终可见。不确定这是否是 OP 想要的,但这是我的解决方案。
首先需要获取表格的表头元素和正文元素
header = document.querySelector('[ref="headerRoot"]');
body = document.querySelector('[ref="eBodyViewport"]');
// More on this later
original.position = header.style.position;
original.top = header.style.top;
original.zIndex = header.style.zIndex;
将滚动处理程序添加到 window
每次用户滚动时都会执行以下逻辑的对象window.addEventListener("scroll", () => {
// More on this below
})
逻辑很简单。它使标题是否具有粘性取决于其位置const shouldStick = header.getBoundingClientRect().top <= 0;
const shouldUnstick = body.getBoundingClientRect().top -
header.getBoundingClientRect().height > 0;
if (shouldStick) { stick(header); }
if (shouldUnstick) { unstick(header); }
最后添加一些css样式使其固定在窗口顶部function stick(header) {
header.style.position = "fixed";
header.style.top = "0";
// this is optional, but if you have other contents that overlap the
// header you may want to adjust the zIndex accordingly
header.style.zIndex = "2";
}
function unstick(header) {
header.style.position = original.position;
header.style.top = original.top;
header.style.zIndex = original.zIndex;
}
这是 React 中的示例实现。您需要做的就是复制这个钩子(Hook)并将其粘贴到组件的渲染方法中。而已。import { useCallback, useRef, useEffect } from "react";
const useStickyHeader = () => {
const headerElementRef = useRef();
const bodyElementRef = useRef();
const stickyRef = useRef(false);
const originalStyles = useRef({ position: "", top: "", zIndex: "" });
useEffect(() => {
headerElementRef.current = document.querySelector('[ref="headerRoot"]');
bodyElementRef.current = document.querySelector('[ref="eBodyViewport"]');
const header = headerElementRef.current;
originalStyles.current.position = header.style.position;
originalStyles.current.top = header.style.top;
originalStyles.current.zIndex = header.style.zIndex;
}, []);
const onScroll = useCallback(() => {
const header = headerElementRef.current;
const body = bodyElementRef.current;
if (!header || !body) return;
let shouldStick = false;
let shouldUnstick = false;
if (!stickyRef.current) {
shouldStick = header.getBoundingClientRect().top <= 0;
if (shouldStick) stickyRef.current = true;
} else {
shouldUnstick =
body.getBoundingClientRect().top -
header.getBoundingClientRect().height >
0;
if (shouldUnstick) stickyRef.current = false;
}
if (shouldStick) {
header.style.position = "fixed";
header.style.top = "0";
// this is optional, but if you have other contents that overlap the
// header you may want to adjust zIndex accordingly
header.style.zIndex = "2";
}
if (shouldUnstick) {
const original = originalStyles.current;
header.style.position = original.position;
header.style.top = original.top;
header.style.zIndex = original.zIndex;
}
}, []);
useEffect(() => {
window.addEventListener("scroll", onScroll);
return () => window.removeEventListener("scroll", onScroll);
});
return stickyRef;
};
用法function GridExample() {
useStickyHeader();
return (
<div style={{ width: "100%", height: "100%" }}>
<div className="grid-wrapper">
<div
style={{
height: "100%",
width: "100%"
}}
className="ag-theme-balham"
>
<AgGridReact
...
/>
</div>
</div>
</div>
);
}
活生生的例子关于javascript - ag-grid:滚动时将标题保持在顶部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40427888/