如何使用react-virtualized显示嵌套的可变高度行

问题描述 投票:0回答:1
    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

reactjs nested-lists react-virtualized
1个回答
0
投票

我自己也遇到了类似的问题,我找到了一个解决方案并为其创建了自己的库:react-dynamic-virtual-tree

© www.soinside.com 2019 - 2024. All rights reserved.