提问人:Ayoub Benayache 提问时间:9/1/2023 更新时间:9/24/2023 访问量:49
如何使用 React-virtualized 显示嵌套的可变高度行
how to display nested variable height rows with react-virtualized
问:
import * as React from "react";
import { render } from "react-dom";
import {
List,
CellMeasurerCache,
CellMeasurer,
WindowScroller,
AutoSizer
} from "react-virtualized";
import { ListGroupItem, Panel } from "react-bootstrap";
import "./styles.css";
const cache = new CellMeasurerCache({
fixedWidth: true
});
const renderRow = ({ index, key, parent, style }) => {
return (
<CellMeasurer
rowIndex={index}
columnIndex={0}
key={key}
cache={cache}
parent={parent}
enableMargins
>
<div
style={{
...style,
height: "auto",
display: "flex",
flexDirection: "column"
}}
>
<span> Row {index}</span>
<span
style={{
border: "solid 1px #ccc",
height: 100 + (index % 30) * 10 + "px"
}}
>
random text with varible height
</span>
<span style={{ border: "solid red" }}>
{index % 5 === 0 ? (
<AutoSizer>
{({ height }) => <App length={10} depth={1} />}
</AutoSizer>
) : (
""
)}
</span>
</div>
</CellMeasurer>
);
};
const App = ({ length = 1000, depth }) => {
if (depth <= 1)
return (
<Panel>
<Panel.Body>
<WindowScroller>
{({ height, isScrolling, scrollTop, onChildScroll }) => (
<AutoSizer>
{({ width }) => (
<List
height={height}
width={width}
deferredMeasurementCache={cache}
isScrolling={isScrolling}
rowCount={length}
rowHeight={(params) =>
cache.rowHeight(params) -
(params.index < length - 1 ? 1 : 0)
}
rowRenderer={renderRow}
autoHeight
scrollTop={scrollTop}
onChildScroll={onChildScroll}
/>
)}
</AutoSizer>
)}
</WindowScroller>
</Panel.Body>
</Panel>
);
};
const rootElement = document.getElementById("root");
render(<App depth={0} />, rootElement);
如何生成具有可变行高(基于内容)的递归嵌套虚拟化列表, 它适用于父项,但不适用于子项。
完整代码示例: https://codesandbox.io/s/react-virtualized-bootstrap-list-group-forked-gqz5z5
答:
0赞
yatsemirsky
9/24/2023
#1
我自己也遇到了类似的问题,我找到了一个解决方案并为它创建了自己的库:react-dynamic-virtual-tree
评论