javascript - 在 React 表中添加更多视图
问题描述
complete data
我有一种情况,我frontend
喜欢50 records
。
我必须在 中显示记录Table
。
有一次我只能show 10
记录。然后我有view more
按钮。click
查看更多. It will load the next
10 条记录`等等...
我可以得到一些帮助来解决这个问题吗?
表格代码:- 4 个组件
TableWrapper
TableBody
TableHead TableRow
TableWrapper.js
const TableWrapper = ({...props}) => {
const { tableData, columns } = props;
const addNewRows = () => {
console.log("Code for adding extra rows");
}
return (
<table>
<TableHead headers={columns} />
<TableBody rows={tableData} tableData={tableData} headers={columns} />
</table>
<fragment onClick=addNewRows>View more</fragment>
);
};
export default TableWrapper;
表体.js
const TableBody = ({...props}) => {
const { rows, tableData, headers } = props;
const [columnHeaderKeys, setColumnHeaderKeys] = useState([]);
useEffect(()=>{
let keysArray = [];
headers.map((headKeys) =>{
keysArray.push(headKeys.field)
})
{keysArray.length !== 0 ? setColumnHeaderKeys(keysArray) : setColumnHeaderKeys([])}
},[])
return (
<>
{tableData.length !== 0 ?
tableData.data.firstResponse.map((row, index)=>{
return (<tr key={index}><TableRow key={index} data={row} keys={columnHeaderKeys}/></tr>)
}) : <div>{"No Data is there"}</div>
}
</>
);
};
export default TableBody;
表头.js
const TableHead = ({...props}) => {
const { headers } = props;
return (
<>
{headers.map(header => (
<th
key={`table-header-${header.key}`}
style={header.style}>
{header.header}
</th>
))}
</>
);
};
export default TableHead;
TableRow.js
const TableRow = ({...props}) => {
const { keys, data } = props;
return (
<>
{keys.map((key)=>{
return <td key={props.data[key]}>{props.data[key]}</td>
})}
</>
);
};
export default TableRow;
提前致谢
解决方案
假设tableData
有所有行(50 条记录),您可以跟踪应该使用useState
钩子显示的记录数。然后只需根据该值拉出您想要的行并将其交给表格。
像这样的东西:
const PAGE_SIZE = 10;
const TableWrapper = ({...props}) => {
const { tableData, columns } = props;
const [ numToDisplay, setNumToDisplay ] = useState(PAGE_SIZE);
const addNewRows = () => {
console.log("Code for adding extra rows");
let newCount = numToDisplay + PAGE_SIZE;
if (numToDisplay > tableData.length) {
newCount = tableData.length;
}
setNumToDisplay(newCount)
}
const visibleData = tableData.slice(0, numToDisplay);
return (
<table>
<TableHead headers={columns} />
<TableBody tableData={visibleData} headers={columns} />
</table>
<fragment onClick=addNewRows>View more</fragment>
);
};
我也不认为你需要这个rows
道具,TableBody
因为它tableData
是一样的rows
(从你的描述中我可以看出)。
推荐阅读
- python - 删除键值对中存在的多换行符。想要在python中删除换行符后单行
- git - 禁用 repo init(和/或 git)的 GPG 签名检查
- python - Djangorestframework返回:TypeError:get_extra_actions()缺少1个必需的位置参数:声明get()时的'self'
- mongodb - 使用配置文件部署分片集群 ERROR
- hlsl - 如何确保 AppendStructuredBuffer 高于 32?
- php - Docker - jpeglib.h 未找到
- image - 如何在 5 秒间隔 QML 后触发点击图像的可见性
- python - 得到与我预期不同的输出顺序
- php - 会话超时后的 PHP 消息不显示
- application-server - 如何用 Petriflow 语言创建一个新案例?