在子行中显示数组项(react-table)
Display items of array in subrow (react-table)
我正在使用 react-table 并且需要使用下面的数据结构创建子行。我已经成功地为数据数组中的每个对象创建了子行。但是,数据数组中的每个对象都包含另一个数组 "types."
我如何让每一行将 "type" 名称列为子行?
到目前为止我的代码如下:
Table:
import React from 'react';
import ReactTable from 'react-table';
const Table = (props) => {
const subComponent = row => {
return (
<div>
Names of "types" here respectively for each object in data array
(no column headers or anything needed)
</div>
);
};
return (
<ReactTable data={ props.data }
columns={ props.columns }
SubComponent={ subComponent } />
);
};
export default Table;
数据结构:
const data = [
{
id: '12345',
name: 'sports',
types: [
{
name: 'basketball',
id: '1'
},
{
name: 'soccer',
id: '2'
},
{
name: 'baseball',
id: '3'
}
]
},
{
id: '678910',
name: 'food',
types: [
{
name: 'pizza',
id: '4'
},
{
name: 'hamburger',
id: '5'
},
{
name: 'salad',
id: '6'
}
]
}
];
这里有一个很好的例子来说明如何做 https://codesandbox.io/s/github/tannerlinsley/react-table/tree/master/archives/v6-examples/react-table-sub-components
根据我的最佳猜测,您的代码将如下所示:
import React from 'react';
import ReactTable from 'react-table';
const Table = (props) => {
const subComponent = row => {
return (
<div>
row.Original.types.map((type, idx) => (
<div>{{type.id}}</div>
<div>{{type.name}}</div>
))
</div>
);
};
return (
<ReactTable data={ props.data }
columns={ props.columns }
SubComponent={ subComponent } />
);
};
export default Table;
您可以在 useTable optios 上重写 getSubRows 方法。
像这样:
const getSubRows = useCallback((row) => {
return row.types || [];
}, []);
我正在使用 react-table 并且需要使用下面的数据结构创建子行。我已经成功地为数据数组中的每个对象创建了子行。但是,数据数组中的每个对象都包含另一个数组 "types."
我如何让每一行将 "type" 名称列为子行?
到目前为止我的代码如下:
Table:
import React from 'react';
import ReactTable from 'react-table';
const Table = (props) => {
const subComponent = row => {
return (
<div>
Names of "types" here respectively for each object in data array
(no column headers or anything needed)
</div>
);
};
return (
<ReactTable data={ props.data }
columns={ props.columns }
SubComponent={ subComponent } />
);
};
export default Table;
数据结构:
const data = [
{
id: '12345',
name: 'sports',
types: [
{
name: 'basketball',
id: '1'
},
{
name: 'soccer',
id: '2'
},
{
name: 'baseball',
id: '3'
}
]
},
{
id: '678910',
name: 'food',
types: [
{
name: 'pizza',
id: '4'
},
{
name: 'hamburger',
id: '5'
},
{
name: 'salad',
id: '6'
}
]
}
];
这里有一个很好的例子来说明如何做 https://codesandbox.io/s/github/tannerlinsley/react-table/tree/master/archives/v6-examples/react-table-sub-components
根据我的最佳猜测,您的代码将如下所示:
import React from 'react';
import ReactTable from 'react-table';
const Table = (props) => {
const subComponent = row => {
return (
<div>
row.Original.types.map((type, idx) => (
<div>{{type.id}}</div>
<div>{{type.name}}</div>
))
</div>
);
};
return (
<ReactTable data={ props.data }
columns={ props.columns }
SubComponent={ subComponent } />
);
};
export default Table;
您可以在 useTable optios 上重写 getSubRows 方法。 像这样:
const getSubRows = useCallback((row) => {
return row.types || [];
}, []);