当前位置:首页 > React

react table组件实现

2026-01-27 01:27:21React

实现React Table组件的方法

使用原生HTML表格

通过React组件封装原生HTML的<table>元素,结合状态管理实现动态渲染。适合简单表格需求,无需额外依赖。

function SimpleTable({ 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>
  );
}

使用react-table库

react-table是轻量级无UI的表格工具库,提供排序、分页、过滤等功能,需自行设计样式。

import { useTable } from 'react-table';

function ReactTable({ 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>
  );
}

集成Material-UI表格

Material-UI提供现成的样式化表格组件,适合需要快速实现Material Design风格的场景。

import { Table, TableBody, TableCell, TableContainer, TableHead, TableRow } from '@mui/material';

function MuiTable({ data }) {
  return (
    <TableContainer>
      <Table>
        <TableHead>
          <TableRow>
            {Object.keys(data[0]).map((key) => (
              <TableCell key={key}>{key}</TableCell>
            ))}
          </TableRow>
        </TableHead>
        <TableBody>
          {data.map((row, index) => (
            <TableRow key={index}>
              {Object.values(row).map((value, i) => (
                <TableCell key={i}>{value}</TableCell>
              ))}
            </TableRow>
          ))}
        </TableBody>
      </Table>
    </TableContainer>
  );
}

实现可编辑表格

通过状态管理和事件处理实现单元格编辑功能,适用于需要用户交互的场景。

function EditableTable({ initialData }) {
  const [data, setData] = useState(initialData);

  const handleChange = (rowIndex, key, value) => {
    const newData = [...data];
    newData[rowIndex][key] = value;
    setData(newData);
  };

  return (
    <table>
      <thead>...</thead>
      <tbody>
        {data.map((row, rowIndex) => (
          <tr key={rowIndex}>
            {Object.entries(row).map(([key, value], colIndex) => (
              <td key={colIndex}>
                <input
                  value={value}
                  onChange={(e) => handleChange(rowIndex, key, e.target.value)}
                />
              </td>
            ))}
          </tr>
        ))}
      </tbody>
    </table>
  );
}

虚拟滚动优化

使用react-window等库实现虚拟滚动,解决大数据量渲染性能问题。

import { FixedSizeList as List } from 'react-window';

function VirtualizedTable({ columns, data }) {
  const Row = ({ index, style }) => (
    <div style={style}>
      {columns.map(column => (
        <span key={column.key}>{data[index][column.key]}</span>
      ))}
    </div>
  );

  return (
    <List
      height={500}
      itemCount={data.length}
      itemSize={50}
      width="100%"
    >
      {Row}
    </List>
  );
}

react table组件实现

标签: 组件react
分享给朋友:

相关文章

react 如何引入css

react 如何引入css

在 React 中引入 CSS 的方法 React 提供了多种引入 CSS 的方式,可以根据项目需求选择合适的方法。以下是常见的几种方式: 内联样式 内联样式直接在组件中通过 style 属性定义,…

vue动态组件实现

vue动态组件实现

Vue 动态组件实现方法 使用 component 标签与 is 属性 通过 Vue 内置的 <component> 标签和 is 属性实现动态组件切换。is 的值可以是已注册的组件名或导…

vue组件的实现

vue组件的实现

Vue 组件的实现方式 Vue 组件可以通过多种方式实现,主要包括单文件组件(SFC)、全局注册和局部注册。以下是常见的实现方法: 单文件组件(SFC) 单文件组件是 Vue 最推荐的组件化开发方…

vue实现组件循环图片

vue实现组件循环图片

Vue 实现组件循环图片的方法 在 Vue 中实现组件循环图片通常可以通过 v-for 指令结合动态数据绑定完成。以下是几种常见的实现方式: 使用静态图片路径数组 假设有一组本地图片路径,可以通过数…

vue穿梭框组件实现

vue穿梭框组件实现

实现 Vue 穿梭框组件的基本思路 穿梭框(Transfer)组件通常用于在两个列表之间移动数据项。核心功能包括左侧列表、右侧列表、移动按钮(左移、右移、全选等)以及数据项的渲染与交互。 基础结构…

vue实现下拉框组件

vue实现下拉框组件

Vue 下拉框组件实现 基础实现 创建基础下拉框组件,使用v-model实现双向数据绑定: <template> <div class="dropdown">…