英文:
How to create responsive grid that fills width using react-virtualized's Grid?
问题
Context
我想使用 react-virtualized Grid 来创建一个响应式的卡片网格。
卡片的数据将是一维的(非二维)。
这些卡片都将有一个最小宽度,它们将填满整个网格的宽度,并在需要时换行到下一行。
我该如何在 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
已解决
好的,我能够弄清楚这个问题并自己创建一个基本的实现。最终,我通过一个基于当前网格宽度的函数来计算列数和列宽。
以下是源代码和实时演示,供其他人受益。
代码和实时演示
英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论