1
我试图渲染使用动态表数据结构如下反应:动态表阵营
{
numRows: 2,
numCols: 3,
cells: [
{
id: 1,
pos: {
row: 1,
col: 1
},
content: 'This is the content 1'
},
{
id: 2,
pos: {
row: 1,
col: 2
},
content: 'This is the content 2'
},
{
id: 3,
pos: {
row: 1,
col: 3
},
content: 'This is the content 2.5'
},
{
id: 4,
pos: {
row: 2,
col: 1
},
content: 'This is the content 3'
},
{
id: 5,
pos: {
row: 2,
col: 3
},
content: 'This is the content 4'
}
]
}
我觉得这个数据结构是最适合我的应用程序,用户可以编辑细胞按顺序,但如果有更好的方法,请让我知道。
我有以下逻辑将数据渲染到表中,但它包含很多循环,所以我想知道是否有更好/更有效的方式来呈现此数据结构?
let rows = []
for (let row = 1; row <= numRows; row++) {
let children = []
for (let col = 1; col <= numCols; col++) {
let hasCell = false
cells.forEach((cell) => {
if (cell.pos.row === row && cell.pos.col === col) {
hasCell = true
children.push(<Cell>{cell.content}</Cell>)
}
})
if (!hasCell) {
children.push(<Cell />)
}
}
rows.push(<Row>{children}</Row>)
感谢
谢谢! N^2是一大改进:) –