如何使用react-virtualized的Grid创建填充宽度的响应式网格?

huangapple go评论54阅读模式
英文:

How to create responsive grid that fills width using react-virtualized's Grid?

问题

Context

我想使用 react-virtualized Grid 来创建一个响应式的卡片网格。
卡片的数据将是一维的(非二维)。

这些卡片都将有一个最小宽度,它们将填满整个网格的宽度,并在需要时换行到下一行。

示例(使用 CSS 网格和 minmax 属性)

我该如何在 react-virtualized 的 Grid 中实现这个效果?

Current Code

我目前正在使用 react-virtualized Grid 与以下组件一起使用:

  • AutoSizer
  • WindowScroller
import _ from "lodash";
import { AutoSizer, Grid, WindowScroller } from "react-virtualized";
function GridExample() {
  const items = _.range(100).map((num) => ({ name: `Collection ${num}` })); // 数据是一维数组

  function renderItem({ columnIndex, key, rowIndex, style }) {
    return (
      <div key={key} style={style}>
        {items[rowIndex].name}
      </div>
    );
  }

  return (
    <WindowScroller>
      {({ height, isScrolling, onChildScroll, scrollTop }) => (
        <AutoSizer disableHeight>
          {({ width }) => (
            <Grid
              autoHeight
              columnCount={1} // 我想将其更改为动态的列数
              columnWidth={100} // 我想将其更改为动态的宽度
              width={width}
              height={height}
              rowCount={items.length}
              rowHeight={50}
              cellRenderer={renderItem}
              isScrolling={isScrolling}
              scrollTop={scrollTop}
              onScroll={onChildScroll}
            />
          )}
        </AutoSizer>
      )}
    </WindowScroller>
  );
}

我尝试使用 ColumnSizer,但它导致我的项目不渲染,我不确定它是否适合这个任务。我尝试搜索文档或其他示例,但没有找到相关信息。

非常感谢您的帮助。非常感谢。

英文:

Context

I would like to use react-virtualized Grid to create a responsive grid of cards.
The data of the cards will be one dimensional (non 2D).

The cards will each have a minimum width and they will fill the entire width of the grid, wrapping to the next row when needed.

Example (using CSS grid and minmax property)

How can I achieve this with react-virtualized's Grid?

Current Code

I am currently using react-virtualized Grid inconjunction with

  • AutoSizer
  • WindowScroller
import _ from "lodash";
import { AutoSizer, Grid, WindowScroller } from "react-virtualized";
function GridExample() {
  const items = _.range(100).map((num) => ({ name: `Collection ${num}` })); // data is a one-dimensional array

  function renderItem({ columnIndex, key, rowIndex, style }) {
    return (
      <div key={key} style={style}>
        {items[rowIndex].name}
      </div>
    );
  }

  return (
    <WindowScroller>
      {({ height, isScrolling, onChildScroll, scrollTop }) => (
        <AutoSizer disableHeight>
          {({ width }) => (
            <Grid
              autoHeight
              columnCount={1} // I want to change this to be dynamic number of columns
              columnWidth={100} // I want to change this to be dynamic widths
              width={width}
              height={height}
              rowCount={items.length}
              rowHeight={50}
              cellRenderer={renderItem}
              isScrolling={isScrolling}
              scrollTop={scrollTop}
              onScroll={onChildScroll}
            />
          )}
        </AutoSizer>
      )}
    </WindowScroller>
  );
}

I have tried to use ColumnSizer, but it stopped my items from rendering and I am not sure that it is the right tool for the job. I've tried searching for docs or other examples, but came up empty.

Help would be greatly appreciated. Thank you so much.

答案1

得分: 1

已解决

好的,我能够弄清楚这个问题并自己创建一个基本的实现。最终,我通过一个基于当前网格宽度的函数来计算列数和列宽。

以下是源代码和实时演示,供其他人受益。

代码和实时演示

CodeSandbox

英文:

Resolved

Okay I was able to figure this out and create a basic implementation on my own. I ended up calculating the column counts and column widths through a function based on the current grid width.

Here is the source code and live demo for anyone else who could benefit from it.

Code and Live Demo

CodeSandbox

huangapple
  • 本文由 发表于 2023年3月9日 14:44:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/75681227.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定