react实现表格代码

以下是一个使用React实现表格的完整代码示例,包含基本功能如动态渲染数据、排序和分页:
基础表格实现
import React from 'react';
const BasicTable = ({ data }) => {
return (
<table>
<thead>
<tr>
{Object.keys(data[0]).map((key) => (
<th key={key}>{key}</th>
))}
</tr>
</thead>
<tbody>
{data.map((row, index) => (
<tr key={index}>
{Object.values(row).map((value, i) => (
<td key={i}>{value}</td>
))}
</tr>
))}
</tbody>
</table>
);
};
带排序功能的表格
import React, { useState } from 'react';
const SortableTable = ({ data }) => {
const [sortConfig, setSortConfig] = useState({ key: null, direction: 'asc' });
const requestSort = (key) => {
let direction = 'asc';
if (sortConfig.key === key && sortConfig.direction === 'asc') {
direction = 'desc';
}
setSortConfig({ key, direction });
};
const sortedData = React.useMemo(() => {
if (!sortConfig.key) return data;
return [...data].sort((a, b) => {
if (a[sortConfig.key] < b[sortConfig.key]) {
return sortConfig.direction === 'asc' ? -1 : 1;
}
if (a[sortConfig.key] > b[sortConfig.key]) {
return sortConfig.direction === 'asc' ? 1 : -1;
}
return 0;
});
}, [data, sortConfig]);
return (
<table>
<thead>
<tr>
{Object.keys(data[0]).map((key) => (
<th key={key} onClick={() => requestSort(key)}>
{key}
{sortConfig.key === key && (
<span>{sortConfig.direction === 'asc' ? ' ↑' : ' ↓'}</span>
)}
</th>
))}
</tr>
</thead>
<tbody>
{sortedData.map((row, index) => (
<tr key={index}>
{Object.values(row).map((value, i) => (
<td key={i}>{value}</td>
))}
</tr>
))}
</tbody>
</table>
);
};
带分页的表格
import React, { useState } from 'react';
const PaginatedTable = ({ data, itemsPerPage = 5 }) => {
const [currentPage, setCurrentPage] = useState(1);
const totalPages = Math.ceil(data.length / itemsPerPage);
const currentData = data.slice(
(currentPage - 1) * itemsPerPage,
currentPage * itemsPerPage
);
return (
<div>
<table>
<thead>
<tr>
{Object.keys(data[0]).map((key) => (
<th key={key}>{key}</th>
))}
</tr>
</thead>
<tbody>
{currentData.map((row, index) => (
<tr key={index}>
{Object.values(row).map((value, i) => (
<td key={i}>{value}</td>
))}
</tr>
))}
</tbody>
</table>
<div>
<button
disabled={currentPage === 1}
onClick={() => setCurrentPage(currentPage - 1)}
>
上一页
</button>
<span> 第 {currentPage} 页/共 {totalPages} 页 </span>
<button
disabled={currentPage === totalPages}
onClick={() => setCurrentPage(currentPage + 1)}
>
下一页
</button>
</div>
</div>
);
};
使用第三方库react-table
import React from 'react';
import { useTable } from 'react-table';
const ReactTableExample = ({ columns, data }) => {
const {
getTableProps,
getTableBodyProps,
headerGroups,
rows,
prepareRow,
} = useTable({ columns, data });
return (
<table {...getTableProps()}>
<thead>
{headerGroups.map(headerGroup => (
<tr {...headerGroup.getHeaderGroupProps()}>
{headerGroup.headers.map(column => (
<th {...column.getHeaderProps()}>{column.render('Header')}</th>
))}
</tr>
))}
</thead>
<tbody {...getTableBodyProps()}>
{rows.map(row => {
prepareRow(row);
return (
<tr {...row.getRowProps()}>
{row.cells.map(cell => (
<td {...cell.getCellProps()}>{cell.render('Cell')}</td>
))}
</tr>
);
})}
</tbody>
</table>
);
};
样式优化建议
table {
width: 100%;
border-collapse: collapse;
}
th, td {
padding: 8px 12px;
text-align: left;
border-bottom: 1px solid #ddd;
}
th {
background-color: #f2f2f2;
cursor: pointer;
}
tr:hover {
background-color: #f5f5f5;
}
.pagination {
margin-top: 16px;
display: flex;
justify-content: center;
align-items: center;
}
这些实现可以根据实际需求进行组合和扩展。对于更复杂的需求,推荐使用专门的表格库如react-table或Material-UI的表格组件。







